@@ -135,7 +135,7 @@ export const pluginContentScripts: CrxPluginFn = () => {
135
135
} ,
136
136
}
137
137
} ,
138
- generateBundle ( ) {
138
+ generateBundle ( _options , bundle ) {
139
139
// emit content script loaders
140
140
for ( const [ key , script ] of contentScripts )
141
141
if ( key === script . refId ) {
@@ -145,12 +145,29 @@ export const pluginContentScripts: CrxPluginFn = () => {
145
145
} else if ( script . type === 'loader' ) {
146
146
const fileName = this . getFileName ( script . refId )
147
147
script . fileName = fileName
148
- const refId = this . emitFile ( {
149
- type : 'asset' ,
150
- name : getFileName ( { type : 'loader' , id : basename ( script . id ) } ) ,
151
- source : createProLoader ( { fileName } ) ,
152
- } )
153
- script . loaderName = this . getFileName ( refId )
148
+
149
+ const bundleFileInfo = bundle [ fileName ]
150
+ // the loader uses IIFE which in this case needlessly
151
+ // delays content script execution which may not be desired
152
+ const shouldUseLoader = ! (
153
+ bundleFileInfo . type === 'chunk' &&
154
+ bundleFileInfo . imports . length === 0 &&
155
+ bundleFileInfo . dynamicImports . length === 0 &&
156
+ bundleFileInfo . exports . length === 0
157
+ )
158
+
159
+ if ( shouldUseLoader ) {
160
+ const refId = this . emitFile ( {
161
+ type : 'asset' ,
162
+ name : getFileName ( {
163
+ type : 'loader' ,
164
+ id : basename ( script . id ) ,
165
+ } ) ,
166
+ source : createProLoader ( { fileName } ) ,
167
+ } )
168
+
169
+ script . loaderName = this . getFileName ( refId )
170
+ }
154
171
} else if ( script . type === 'iife' ) {
155
172
throw new Error ( 'IIFE content scripts are not implemented' )
156
173
}
0 commit comments