Skip to content

Commit b719d8b

Browse files
committed
Second attempt
1 parent f1c0fc4 commit b719d8b

File tree

2 files changed

+32
-9
lines changed
  • programs
    • cli/commands
    • develop/webpack/plugin-extension/feature-scripts/steps/setup-reload-strategy

2 files changed

+32
-9
lines changed

programs/cli/commands/dev.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ export function registerDevCommand(program: Command, telemetry: any) {
146146

147147
let extensionDev: any
148148
try {
149-
;({extensionDev} = await requireOrDlx('extension-develop', versionExact))
149+
;({extensionDev} = await requireOrDlx(
150+
'extension-develop',
151+
versionExact
152+
))
150153
} catch {
151154
;({extensionDev} = await requireOrDlx('extension-develop', major))
152155
}

programs/develop/webpack/plugin-extension/feature-scripts/steps/setup-reload-strategy/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs'
22
import {type Compiler} from '@rspack/core'
3-
import WebExtensionFork from './webpack-target-webextension-fork'
43
import WebExtension from 'webpack-target-webextension'
4+
import {createRequire} from 'node:module'
55
import {filterKeysForThisBrowser} from '../../scripts-lib/manifest'
66
import {SetupBackgroundEntry} from './setup-background-entry'
77
import {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+
1429
export 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

Comments
 (0)