11import * as fs from 'fs'
22import { type Compiler } from '@rspack/core'
3- import WebExtensionFork from './webpack-target-webextension-fork'
43import WebExtension from 'webpack-target-webextension'
4+ import { createRequire } from 'node:module'
55import { filterKeysForThisBrowser } from '../../scripts-lib/manifest'
66import { SetupBackgroundEntry } from './setup-background-entry'
77import { ApplyManifestDevDefaults } from './apply-manifest-dev-defaults'
@@ -11,6 +11,21 @@ import type {
1111 DevOptions
1212} from '../../../../webpack-types'
1313
14+ const requireForEsm = createRequire ( import . meta. url )
15+
16+ function tryLoadWebExtensionFork ( ) :
17+ | ( new ( args : any ) => { apply : ( compiler : any ) => void } )
18+ | null {
19+ try {
20+ // prefer local fork when present (not shipped in CI)
21+ // eslint-disable-next-line @typescript-eslint/no-var-requires
22+ const mod = requireForEsm ( './webpack-target-webextension-fork' )
23+ return ( mod && ( mod . default || mod ) ) as any
24+ } catch {
25+ return null
26+ }
27+ }
28+
1429export class SetupReloadStrategy {
1530 private readonly manifestPath : string
1631 private readonly browser : DevOptions [ 'browser' ]
@@ -77,16 +92,21 @@ export class SetupReloadStrategy {
7792
7893 // 4 - Now that we know the background exists, add the web extension target
7994 // using it. This is our core upstream plugin.
80- if ( process . env . EXTENSION_EXPERIMENTAL_HMR === 'true' ) {
81- new WebExtensionFork ( {
82- background : this . getEntryName ( patchedManifest ) ,
83- weakRuntimeCheck : true
84- } ) . apply ( compiler as any )
85- } else {
86- new WebExtension ( {
95+ const wantFork = process . env . EXTENSION_EXPERIMENTAL_HMR === 'true'
96+
97+ if ( wantFork ) {
98+ const Fork = tryLoadWebExtensionFork ( )
99+ const PluginCtor = Fork || ( WebExtension as any )
100+ new PluginCtor ( {
87101 background : this . getEntryName ( patchedManifest ) ,
88102 weakRuntimeCheck : true
89103 } ) . apply ( compiler as any )
104+ return
90105 }
106+
107+ new WebExtension ( {
108+ background : this . getEntryName ( patchedManifest ) ,
109+ weakRuntimeCheck : true
110+ } ) . apply ( compiler as any )
91111 }
92112}
0 commit comments