Skip to content

Commit

Permalink
feat: chunk split for dynamic import dep
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Dec 30, 2021
1 parent 0ebeb98 commit 0431163
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 9 deletions.
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'
3 changes: 3 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,9 @@ const fs = require('fs')
const path = require('path')

module.exports = {
resolve: {
preserveSymlinks: true
},
plugins: [
{
name: 'copy',
Expand Down
45 changes: 39 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 = getDynamicImportEntry(id, getModuleInfo, [])
if (entryName) {
return `async-vendor-${entryName}`
}
}
}
}
}
Expand Down Expand Up @@ -693,6 +696,36 @@ function staticImportedByEntry(
return someImporterIs
}

function getDynamicImportEntry(
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'))
) {
return path.basename(id, path.extname(id))
}

let entry
for (const importer of mod.importers) {
entry = getDynamicImportEntry(
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.

0 comments on commit 0431163

Please sign in to comment.