Skip to content

Commit 465d2e0

Browse files
committed
fix: compatibility with module, although it still not working
1 parent 8932412 commit 465d2e0

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

lib/webpack5/InitialChunkFile.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,20 @@ module.exports = class WebExtensionContentScriptEntryPlugin {
8888
/** @type {string[]} */
8989
let code
9090
if (isBackgroundEntry) {
91-
// TODO: support ES Module output
92-
code = [
93-
'try {',
94-
Template.indent(
95-
'importScripts(' +
96-
getInitialAndAsyncFiles(compilation, entry)
97-
.map((file) => JSON.stringify(file))
98-
.join(', ') +
99-
');'
100-
),
101-
'} catch (e) {',
102-
Template.indent('console.error(e);'),
103-
'}',
104-
]
91+
const asyncAndSyncFiles = getInitialAndAsyncFiles(compilation, entry)
92+
if (compilation.outputOptions.chunkFormat === 'module') {
93+
code = asyncAndSyncFiles.map((file) => `import ${JSON.stringify('./' + file)};`)
94+
} else {
95+
code = [
96+
'try {',
97+
Template.indent(
98+
'importScripts(' + asyncAndSyncFiles.map((file) => JSON.stringify(file)).join(', ') + ');'
99+
),
100+
'} catch (e) {',
101+
Template.indent('console.error(e);'),
102+
'}',
103+
]
104+
}
105105
} else {
106106
code = [
107107
';(() => {',
@@ -174,7 +174,7 @@ module.exports = class WebExtensionContentScriptEntryPlugin {
174174
function getInitialFiles(compilation, entry) {
175175
const entryPoint = compilation.entrypoints.get(entry)
176176
if (!entryPoint) return undefined
177-
const allFiles = entryPoint.getFiles().filter((file) => file.endsWith('.js') && compilation.getAsset(file))
177+
const allFiles = entryPoint.getFiles().filter((file) => isJSFile(file) && compilation.getAsset(file))
178178
if ('rspack' in compilation.compiler) {
179179
const runtimeChunk = entryPoint.getRuntimeChunk()
180180
if (runtimeChunk) {
@@ -206,6 +206,13 @@ function getInitialAndAsyncFiles(compilation, entry) {
206206
}
207207
}
208208
entryPoint.chunks.forEach(visit)
209-
const allFiles = [...files].filter((file) => file.endsWith('.js') && compilation.getAsset(file))
209+
const allFiles = [...files].filter((file) => isJSFile(file) && compilation.getAsset(file))
210210
return allFiles
211211
}
212+
213+
/**
214+
* @param {string} file
215+
*/
216+
function isJSFile(file) {
217+
return file.endsWith('.js') || file.endsWith('.mjs')
218+
}

lib/webpack5/ServiceWorkerPlugin.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ module.exports = class WebExtensionServiceWorkerEntryPlugin {
1515
const { javascript, sources } = compiler.webpack
1616
const entry = this.options.serviceWorkerEntry
1717
if (entry === undefined) return
18-
const hook = compiler.hooks.entryOption
19-
// Set chunkLoading to import-scripts
20-
// @ts-ignore DO NOT add return boolean to this function, this is a BailHook and we don't want to bail.
21-
hook.tap(WebExtensionServiceWorkerEntryPlugin.name, (context, entries) => {
22-
if (typeof entries === 'function') {
23-
if (this.options.noDynamicEntryWarning) return
24-
console.warn(`[webpack-extension-target] Dynamic entry points not supported yet.
18+
if (!(compiler.options.output.module || compiler.options.experiments.outputModule)) {
19+
const hook = compiler.hooks.entryOption
20+
// Set chunkLoading to import-scripts
21+
hook.tap(WebExtensionServiceWorkerEntryPlugin.name, (context, entries) => {
22+
if (typeof entries === 'function') {
23+
if (this.options.noDynamicEntryWarning) return
24+
console.warn(`[webpack-extension-target] Dynamic entry points not supported yet.
2525
You must manually set the chuck loading of entry point ${entry} to "import-scripts".
2626
2727
See https://webpack.js.org/configuration/entry-context/#entry-descriptor
2828
2929
Set background.noDynamicEntryWarning to true to disable this warning.
3030
`)
31-
}
32-
/** @type {import('@rspack/core').EntryDescriptionNormalized} */
33-
const selectedEntry = /** @type {any} */ (entries)[entry]
34-
if (!selectedEntry) throw new Error(`[webpack-extension-target] There is no entry called ${entry}.`)
35-
selectedEntry.chunkLoading = 'import-scripts'
36-
})
31+
}
32+
/** @type {import('@rspack/core').EntryDescriptionNormalized} */
33+
const selectedEntry = /** @type {any} */ (entries)[entry]
34+
if (!selectedEntry) throw new Error(`[webpack-extension-target] There is no entry called ${entry}.`)
35+
selectedEntry.chunkLoading = 'import-scripts'
36+
})
37+
}
3738

3839
// Set all lazy chunks to eagerly loaded
3940
// See https://bugs.chromium.org/p/chromium/issues/detail?id=1198822

0 commit comments

Comments
 (0)