diff --git a/packages/vite/build.config.ts b/packages/vite/build.config.ts deleted file mode 100644 index f29d9a99..00000000 --- a/packages/vite/build.config.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { defineBuildConfig } from 'unbuild' - -export default defineBuildConfig({ - entries: [ - 'src/vite', - ], - clean: true, - declaration: true, - externals: [ - 'vite', - 'vite-plugin-inspect', - 'vite-plugin-vue-inspector', - 'execa', - ], - rollup: { - emitCJS: true, - inlineDependencies: true, - }, -}) diff --git a/packages/vite/esbuild-shims/cjs-shim.ts b/packages/vite/esbuild-shims/cjs-shim.ts new file mode 100644 index 00000000..57962331 --- /dev/null +++ b/packages/vite/esbuild-shims/cjs-shim.ts @@ -0,0 +1,7 @@ +import { createRequire } from 'node:module' +import path from 'node:path' +import url from 'node:url' + +globalThis.require = createRequire(import.meta.url) +globalThis.__filename = url.fileURLToPath(import.meta.url) +globalThis.__dirname = path.dirname(__filename) diff --git a/packages/vite/package.json b/packages/vite/package.json index 7024095c..b883bc0a 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -43,8 +43,8 @@ "node": ">=v14.21.3" }, "scripts": { - "build": "unbuild", - "stub": "unbuild --stub" + "build": "tsup", + "stub": "tsup --watch" }, "peerDependencies": { "vite": "^3.1.0 || ^4.0.0-0 || ^5.0.0-0" diff --git a/packages/vite/tsup.config.ts b/packages/vite/tsup.config.ts new file mode 100644 index 00000000..3a542fbc --- /dev/null +++ b/packages/vite/tsup.config.ts @@ -0,0 +1,35 @@ +import { defineConfig } from 'tsup' + +export default defineConfig({ + entryPoints: [ + 'src/vite.ts', + ], + // To avoid esbuild compile import.meta.url to import_meta.url + // See: https://github.com/vitejs/vite/issues/503 + target: 'es2020', + clean: true, + format: ['esm', 'cjs'], + // Force esbuild to use .mjs extension for ESM output + outExtension({ format }) { + if (format === 'esm') { + return { + js: '.mjs', + dts: '.d.ts', + } + } + else if (format === 'cjs') { + return { + js: '.cjs', + dts: '.d.cts', + } + } + return { + js: '.js', + dts: '.d.ts', + } + }, + // See: https://github.com/evanw/esbuild/issues/1921 + inject: ['./esbuild-shims/cjs-shim.ts'], + dts: true, + shims: true, +})