@@ -73,13 +73,25 @@ export async function bundle(
73
73
...( ssr
74
74
? { }
75
75
: {
76
- chunkFileNames ( chunk ) : string {
77
- if ( ! chunk . isEntry && / r u n t i m e / . test ( chunk . name ) ) {
78
- return `assets/framework.[hash].js`
76
+ chunkFileNames ( chunk ) {
77
+ // avoid ads chunk being intercepted by adblock
78
+ return / (?: C a r b o n | B u y S e l l ) A d s / . test ( chunk . name )
79
+ ? `assets/chunks/ui-custom.[hash].js`
80
+ : `assets/chunks/[name].[hash].js`
81
+ } ,
82
+ manualChunks ( id , ctx ) {
83
+ // move known framework code into a stable chunk so that
84
+ // custom theme changes do not invalidate hash for all pages
85
+ if ( id . includes ( 'plugin-vue:export-helper' ) ) {
86
+ return 'framework'
87
+ }
88
+ if (
89
+ isEagerChunk ( id , ctx ) &&
90
+ ( / @ v u e \/ ( r u n t i m e | s h a r e d | r e a c t i v i t y ) / . test ( id ) ||
91
+ / v i t e p r e s s \/ d i s t \/ c l i e n t / . test ( id ) )
92
+ ) {
93
+ return 'framework'
79
94
}
80
- return adComponentRE . test ( chunk . name )
81
- ? `assets/ui-custom.[hash].js`
82
- : `assets/[name].[hash].js`
83
95
}
84
96
} )
85
97
}
@@ -133,4 +145,53 @@ export async function bundle(
133
145
return { clientResult, serverResult, pageToHashMap }
134
146
}
135
147
136
- const adComponentRE = / (?: C a r b o n | B u y S e l l ) A d s /
148
+ const cache = new Map < string , boolean > ( )
149
+
150
+ /**
151
+ * Check if a module is statically imported by at least one entry.
152
+ */
153
+ function isEagerChunk ( id : string , { getModuleInfo } : any ) {
154
+ if (
155
+ id . includes ( 'node_modules' ) &&
156
+ ! / \. c s s ( $ | \\ ? ) / . test ( id ) &&
157
+ staticImportedByEntry ( id , getModuleInfo , cache )
158
+ ) {
159
+ return 'vendor'
160
+ }
161
+ }
162
+
163
+ function staticImportedByEntry (
164
+ id : string ,
165
+ getModuleInfo : any ,
166
+ cache : Map < string , boolean > ,
167
+ importStack : string [ ] = [ ]
168
+ ) : boolean {
169
+ if ( cache . has ( id ) ) {
170
+ return cache . get ( id ) as boolean
171
+ }
172
+ if ( importStack . includes ( id ) ) {
173
+ // circular deps!
174
+ cache . set ( id , false )
175
+ return false
176
+ }
177
+ const mod = getModuleInfo ( id )
178
+ if ( ! mod ) {
179
+ cache . set ( id , false )
180
+ return false
181
+ }
182
+
183
+ if ( mod . isEntry ) {
184
+ cache . set ( id , true )
185
+ return true
186
+ }
187
+ const someImporterIs = mod . importers . some ( ( importer : string ) =>
188
+ staticImportedByEntry (
189
+ importer ,
190
+ getModuleInfo ,
191
+ cache ,
192
+ importStack . concat ( id )
193
+ )
194
+ )
195
+ cache . set ( id , someImporterIs )
196
+ return someImporterIs
197
+ }
0 commit comments