1- import * as fs from "node:fs" ;
2- import path from "node:path" ;
31import { type RsbuildPlugin , rspack } from "@rsbuild/core" ;
4- import { createJiti } from "jiti" ;
52import ManifestParser from "./manifest/parser.js" ;
63
74interface Options {
8- manifestPath : string ;
5+ manifest : chrome . runtime . ManifestV3 ;
96}
107
118const pluginName = "rsbuild:plugin-web-extension" ;
129
13- const port = + ( process . env . PORT || 3130 ) ;
14-
15- export const pluginWebExtension = ( {
16- manifestPath,
17- } : Options ) : RsbuildPlugin => ( {
10+ export const pluginWebExtension = ( { manifest } : Options ) : RsbuildPlugin => ( {
1811 name : pluginName ,
1912 setup : async ( api ) => {
20- const jiti = createJiti ( api . context . rootPath , { moduleCache : false } ) ;
21-
22- const manifestSourcePath = path . resolve ( api . context . rootPath , manifestPath ) ;
23- if ( ! fs . existsSync ( manifestSourcePath ) ) {
24- throw new Error ( `${ pluginName } : Failed to read ${ manifestSourcePath } ` ) ;
25- }
26-
27- const manifestModule = jiti ( manifestSourcePath ) ;
28- const manifest = manifestModule . default || manifestModule . manifest ;
29-
3013 const htmlEntryPoints = Object . entries ( {
3114 popup : manifest . action ?. default_popup ,
3215 devtools : manifest . devtools_page ,
@@ -48,10 +31,6 @@ export const pluginWebExtension = ({
4831
4932 api . modifyRspackConfig ( ( config , { mergeConfig, HtmlPlugin } ) => {
5033 return mergeConfig ( config , {
51- output : {
52- hotUpdateChunkFilename : "hot/[id].[fullhash].hot-update.js" ,
53- hotUpdateMainFilename : "hot/[runtime].[fullhash].hot-update.json" ,
54- } ,
5534 plugins : htmlEntryPoints . map ( ( [ name , template ] ) => {
5635 return new HtmlPlugin ( {
5736 chunks : [ name ] ,
@@ -82,15 +61,9 @@ export const pluginWebExtension = ({
8261 js : "src/[name]" ,
8362 } ,
8463 } ,
85- server : {
86- port,
87- } ,
64+
8865 dev : {
8966 writeToDisk : true ,
90- client : {
91- port,
92- host : "localhost" ,
93- } ,
9467 } ,
9568 } ) ;
9669 } ) ;
@@ -104,22 +77,16 @@ export const pluginWebExtension = ({
10477 compilation . hooks . processAssets . tap (
10578 {
10679 name : pluginName ,
107- stage : compiler . webpack . Compilation . PROCESS_ASSETS_STAGE_ADDITIONAL ,
80+ stage : rspack . Compilation . PROCESS_ASSETS_STAGE_ADDITIONAL ,
10881 } ,
10982 ( ) => {
110- compilation . fileDependencies . add ( manifestSourcePath ) ;
111-
112- const updatedManifestModule = jiti ( manifestPath ) ;
113- const manifest =
114- updatedManifestModule . default || updatedManifestModule . manifest ;
11583 const content = ManifestParser . convertManifestToString ( manifest ) ;
11684
11785 const { RawSource } = compiler . webpack . sources ;
11886
11987 const source = new RawSource ( content ) ;
120- const outputFilename = "manifest.json" ;
12188
122- compilation . emitAsset ( outputFilename , source ) ;
89+ compilation . emitAsset ( "manifest.json" , source ) ;
12390 }
12491 ) ;
12592 } ) ;
0 commit comments