-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
refactor: avoid splitting vendor chunk by default #6534
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d0fda58
refactor: avoid splitting vendor chunk by default
patak-dev 641e725
chore: fix typo
patak-dev 0a06f13
docs: add section to build guide
patak-dev 2306dad
fix: account for rollupOptions.output array
patak-dev 41421c4
chore: fix type
patak-dev 9e07062
chore: fix default value
patak-dev a4d75bc
chore: update
patak-dev 23440e9
feat: support user manualChunks composition
patak-dev 060c1a3
Merge branch 'main' into refactor/avoid-split-vendor-chunk
patak-dev df912c0
chore: format
patak-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import type { UserConfig } from '../../node' | ||
import type { Plugin } from '../plugin' | ||
import type { | ||
OutputOptions, | ||
GetManualChunk, | ||
GetManualChunkApi, | ||
GetModuleInfo | ||
} from 'rollup' | ||
import { isCSSRequest } from './css' | ||
|
||
// Use splitVendorChunkPlugin() to get the same manualChunks strategy as Vite 2.7 | ||
// We don't recommend using this strategy as a general solution moving forward | ||
|
||
// splitVendorChunk is a simple index/vendor strategy that was used in Vite | ||
// until v2.8. It is exposed to let people continue to use it in case it was | ||
// working well for their setups. | ||
// The cache needs to be reset on buildStart for watch mode to work correctly | ||
// Don't use this manualChunks strategy for ssr, lib mode, and 'umd' or 'iife' | ||
|
||
export class SplitVendorChunkCache { | ||
cache: Map<string, boolean> | ||
constructor() { | ||
this.cache = new Map<string, boolean>() | ||
} | ||
reset() { | ||
this.cache = new Map<string, boolean>() | ||
} | ||
} | ||
|
||
export function splitVendorChunk( | ||
options: { cache?: SplitVendorChunkCache } = {} | ||
): GetManualChunk { | ||
const cache = options.cache ?? new SplitVendorChunkCache() | ||
return (id, { getModuleInfo }) => { | ||
if ( | ||
id.includes('node_modules') && | ||
!isCSSRequest(id) && | ||
staticImportedByEntry(id, getModuleInfo, cache.cache) | ||
) { | ||
return 'vendor' | ||
} | ||
} | ||
} | ||
|
||
function staticImportedByEntry( | ||
id: string, | ||
getModuleInfo: GetModuleInfo, | ||
cache: Map<string, boolean>, | ||
importStack: string[] = [] | ||
): boolean { | ||
if (cache.has(id)) { | ||
return cache.get(id) as boolean | ||
} | ||
if (importStack.includes(id)) { | ||
// circular deps! | ||
cache.set(id, false) | ||
return false | ||
} | ||
const mod = getModuleInfo(id) | ||
if (!mod) { | ||
cache.set(id, false) | ||
return false | ||
} | ||
|
||
if (mod.isEntry) { | ||
cache.set(id, true) | ||
return true | ||
} | ||
const someImporterIs = mod.importers.some((importer) => | ||
staticImportedByEntry( | ||
importer, | ||
getModuleInfo, | ||
cache, | ||
importStack.concat(id) | ||
) | ||
) | ||
cache.set(id, someImporterIs) | ||
return someImporterIs | ||
} | ||
|
||
export function splitVendorChunkPlugin(): Plugin { | ||
const caches: SplitVendorChunkCache[] = [] | ||
function createSplitVendorChunk(output: OutputOptions, config: UserConfig) { | ||
const cache = new SplitVendorChunkCache() | ||
caches.push(cache) | ||
const build = config.build ?? {} | ||
const format = output?.format | ||
if (!build.ssr && !build.lib && format !== 'umd' && format !== 'iife') { | ||
return splitVendorChunk({ cache }) | ||
} | ||
} | ||
return { | ||
name: 'vite:split-vendor-chunk', | ||
config(config) { | ||
let outputs = config?.build?.rollupOptions?.output | ||
if (outputs) { | ||
outputs = Array.isArray(outputs) ? outputs : [outputs] | ||
for (const output of outputs) { | ||
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: { | ||
output: { | ||
manualChunks: createSplitVendorChunk({}, config) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
buildStart() { | ||
caches.forEach((cache) => cache.reset()) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class seems unnecessary. The
Map
object has aclear
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we will need to expand the cache later. I wanted to expose the cache for the method, not a particular Map that is an implementation detail and may change