@@ -108,7 +108,8 @@ export function htmlInlineProxyPlugin(config: ResolvedConfig): Plugin {
108
108
const url = file . replace ( normalizePath ( config . root ) , '' )
109
109
const result = htmlProxyMap . get ( config ) ! . get ( url ) ?. [ index ]
110
110
if ( result ) {
111
- return result
111
+ // set moduleSideEffects to keep the module even if `treeshake.moduleSideEffects=false` is set
112
+ return { ...result , moduleSideEffects : true }
112
113
} else {
113
114
throw new Error ( `No matching HTML proxy module found from ${ id } ` )
114
115
}
@@ -426,6 +427,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
426
427
return url
427
428
}
428
429
430
+ const setModuleSideEffectPromises : Promise < void > [ ] = [ ]
429
431
await traverseHtml ( html , id , ( node ) => {
430
432
if ( ! nodeIsElement ( node ) ) {
431
433
return
@@ -452,6 +454,19 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
452
454
if ( isModule ) {
453
455
inlineModuleIndex ++
454
456
if ( url && ! isExcludedUrl ( url ) && ! isPublicFile ) {
457
+ setModuleSideEffectPromises . push (
458
+ this . resolve ( url , id )
459
+ . then ( ( resolved ) => {
460
+ if ( ! resolved ) {
461
+ return Promise . reject ( )
462
+ }
463
+ return this . load ( resolved )
464
+ } )
465
+ . then ( ( mod ) => {
466
+ // set this to keep the module even if `treeshake.moduleSideEffects=false` is set
467
+ mod . moduleSideEffects = true
468
+ } ) ,
469
+ )
455
470
// <script type="module" src="..."/>
456
471
// add it as an import
457
472
js += `\nimport ${ JSON . stringify ( url ) } `
@@ -687,6 +702,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
687
702
js = `import "${ modulePreloadPolyfillId } ";\n${ js } `
688
703
}
689
704
705
+ await Promise . all ( setModuleSideEffectPromises )
706
+
690
707
// Force rollup to keep this module from being shared between other entry points.
691
708
// If the resulting chunk is empty, it will be removed in generateBundle.
692
709
return { code : js , moduleSideEffects : 'no-treeshake' }
0 commit comments