Skip to content

Commit f6b58a0

Browse files
committed
feat: default vendor chunk splitting
1 parent d4909b9 commit f6b58a0

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

jest.config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module.exports = {
22
preset: 'ts-jest',
3-
testMatch: ['**/*.spec.[jt]s?(x)'],
3+
testMatch: process.env.VITE_TEST_BUILD
4+
? ['**/playground/**/*.spec.[jt]s?(x)']
5+
: ['**/*.spec.[jt]s?(x)'],
46
testTimeout: process.env.CI ? 30000 : 10000,
57
globalSetup: './scripts/jestGlobalSetup.js',
68
globalTeardown: './scripts/jestGlobalTeardown.js',

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test": "run-s test-serve test-build",
1111
"test-serve": "jest",
1212
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest",
13-
"test-build": "cross-env VITE_TEST_BUILD=1 jest playground",
13+
"test-build": "cross-env VITE_TEST_BUILD=1 jest",
1414
"debug-build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 node --inspect-brk ./node_modules/.bin/jest",
1515
"docs": "vitepress dev docs",
1616
"build-docs": "vitepress build docs",

packages/plugin-legacy/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ async function buildPolyfillChunk(
418418
[name]: polyfillId
419419
},
420420
output: {
421-
format: name.includes('legacy') ? 'iife' : 'es'
421+
format: name.includes('legacy') ? 'iife' : 'es',
422+
manualChunks: undefined
422423
}
423424
}
424425
}

packages/vite/src/node/build.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import Rollup, {
1010
WarningHandler,
1111
OutputOptions,
1212
RollupOutput,
13-
ExternalOption
13+
ExternalOption,
14+
GetManualChunk,
15+
GetModuleInfo
1416
} from 'rollup'
1517
import { buildReporterPlugin } from './plugins/reporter'
1618
import { buildDefinePlugin } from './plugins/define'
@@ -338,8 +340,8 @@ async function doBuild(
338340
const generate = (output: OutputOptions = {}) => {
339341
return bundle[options.write ? 'write' : 'generate']({
340342
dir: outDir,
341-
format: options.ssr ? 'cjs' : 'es',
342-
exports: options.ssr ? 'named' : 'auto',
343+
format: ssr ? 'cjs' : 'es',
344+
exports: ssr ? 'named' : 'auto',
343345
sourcemap: options.sourcemap,
344346
name: libOptions ? libOptions.name : undefined,
345347
entryFileNames: ssr
@@ -359,6 +361,13 @@ async function doBuild(
359361
// #1048 add `Symbol.toStringTag` for module default export
360362
namespaceToStringTag: true,
361363
inlineDynamicImports: ssr && typeof input === 'string',
364+
manualChunks:
365+
!ssr &&
366+
!libOptions &&
367+
output?.format !== 'umd' &&
368+
output?.format !== 'iife'
369+
? moveToVendorChunk
370+
: undefined,
362371
...output
363372
})
364373
}
@@ -418,6 +427,33 @@ async function doBuild(
418427
}
419428
}
420429

430+
const moveToVendorChunk: GetManualChunk = (id, { getModuleInfo }) => {
431+
if (id.includes('node_modules') && !hasDynamicImporter(id, getModuleInfo)) {
432+
return 'vendor'
433+
}
434+
}
435+
436+
function hasDynamicImporter(
437+
id: string,
438+
getModuleInfo: GetModuleInfo,
439+
importStack: string[] = []
440+
): boolean {
441+
if (importStack.includes(id)) {
442+
// circular deps!
443+
return false
444+
}
445+
const mod = getModuleInfo(id)
446+
if (!mod) {
447+
return false
448+
}
449+
if (mod.dynamicImporters.length) {
450+
return true
451+
}
452+
return mod.importers.some((importer) =>
453+
hasDynamicImporter(importer, getModuleInfo, importStack.concat(id))
454+
)
455+
}
456+
421457
function resolveBuildOutputs(
422458
outputs: OutputOptions | OutputOptions[] | undefined,
423459
libOptions: LibraryOptions | false,

0 commit comments

Comments
 (0)