Skip to content

Commit cdd9e21

Browse files
committed
fix: optimize-deps-dev
1 parent ca9df7e commit cdd9e21

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

packages/vite/src/node/optimizer/index.ts

+17-13
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ import {
2424
tryStatSync,
2525
unique,
2626
} from '../utils'
27-
import {
28-
transformWithEsbuild,
29-
} from '../plugins/esbuild'
27+
import { transformWithEsbuild } from '../plugins/esbuild'
3028
import { ESBUILD_MODULES_TARGET, METADATA_FILENAME } from '../constants'
3129
import { isWindows } from '../../shared/utils'
32-
import { rolldownCjsExternalPlugin, rolldownDepPlugin } from './rolldownDepPlugin'
30+
import {
31+
rolldownCjsExternalPlugin,
32+
rolldownDepPlugin,
33+
} from './rolldownDepPlugin'
3334
import { scanImports } from './scan'
3435
import { createOptimizeDepsIncludeResolver, expandGlobIds } from './resolve'
3536
export {
@@ -617,8 +618,7 @@ export function runOptimizeDeps(
617618
if (chunk.isEntry) {
618619
// One chunk maybe corresponding multiply entry
619620
const deps = Object.values(depsInfo).filter(
620-
(d) =>
621-
d.src === chunk.facadeModuleId!,
621+
(d) => d.src === chunk.facadeModuleId!,
622622
)
623623
for (const { exportsData, file, id, ...info } of deps) {
624624
addOptimizedDepInfo(metadata, 'optimized', {
@@ -803,14 +803,15 @@ async function prepareRolldownOptimizerRun(
803803

804804
async function build() {
805805
const bundle = await rolldown.rolldown({
806-
input: Object.keys(flatIdDeps),
806+
// TODO using Array input generate name is not expected, the entry name will be like index~1.js
807+
input: flatIdDeps,
807808
// external,
808809
logLevel: 'warn',
809810
plugins,
810811
resolve: {
811812
mainFields: ['module', 'main'],
812813
aliasFields: [['browser']],
813-
extensions: ['.js', '.css']
814+
extensions: ['.js', '.css'],
814815
},
815816
...rollupOptions,
816817
})
@@ -1037,13 +1038,16 @@ function stringifyDepsOptimizerMetadata(
10371038
file,
10381039
fileHash,
10391040
needsInterop,
1040-
isDynamicEntry
1041+
isDynamicEntry,
10411042
},
10421043
],
10431044
),
10441045
),
10451046
chunks: Object.fromEntries(
1046-
Object.values(chunks).map(({ id, file, isDynamicEntry }) => [id, { file, isDynamicEntry }]),
1047+
Object.values(chunks).map(({ id, file, isDynamicEntry }) => [
1048+
id,
1049+
{ file, isDynamicEntry },
1050+
]),
10471051
),
10481052
},
10491053
(key: string, value: string) => {
@@ -1075,11 +1079,11 @@ export async function extractExportsData(
10751079
const rolldownBuild = await rolldown.rolldown({
10761080
...rollupOptions,
10771081
input: [filePath],
1078-
});
1082+
})
10791083
const result = await rolldownBuild.generate({
10801084
...rollupOptions.output,
10811085
format: 'esm',
1082-
});
1086+
})
10831087
const [, exports, , hasModuleSyntax] = parse(result.output[0].code)
10841088
return {
10851089
hasModuleSyntax,
@@ -1350,4 +1354,4 @@ const safeRename = promisify(function gracefulRename(
13501354
}
13511355
if (cb) cb(er)
13521356
})
1353-
})
1357+
})

packages/vite/src/node/plugins/resolve.ts

+34-18
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
133133
const {
134134
root,
135135
isProduction,
136+
isBuild,
136137
asSrc,
137138
ssrConfig,
138139
preferRelative = false,
@@ -448,27 +449,42 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
448449

449450
load(id) {
450451
if (id.startsWith(browserExternalId)) {
451-
if (isProduction) {
452-
// return `export default {}`
453-
// The rolldown missing export is always error level, it will break build.
454-
// So here using the cjs module to avoid it.
455-
return `module.exports = {}`
452+
if (isBuild) {
453+
if (isProduction) {
454+
// return `export default {}`
455+
// The rolldown missing export is always error level, it will break build.
456+
// So here using the cjs module to avoid it.
457+
return `module.exports = {}`
458+
} else {
459+
id = id.slice(browserExternalId.length + 1)
460+
// The rolldown using esbuild interop helper, so here copy the proxy module from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L259-------
461+
return `\
462+
module.exports = Object.create(new Proxy({}, {
463+
get(_, key) {
464+
if (
465+
key !== '__esModule' &&
466+
key !== '__proto__' &&
467+
key !== 'constructor' &&
468+
key !== 'splice'
469+
) {
470+
throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
471+
}
472+
}
473+
}))`
474+
}
456475
} else {
457-
id = id.slice(browserExternalId.length + 1)
458-
// The rolldown using esbuild interop helper, so here copy the proxy module from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L259-------
459-
return `\
460-
module.exports = Object.create(new Proxy({}, {
461-
get(_, key) {
462-
if (
463-
key !== '__esModule' &&
464-
key !== '__proto__' &&
465-
key !== 'constructor' &&
466-
key !== 'splice'
467-
) {
476+
// The dev need to return esm module.
477+
if (isProduction) {
478+
return `export default {}`
479+
} else {
480+
id = id.slice(browserExternalId.length + 1)
481+
return `\
482+
export default new Proxy({}, {
483+
get(_, key) {
468484
throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
469485
}
470-
}
471-
}))`
486+
})`
487+
}
472488
}
473489
}
474490
if (id.startsWith(optionalPeerDepId)) {

0 commit comments

Comments
 (0)