From dfb52dbb92ee016f5745709f6529d0b08ce219b9 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Sun, 25 Sep 2022 16:50:39 +0800 Subject: [PATCH 01/10] 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 02/10] 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 +} From 54a221d9470b22249597c7e32123ffd70c468000 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Wed, 23 Nov 2022 13:52:44 +0800 Subject: [PATCH 03/10] Update mainPlugin.ts --- .../bundler-vite/src/plugins/mainPlugin.ts | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 53ed615c26..d301402d6c 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -7,6 +7,36 @@ import postcssrc from 'postcss-load-config' import type { GetModuleInfo } from 'rollup' import type { AliasOptions, Connect, Plugin, UserConfig } from 'vite' +const isStaticallyImportedByEntry = ( + cache: Map, + 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 +} + /** * The main plugin to compat vuepress with vite */ @@ -22,6 +52,9 @@ export const mainPlugin = ({ name: 'vuepress:main', config: async () => { + + const cache = new Map() + // create a temp index.html as dev entry point if (!isBuild) { await app.writeTemp( @@ -113,7 +146,7 @@ import '@vuepress/client/app' if ( id.includes('node_modules') && !/\.css($|\\?)/.test(id) && - isStaticallyImportedByEntry(id, ctx.getModuleInfo) + isStaticallyImportedByEntry(cache, id, ctx.getModuleInfo) ) { return 'vendor' } @@ -247,33 +280,3 @@ 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 -} From 7ff644bc1cab78a2435b34c5b88d4597fe4a82c6 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Wed, 23 Nov 2022 13:53:08 +0800 Subject: [PATCH 04/10] Update mainPlugin.ts --- packages/bundler-vite/src/plugins/mainPlugin.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index d301402d6c..e2a4310310 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -52,9 +52,8 @@ export const mainPlugin = ({ name: 'vuepress:main', config: async () => { - const cache = new Map() - + // create a temp index.html as dev entry point if (!isBuild) { await app.writeTemp( From c1505255858e4cbb0f81d8c6bfd36ee8b3205972 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Mon, 5 Dec 2022 14:21:33 +0800 Subject: [PATCH 05/10] Update mainPlugin.ts --- .../bundler-vite/src/plugins/mainPlugin.ts | 50 ++----------------- 1 file changed, 5 insertions(+), 45 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index e2a4310310..dd8b26ea3e 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -4,39 +4,8 @@ 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 isStaticallyImportedByEntry = ( - cache: Map, - 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 -} - /** * The main plugin to compat vuepress with vite */ @@ -134,23 +103,14 @@ import '@vuepress/client/app' // 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) && - isStaticallyImportedByEntry(cache, id, ctx.getModuleInfo) + id.includes("plugin-vue:export-helper") || + /node_modules\/@vuepress\/shared\//.test(id) || + /node_modules\/vue(-router)?\//.test(id) ) { - return 'vendor' + return "framework"; } - return undefined + return undefined; }, }), }, From a2440ae20e85633260baecb3f2cfd9a616ba5c8a Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Mon, 5 Dec 2022 14:22:14 +0800 Subject: [PATCH 06/10] Update mainPlugin.ts --- packages/bundler-vite/src/plugins/mainPlugin.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index dd8b26ea3e..5da32afd28 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -21,8 +21,6 @@ export const mainPlugin = ({ name: 'vuepress:main', config: async () => { - const cache = new Map() - // create a temp index.html as dev entry point if (!isBuild) { await app.writeTemp( From 012c605aab85a2163aeb0b364f842f8a55629562 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Mon, 5 Dec 2022 14:31:36 +0800 Subject: [PATCH 07/10] Update mainPlugin.ts --- packages/bundler-vite/src/plugins/mainPlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 5da32afd28..b5e49275be 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -101,11 +101,11 @@ import '@vuepress/client/app' // 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") || + id.includes('plugin-vue:export-helper') || /node_modules\/@vuepress\/shared\//.test(id) || /node_modules\/vue(-router)?\//.test(id) ) { - return "framework"; + return 'framework'; } return undefined; From dda09013398a26d67888da9cc8968d3180b95d44 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Wed, 7 Dec 2022 19:12:31 +0800 Subject: [PATCH 08/10] Update mainPlugin.ts --- packages/bundler-vite/src/plugins/mainPlugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index b5e49275be..322ac5dad8 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -105,10 +105,10 @@ import '@vuepress/client/app' /node_modules\/@vuepress\/shared\//.test(id) || /node_modules\/vue(-router)?\//.test(id) ) { - return 'framework'; + return 'framework' } - return undefined; + return undefined }, }), }, From 445c15ef9c2b3d97c0a4719effef7903fc226c7e Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Thu, 8 Dec 2022 20:00:46 +0800 Subject: [PATCH 09/10] chore: tweaks --- packages/bundler-vite/src/plugins/mainPlugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 322ac5dad8..292742b241 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -97,7 +97,7 @@ import '@vuepress/client/app' entryFileNames: `[name].[hash].mjs`, } : { - manualChunks(id, ctx) { + manualChunks(id) { // move known framework code into a stable chunk so that // custom theme changes do not invalidate hash for all pages if ( From b4a081df157ef40ebf95a6d5b05d3ecfc5237c45 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Thu, 8 Dec 2022 20:01:15 +0800 Subject: [PATCH 10/10] chore: update comments --- packages/bundler-vite/src/plugins/mainPlugin.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/bundler-vite/src/plugins/mainPlugin.ts b/packages/bundler-vite/src/plugins/mainPlugin.ts index 292742b241..bb64d53d5b 100644 --- a/packages/bundler-vite/src/plugins/mainPlugin.ts +++ b/packages/bundler-vite/src/plugins/mainPlugin.ts @@ -98,8 +98,7 @@ import '@vuepress/client/app' } : { manualChunks(id) { - // move known framework code into a stable chunk so that - // custom theme changes do not invalidate hash for all pages + // move known framework code into a stable chunk if ( id.includes('plugin-vue:export-helper') || /node_modules\/@vuepress\/shared\//.test(id) ||