Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support module-sync condition when loading config if enabled #18650

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/vite/misc/false.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const result: boolean
export default result
1 change: 1 addition & 0 deletions packages/vite/misc/false.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default false
2 changes: 2 additions & 0 deletions packages/vite/misc/true.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const result: boolean
export default result
1 change: 1 addition & 0 deletions packages/vite/misc/true.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default true
19 changes: 10 additions & 9 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,14 @@
"types": "./dist/node/index.d.ts",
"exports": {
".": {
"import": {
"types": "./dist/node/index.d.ts",
"default": "./dist/node/index.js"
Comment on lines -24 to -25
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypeScript can infer the position in these kind of cases.

},
"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"
},
"./dist/client/*": "./dist/client/*",
Expand All @@ -50,9 +44,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",
Expand Down
1 change: 1 addition & 0 deletions packages/vite/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const nodeConfig = defineConfig({
/^vite\//,
'rollup/parseAst',
/^tsx\//,
/^#/,
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
],
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,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'
Expand Down Expand Up @@ -1735,7 +1738,10 @@ async function bundleConfigFile(
preferRelative: false,
tryIndex: true,
mainFields: [],
conditions: ['node'],
conditions: [
'node',
...(isModuleSyncConditionEnabled ? ['module-sync'] : []),
],
externalConditions: [],
external: [],
noExternal: [],
Expand Down
1 change: 1 addition & 0 deletions packages/vite/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"rootDir": ".",
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
Expand Down