Skip to content

Commit

Permalink
feat: default vendor chunk splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 24, 2021
1 parent d4909b9 commit f6b58a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = {
preset: 'ts-jest',
testMatch: ['**/*.spec.[jt]s?(x)'],
testMatch: process.env.VITE_TEST_BUILD
? ['**/playground/**/*.spec.[jt]s?(x)']
: ['**/*.spec.[jt]s?(x)'],
testTimeout: process.env.CI ? 30000 : 10000,
globalSetup: './scripts/jestGlobalSetup.js',
globalTeardown: './scripts/jestGlobalTeardown.js',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "run-s test-serve test-build",
"test-serve": "jest",
"debug-serve": "cross-env VITE_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest",
"test-build": "cross-env VITE_TEST_BUILD=1 jest playground",
"test-build": "cross-env VITE_TEST_BUILD=1 jest",
"debug-build": "cross-env VITE_TEST_BUILD=1 VITE_PRESERVE_BUILD_ARTIFACTS=1 node --inspect-brk ./node_modules/.bin/jest",
"docs": "vitepress dev docs",
"build-docs": "vitepress build docs",
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-legacy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,8 @@ async function buildPolyfillChunk(
[name]: polyfillId
},
output: {
format: name.includes('legacy') ? 'iife' : 'es'
format: name.includes('legacy') ? 'iife' : 'es',
manualChunks: undefined
}
}
}
Expand Down
42 changes: 39 additions & 3 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import Rollup, {
WarningHandler,
OutputOptions,
RollupOutput,
ExternalOption
ExternalOption,
GetManualChunk,
GetModuleInfo
} from 'rollup'
import { buildReporterPlugin } from './plugins/reporter'
import { buildDefinePlugin } from './plugins/define'
Expand Down Expand Up @@ -338,8 +340,8 @@ async function doBuild(
const generate = (output: OutputOptions = {}) => {
return bundle[options.write ? 'write' : 'generate']({
dir: outDir,
format: options.ssr ? 'cjs' : 'es',
exports: options.ssr ? 'named' : 'auto',
format: ssr ? 'cjs' : 'es',
exports: ssr ? 'named' : 'auto',
sourcemap: options.sourcemap,
name: libOptions ? libOptions.name : undefined,
entryFileNames: ssr
Expand All @@ -359,6 +361,13 @@ async function doBuild(
// #1048 add `Symbol.toStringTag` for module default export
namespaceToStringTag: true,
inlineDynamicImports: ssr && typeof input === 'string',
manualChunks:
!ssr &&
!libOptions &&
output?.format !== 'umd' &&
output?.format !== 'iife'
? moveToVendorChunk
: undefined,
...output
})
}
Expand Down Expand Up @@ -418,6 +427,33 @@ async function doBuild(
}
}

const moveToVendorChunk: GetManualChunk = (id, { getModuleInfo }) => {
if (id.includes('node_modules') && !hasDynamicImporter(id, getModuleInfo)) {
return 'vendor'
}
}

function hasDynamicImporter(
id: string,
getModuleInfo: GetModuleInfo,
importStack: string[] = []
): boolean {
if (importStack.includes(id)) {
// circular deps!
return false
}
const mod = getModuleInfo(id)
if (!mod) {
return false
}
if (mod.dynamicImporters.length) {
return true
}
return mod.importers.some((importer) =>
hasDynamicImporter(importer, getModuleInfo, importStack.concat(id))
)
}

function resolveBuildOutputs(
outputs: OutputOptions | OutputOptions[] | undefined,
libOptions: LibraryOptions | false,
Expand Down

0 comments on commit f6b58a0

Please sign in to comment.