Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose vite's default manualChunks function #5585

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/playground/vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import { defineConfig } from 'vite'
import vuePlugin from '@vitejs/plugin-vue'
import { vueI18nPlugin } from './CustomBlockPlugin'
import { moveToVendorChunkFn } from 'vite'

export default defineConfig({
resolve: {
Expand All @@ -17,7 +18,18 @@ export default defineConfig({
],
build: {
// to make tests faster
minify: false
minify: false,
rollupOptions: {
output: {
manualChunks: (id, api) => {
if (id.includes('node_modules') && id.includes('vue')) {
return 'vue-chunk'
}
// call vite's default manualChunks
return moveToVendorChunkFn(id, api)
}
}
}
},
css: {
modules: {
Expand Down
40 changes: 21 additions & 19 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ let parallelCallCounts = 0
// bundle is even pushed.
const parallelBuilds: RollupBuild[] = []

// cache indicating whether processed chunks are imported by entry
const cacheVendorChunks = new Map<string, boolean>()

/**
* Bundles the app for production.
* Returns a Promise containing the build result.
Expand Down Expand Up @@ -477,7 +480,7 @@ async function doBuild(
!libOptions &&
output?.format !== 'umd' &&
output?.format !== 'iife'
? createMoveToVendorChunkFn(config)
? moveToVendorChunkFn
: undefined,
...output
}
Expand Down Expand Up @@ -525,6 +528,8 @@ async function doBuild(
watcher.on('event', (event) => {
if (event.code === 'BUNDLE_START') {
config.logger.info(chalk.cyanBright(`\nbuild started...`))
// clear cache for build watch
cacheVendorChunks.clear()
if (options.write) {
prepareOutDir(outDir, options.emptyOutDir, config)
}
Expand Down Expand Up @@ -556,6 +561,8 @@ async function doBuild(
prepareOutDir(outDir, options.emptyOutDir, config)
}

// clear cache for parallel builds
cacheVendorChunks.clear()
Comment on lines +564 to +565
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please focus on checking whether it needs to be cleared here?
Will the parallelCallCounts introduced by 4a955bc actually be greater than 1? I tested several scenarios that I expected and found nothing. If it is greater than, maybe parallel build will affect the shared cache?

if (Array.isArray(outputs)) {
const res = []
for (const output of outputs) {
Expand Down Expand Up @@ -605,52 +612,47 @@ function getPkgName(root: string) {
return name?.startsWith('@') ? name.split('/')[1] : name
}

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'
}
export const moveToVendorChunkFn: GetManualChunk = (id, { getModuleInfo }) => {
if (
id.includes('node_modules') &&
!isCSSRequest(id) &&
staticImportedByEntry(id, getModuleInfo)
) {
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 (cacheVendorChunks.has(id)) {
return cacheVendorChunks.get(id) as boolean
}
if (importStack.includes(id)) {
// circular deps!
cache.set(id, false)
cacheVendorChunks.set(id, false)
return false
}
const mod = getModuleInfo(id)
if (!mod) {
cache.set(id, false)
cacheVendorChunks.set(id, false)
return false
}

if (mod.isEntry) {
cache.set(id, true)
cacheVendorChunks.set(id, true)
return true
}
const someImporterIs = mod.importers.some((importer) =>
staticImportedByEntry(
importer,
getModuleInfo,
cache,
importStack.concat(id)
)
)
cache.set(id, someImporterIs)
cacheVendorChunks.set(id, someImporterIs)
return someImporterIs
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './config'
export { createServer, searchForWorkspaceRoot } from './server'
export { preview } from './preview'
export { build } from './build'
export { build, moveToVendorChunkFn } from './build'
export { optimizeDeps } from './optimizer'
export { send } from './server/send'
export { createLogger, printHttpServerUrls } from './logger'
Expand Down