From dfb52dbb92ee016f5745709f6529d0b08ce219b9 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Sun, 25 Sep 2022 16:50:39 +0800 Subject: [PATCH 1/2] feat(bundler-vite): improve chunk split --- .../bundler-vite/src/plugins/mainPlugin.ts | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 4246a59c15..565b48b890 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -6,6 +6,44 @@ import type { AcceptedPlugin } from 'postcss' import postcssrc from 'postcss-load-config' import type { AliasOptions, Connect, Plugin, UserConfig } from 'vite' +const cache = new Map() + +const staticImportedByEntry = ( + id: string, + getModuleInfo: any, + cache: Map, + importStack: string[] = [] +): boolean => { + if (cache.has(id)) { + return !!cache.get(id) + } + 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: string) => + staticImportedByEntry( + importer, + getModuleInfo, + cache, + importStack.concat(id) + ) + ) + cache.set(id, someImporterIs) + return someImporterIs +} + /** * The main plugin to compat vuepress with vite */ @@ -96,7 +134,30 @@ import '@vuepress/client/app' // also add hash to ssr entry file, so that users could build multiple sites in a single process entryFileNames: `[name].[hash].mjs`, } - : {}), + : { + manualChunks(id, ctx) { + // move known framework code into a stable chunk so that + // custom theme changes do not invalidate hash for all pages + if ( + id.includes('plugin-vue:export-helper') || + /@vue\/(runtime|shared|reactivity)/.test(id) || + /@vuepress\/(client|shared)/.test(id) + ) { + return 'framework' + } + + // Check if a module is statically imported by at least one entry. + if ( + id.includes('node_modules') && + !/\.css($|\\?)/.test(id) && + staticImportedByEntry(id, ctx.getModuleInfo, cache) + ) { + return 'vendor' + } + + return undefined + }, + }), }, preserveEntrySignatures: 'allow-extension', }, From 585e5940ef56770d5c21daf7de5e5bd864eae36f Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Sat, 22 Oct 2022 00:07:01 +0800 Subject: [PATCH 2/2] chore: tweaks --- .../bundler-vite/src/plugins/mainPlugin.ts | 73 +++++++++---------- 1 file changed, 33 insertions(+), 40 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 565b48b890..53ed615c26 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -4,46 +4,9 @@ import autoprefixer from 'autoprefixer' import history from 'connect-history-api-fallback' import type { AcceptedPlugin } from 'postcss' import postcssrc from 'postcss-load-config' +import type { GetModuleInfo } from 'rollup' import type { AliasOptions, Connect, Plugin, UserConfig } from 'vite' -const cache = new Map() - -const staticImportedByEntry = ( - id: string, - getModuleInfo: any, - cache: Map, - importStack: string[] = [] -): boolean => { - if (cache.has(id)) { - return !!cache.get(id) - } - 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: string) => - staticImportedByEntry( - importer, - getModuleInfo, - cache, - importStack.concat(id) - ) - ) - cache.set(id, someImporterIs) - return someImporterIs -} - /** * The main plugin to compat vuepress with vite */ @@ -146,11 +109,11 @@ import '@vuepress/client/app' return 'framework' } - // Check if a module is statically imported by at least one entry. + // check if a module is statically imported by at least one entry. if ( id.includes('node_modules') && !/\.css($|\\?)/.test(id) && - staticImportedByEntry(id, ctx.getModuleInfo, cache) + isStaticallyImportedByEntry(id, ctx.getModuleInfo) ) { return 'vendor' } @@ -284,3 +247,33 @@ const resolveDefine = async ({ return define } + +const cache = new Map() +const isStaticallyImportedByEntry = ( + id: string, + getModuleInfo: GetModuleInfo, + importStack: string[] = [] +): boolean => { + if (cache.has(id)) { + return !!cache.get(id) + } + 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((item) => + isStaticallyImportedByEntry(item, getModuleInfo, importStack.concat(id)) + ) + cache.set(id, someImporterIs) + return someImporterIs +}