Skip to content

Commit

Permalink
feat: support user manualChunks composition
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 1, 2022
1 parent a4d75bc commit 23440e9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
12 changes: 11 additions & 1 deletion packages/playground/vue/vite.config.ts
Expand Up @@ -17,7 +17,17 @@ export default defineConfig({
],
build: {
// to make tests faster
minify: false
minify: false,
rollupOptions: {
output: {
// Test splitVendorChunkPlugin composition
manualChunks(id) {
if (id.includes('src-import')) {
return 'src-import'
}
}
}
}
},
css: {
modules: {
Expand Down
27 changes: 24 additions & 3 deletions packages/vite/src/node/plugins/splitVendorChunk.ts
@@ -1,6 +1,11 @@
import type { UserConfig } from '../../node'
import type { Plugin } from '../plugin'
import type { OutputOptions, GetManualChunk, GetModuleInfo } from 'rollup'
import type {
OutputOptions,
GetManualChunk,
GetManualChunkApi,
GetModuleInfo
} from 'rollup'
import { isCSSRequest } from './css'

// Use splitVendorChunkPlugin() to get the same manualChunks strategy as Vite 2.7
Expand Down Expand Up @@ -91,13 +96,29 @@ export function splitVendorChunkPlugin(): Plugin {
if (outputs) {
outputs = Array.isArray(outputs) ? outputs : [outputs]
for (const output of outputs) {
output.manualChunks = createSplitVendorChunk(output, config)
const viteManualChunks = createSplitVendorChunk(output, config)
if (viteManualChunks) {
if (output.manualChunks) {
if (typeof output.manualChunks === 'function') {
const userManualChunks = output.manualChunks
output.manualChunks = (id: string, api: GetManualChunkApi) => {
return userManualChunks(id, api) ?? viteManualChunks(id, api)
}
}
// else, leave the object form of manualChunks untouched, as
// we can't safely replicate rollup handling.
} else {
output.manualChunks = viteManualChunks
}
}
}
} else {
return {
build: {
rollupOptions: {
manualChunks: createSplitVendorChunk({}, config)
output: {
manualChunks: createSplitVendorChunk({}, config)
}
}
}
}
Expand Down

0 comments on commit 23440e9

Please sign in to comment.