diff --git a/packages/vite/misc/false.d.ts b/packages/vite/misc/false.d.ts new file mode 100644 index 00000000000000..b2ba15eb1e7fc3 --- /dev/null +++ b/packages/vite/misc/false.d.ts @@ -0,0 +1,2 @@ +declare const result: boolean +export default result diff --git a/packages/vite/misc/false.js b/packages/vite/misc/false.js new file mode 100644 index 00000000000000..2693369b44337b --- /dev/null +++ b/packages/vite/misc/false.js @@ -0,0 +1 @@ +export default false diff --git a/packages/vite/misc/true.d.ts b/packages/vite/misc/true.d.ts new file mode 100644 index 00000000000000..b2ba15eb1e7fc3 --- /dev/null +++ b/packages/vite/misc/true.d.ts @@ -0,0 +1,2 @@ +declare const result: boolean +export default result diff --git a/packages/vite/misc/true.js b/packages/vite/misc/true.js new file mode 100644 index 00000000000000..186b120756be19 --- /dev/null +++ b/packages/vite/misc/true.js @@ -0,0 +1 @@ +export default true diff --git a/packages/vite/package.json b/packages/vite/package.json index 366dc1bad420d9..646e7bf0cc89e2 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -20,22 +20,14 @@ "types": "./dist/node/index.d.ts", "exports": { ".": { - "import": { - "types": "./dist/node/index.d.ts", - "default": "./dist/node/index.js" - }, - "require": { - "types": "./index.d.cts", - "default": "./index.cjs" - } + "module-sync": "./dist/node/index.js", + "import": "./dist/node/index.js", + "require": "./index.cjs" }, "./client": { "types": "./client.d.ts" }, - "./module-runner": { - "types": "./dist/node/module-runner.d.ts", - "import": "./dist/node/module-runner.js" - }, + "./module-runner": "./dist/node/module-runner.js", "./dist/client/*": "./dist/client/*", "./types/*": { "types": "./types/*" @@ -50,9 +42,16 @@ ] } }, + "imports": { + "#module-sync-enabled": { + "module-sync": "./misc/true.js", + "default": "./misc/false.js" + } + }, "files": [ "bin", "dist", + "misc/**/*.js", "client.d.ts", "index.cjs", "index.d.cts", diff --git a/packages/vite/rollup.config.ts b/packages/vite/rollup.config.ts index 5727cb537a046c..bec0eabdd65d38 100644 --- a/packages/vite/rollup.config.ts +++ b/packages/vite/rollup.config.ts @@ -112,6 +112,7 @@ const nodeConfig = defineConfig({ 'fsevents', 'rollup/parseAst', /^tsx\//, + /^#/, ...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies), ], diff --git a/packages/vite/rollup.dts.config.ts b/packages/vite/rollup.dts.config.ts index fd09273f780e8f..e622053ba156ba 100644 --- a/packages/vite/rollup.dts.config.ts +++ b/packages/vite/rollup.dts.config.ts @@ -24,8 +24,8 @@ const external = [ export default defineConfig({ input: { - index: './temp/node/index.d.ts', - 'module-runner': './temp/module-runner/index.d.ts', + index: './temp/src/node/index.d.ts', + 'module-runner': './temp/src/module-runner/index.d.ts', }, output: { dir: './dist/node', diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index 946e013bab82aa..a6f0bd5a3ff13d 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -1703,6 +1703,9 @@ async function bundleConfigFile( fileName: string, isESM: boolean, ): Promise<{ code: string; dependencies: string[] }> { + const isModuleSyncConditionEnabled = (await import('#module-sync-enabled')) + .default + const dirnameVarName = '__vite_injected_original_dirname' const filenameVarName = '__vite_injected_original_filename' const importMetaUrlVarName = '__vite_injected_original_import_meta_url' @@ -1741,7 +1744,10 @@ async function bundleConfigFile( preferRelative: false, tryIndex: true, mainFields: [], - conditions: ['node'], + conditions: [ + 'node', + ...(isModuleSyncConditionEnabled ? ['module-sync'] : []), + ], externalConditions: [], external: [], noExternal: [], diff --git a/packages/vite/tsconfig.base.json b/packages/vite/tsconfig.base.json index 15fcd2c74e2a9e..9f6629981f2921 100644 --- a/packages/vite/tsconfig.base.json +++ b/packages/vite/tsconfig.base.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "rootDir": ".", "target": "ES2022", "module": "ESNext", "moduleResolution": "Bundler",