diff --git a/packages/playground/dynamic-import/dep-a/index.js b/packages/playground/dynamic-import/dep-a/index.js new file mode 100644 index 00000000000000..fab47e37a534f2 --- /dev/null +++ b/packages/playground/dynamic-import/dep-a/index.js @@ -0,0 +1 @@ +export default () => 'A@2.0.0' diff --git a/packages/playground/dynamic-import/dep-a/package.json b/packages/playground/dynamic-import/dep-a/package.json new file mode 100644 index 00000000000000..666908afc6886c --- /dev/null +++ b/packages/playground/dynamic-import/dep-a/package.json @@ -0,0 +1,5 @@ +{ + "name": "dep-a", + "version": "2.0.0", + "main": "index.js" +} diff --git a/packages/playground/dynamic-import/package.json b/packages/playground/dynamic-import/package.json index a6b6d5f863f1b8..22016387c31faa 100644 --- a/packages/playground/dynamic-import/package.json +++ b/packages/playground/dynamic-import/package.json @@ -7,5 +7,8 @@ "build": "vite build", "debug": "node --inspect-brk ../../vite/bin/vite", "preview": "vite preview" + }, + "dependencies": { + "dep-a": "file:./dep-a" } } diff --git a/packages/playground/dynamic-import/views/bar.js b/packages/playground/dynamic-import/views/bar.js index 94401802f2dd50..04140a7826814e 100644 --- a/packages/playground/dynamic-import/views/bar.js +++ b/packages/playground/dynamic-import/views/bar.js @@ -1 +1,4 @@ +import getName from 'dep-a' +console.log('bar' + getName()) + export const msg = 'Bar view' diff --git a/packages/playground/dynamic-import/vite.config.js b/packages/playground/dynamic-import/vite.config.js index 010e47d6308d30..c88ccc30c228e7 100644 --- a/packages/playground/dynamic-import/vite.config.js +++ b/packages/playground/dynamic-import/vite.config.js @@ -2,6 +2,12 @@ const fs = require('fs') const path = require('path') module.exports = { + resolve: { + preserveSymlinks: true + }, + optimizeDeps: { + exclude: ['dep-a'] + }, plugins: [ { name: 'copy', diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 4b247c0151c881..24344cda18ce85 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -647,12 +647,15 @@ function getPkgName(root: string) { function createMoveToVendorChunkFn(config: ResolvedConfig): GetManualChunk { const cache = new Map() return (id, { getModuleInfo }) => { - if ( - id.includes('node_modules') && - !isCSSRequest(id) && - staticImportedByEntry(id, getModuleInfo, cache) - ) { - return 'vendor' + if (id.includes('node_modules') && !isCSSRequest(id)) { + if (staticImportedByEntry(id, getModuleInfo, cache)) { + return 'vendor' + } else { + const entryName = resolveDynamicImportEntry(id, getModuleInfo, []) + if (entryName) { + return `async-vendor-${entryName}` + } + } } } } @@ -693,6 +696,48 @@ function staticImportedByEntry( return someImporterIs } +function resolveDynamicImportEntry( + id: string, + getModuleInfo: GetModuleInfo, + importStack: string[] = [] +): string | undefined { + const mod = getModuleInfo(id) + if (!mod || importStack.includes(id)) { + return + } + + if ( + mod.dynamicImporters.length > 0 && + mod.dynamicImporters.some((importer) => !importer.includes('node_modules')) + ) { + const dirs = id.split(path.sep) + if (id.includes('node_modules')) { + const nodeModulesIndex = dirs.lastIndexOf('node_modules') + let packageName = dirs[nodeModulesIndex + 1] + if (packageName.startsWith('@')) { + packageName = path.join(packageName, dirs[nodeModulesIndex + 2]) + } + return packageName + } + let [parentDir, basename] = dirs.slice(-2) + basename = basename.replace(path.extname(basename), '') + return `${parentDir}-${basename}` + } + + let entry: string | undefined + for (const importer of mod.importers) { + entry = resolveDynamicImportEntry( + importer, + getModuleInfo, + importStack.concat(id) + ) + if (entry) { + return entry + } + } + return +} + export function resolveLibFilename( libOptions: LibraryOptions, format: ModuleFormat, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bb552d8aabaddb..5b417a8568668e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -164,6 +164,12 @@ importers: specifiers: {} packages/playground/dynamic-import: + specifiers: + dep-a: file:./dep-a + dependencies: + dep-a: link:dep-a + + packages/playground/dynamic-import/dep-a: specifiers: {} packages/playground/env: @@ -713,6 +719,12 @@ importers: '@vue/babel-plugin-jsx': 1.1.1_@babel+core@7.16.5 hash-sum: 2.0.0 + packages/temp: + specifiers: + css-color-names: ^1.0.1 + devDependencies: + css-color-names: 1.0.1 + packages/vite: specifiers: '@ampproject/remapping': ^1.0.2 @@ -4616,7 +4628,7 @@ packages: resolution: {integrity: sha1-FQStJSMVjKpA20onh8sBQRmU6k8=} /fsevents/2.3.2: - resolution: {integrity: sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==} + resolution: {integrity: sha1-ilJveLj99GI7cJ4Ll1xSwkwC/Ro=} engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0} os: [darwin] requiresBuild: true @@ -6212,7 +6224,7 @@ packages: sourcemap-codec: 1.4.8 /make-dir/2.1.0: - resolution: {integrity: sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==} + resolution: {integrity: sha1-XwMQ4YuL6JjMBwCSlaMK5B6R5vU=} engines: {node: '>=6'} requiresBuild: true dependencies: @@ -7319,7 +7331,7 @@ packages: dev: true /pvutils/1.0.17: - resolution: {integrity: sha512-wLHYUQxWaXVQvKnwIDWFVKDJku9XDCvyhhxoq8dc5MFdIlRenyPI9eSfEtcvgHgD7FlvCyGAlWgOzRnZD99GZQ==} + resolution: {integrity: sha1-rePHTf5xeJRP5EgGYmvS4knZlr8=} engines: {node: '>=6.0.0'} dev: true