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

fix: chunk split for dynamic import dep #6318

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions packages/playground/dynamic-import/dep-a/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default () => '[email protected]'
5 changes: 5 additions & 0 deletions packages/playground/dynamic-import/dep-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "dep-a",
"version": "2.0.0",
"main": "index.js"
}
3 changes: 3 additions & 0 deletions packages/playground/dynamic-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"dep-a": "file:./dep-a"
}
}
3 changes: 3 additions & 0 deletions packages/playground/dynamic-import/views/bar.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import getName from 'dep-a'
console.log('bar' + getName())

export const msg = 'Bar view'
6 changes: 6 additions & 0 deletions packages/playground/dynamic-import/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const fs = require('fs')
const path = require('path')

module.exports = {
resolve: {
preserveSymlinks: true
},
optimizeDeps: {
exclude: ['dep-a']
},
plugins: [
{
name: 'copy',
Expand Down
54 changes: 48 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,15 @@ function getPkgName(root: string) {
function createMoveToVendorChunkFn(config: ResolvedConfig): GetManualChunk {
const cache = new Map<string, boolean>()
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}`
}
}
}
}
}
Expand Down Expand Up @@ -693,6 +696,45 @@ function staticImportedByEntry(
return someImporterIs
}

function resolveDynamicImportEntry(
id: string,
getModuleInfo: GetModuleInfo,
importStack: string[] = []
): string | void {
const mod = getModuleInfo(id)
if (!mod || importStack.includes(id)) {
return
}

if (
mod.dynamicImporters.length > 0 &&
mod.dynamicImporters.some((importer) => !importer.includes('node_modules'))
) {
if (id.includes('node_modules')) {
const dirs = id.split(path.sep)
const nodeModulesIndex = dirs.lastIndexOf('node_modules')
let packageName = dirs[nodeModulesIndex + 1]
if (packageName.startsWith('@')) {
packageName = path.join(packageName, dirs[nodeModulesIndex + 2])
}
return packageName
}
return path.basename(id, path.extname(id))
}

let entry
sanyuan0704 marked this conversation as resolved.
Show resolved Hide resolved
for (const importer of mod.importers) {
entry = resolveDynamicImportEntry(
importer,
getModuleInfo,
importStack.concat(id)
)
if (entry) {
return entry
}
}
}

export function resolveLibFilename(
libOptions: LibraryOptions,
format: ModuleFormat,
Expand Down
18 changes: 15 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.