From 33c2388b0cb487e042a398847c5036fba2aa409f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E6=98=8A=E5=AE=87?= Date: Tue, 5 May 2026 19:36:54 +0800 Subject: [PATCH] fix(build): preserve ESM imports in published package Rewrite emitted relative ESM specifiers to explicit .js targets so packaged installs load correctly in OpenCode. Use the plugin tool subpath directly to avoid root-entry resolution failures and fix #564. --- package-lock.json | 4 +- package.json | 4 +- script/fix-esm-imports.mjs | 76 +++++++++++++++++++++++++++++++ src/plugin.ts | 2 +- src/plugin/quota-fallback.test.ts | 2 +- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 script/fix-esm-imports.mjs diff --git a/package-lock.json b/package-lock.json index e4f8a6b..9f14b09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "opencode-antigravity-auth", - "version": "1.3.3-beta.2", + "version": "1.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "opencode-antigravity-auth", - "version": "1.3.3-beta.2", + "version": "1.6.0", "license": "MIT", "dependencies": { "@openauthjs/openauth": "^0.4.3", diff --git a/package.json b/package.json index 3adf186..1a21a82 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "LICENSE" ], "scripts": { - "build": "tsc -p tsconfig.build.json", + "build": "tsc -p tsconfig.build.json && node script/fix-esm-imports.mjs", "build:schema": "npx tsx script/build-schema.ts", "typecheck": "tsc --noEmit", "test": "vitest run", @@ -64,4 +64,4 @@ "xdg-basedir": "^5.1.0", "zod": "^4.0.0" } -} \ No newline at end of file +} diff --git a/script/fix-esm-imports.mjs b/script/fix-esm-imports.mjs new file mode 100644 index 0000000..f3ebd68 --- /dev/null +++ b/script/fix-esm-imports.mjs @@ -0,0 +1,76 @@ +import { existsSync } from "node:fs" +import { readdir, readFile, stat, writeFile } from "node:fs/promises" +import path from "node:path" +import { fileURLToPath } from "node:url" + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..") +const distDir = path.join(repoRoot, "dist") + +const importPatterns = [ + /(from\s*["'])(\.{1,2}\/[^"']+)(["'])/g, + /(import\s*\(\s*["'])(\.{1,2}\/[^"']+)(["'])/g, + /(\bimport\s*["'])(\.{1,2}\/[^"']+)(["'])/g, +] + +function hasExtension(specifier) { + return path.posix.extname(specifier) !== "" +} + +function resolveSpecifier(filePath, specifier) { + if (hasExtension(specifier)) return specifier + + const targetPath = path.resolve(path.dirname(filePath), specifier) + if (existsSync(`${targetPath}.js`)) return `${specifier}.js` + if (existsSync(path.join(targetPath, "index.js"))) return `${specifier}/index.js` + return specifier +} + +async function listJsFiles(dir) { + const entries = await readdir(dir) + const files = [] + + for (const entry of entries) { + const entryPath = path.join(dir, entry) + const entryStat = await stat(entryPath) + if (entryStat.isDirectory()) { + files.push(...await listJsFiles(entryPath)) + } else if (entry.endsWith(".js")) { + files.push(entryPath) + } + } + + return files +} + +async function fixFile(filePath) { + const source = await readFile(filePath, "utf8") + let output = source + + for (const pattern of importPatterns) { + output = output.replace(pattern, (match, prefix, specifier, suffix) => { + return `${prefix}${resolveSpecifier(filePath, specifier)}${suffix}` + }) + } + + if (output !== source) { + await writeFile(filePath, output) + return true + } + + return false +} + +async function main() { + if (!existsSync(distDir)) return + + const files = await listJsFiles(distDir) + let changed = 0 + + for (const file of files) { + if (await fixFile(file)) changed += 1 + } + + console.log(`Fixed ESM import specifiers in ${changed} dist file(s)`) +} + +await main() diff --git a/src/plugin.ts b/src/plugin.ts index ee8b624..9422b4f 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,5 +1,5 @@ import { exec } from "node:child_process"; -import { tool } from "@opencode-ai/plugin"; +import { tool } from "@opencode-ai/plugin/tool"; import { ANTIGRAVITY_DEFAULT_PROJECT_ID, ANTIGRAVITY_ENDPOINT_FALLBACKS, diff --git a/src/plugin/quota-fallback.test.ts b/src/plugin/quota-fallback.test.ts index 3575a99..349df83 100644 --- a/src/plugin/quota-fallback.test.ts +++ b/src/plugin/quota-fallback.test.ts @@ -29,7 +29,7 @@ let getHeaderStyleFromUrl: GetHeaderStyleFromUrl | undefined; let resolveHeaderRoutingDecision: ResolveHeaderRoutingDecision | undefined; beforeAll(async () => { - vi.mock("@opencode-ai/plugin", () => ({ + vi.mock("@opencode-ai/plugin/tool", () => ({ tool: vi.fn(), }));