@@ -13,9 +13,9 @@ const defaultOptions = {
1313 manifestTransformer : null
1414}
1515const performanceAssetFilterList = [
16- ( file ) => ! ( / \. m a p $ / . test ( file ) ) ,
16+ ( file ) => ! / \. m a p $ / . test ( file ) ,
1717 ( file ) => ! file . endsWith ( '.zip' ) ,
18- ( file ) => ! ( / ^ i c o n s \/ / . test ( file ) )
18+ ( file ) => ! / ^ i c o n s \/ / . test ( file )
1919]
2020
2121function getManifestJsonString ( pluginOptions , jsonContent ) {
@@ -28,10 +28,11 @@ function getManifestJsonString(pluginOptions, jsonContent) {
2828
2929module . exports = ( api , options ) => {
3030 const appRootPath = api . getCwd ( )
31- const pluginOptions = options . pluginOptions . browserExtension ? Object . assign ( defaultOptions , options . pluginOptions . browserExtension ) : defaultOptions
31+ const pluginOptions = options . pluginOptions . browserExtension
32+ ? Object . assign ( defaultOptions , options . pluginOptions . browserExtension )
33+ : defaultOptions
3234 const componentOptions = pluginOptions . componentOptions
3335 const packageJson = require ( path . join ( appRootPath , 'package.json' ) )
34- const isDevelopment = api . service . mode === 'development'
3536 const isProduction = api . service . mode === 'production'
3637 const keyFile = api . resolve ( 'key.pem' )
3738 const hasKeyFile = fs . existsSync ( keyFile )
@@ -49,10 +50,10 @@ module.exports = (api, options) => {
4950 if ( ! Array . isArray ( paths ) ) {
5051 paths = [ paths ]
5152 }
52- entry [ name ] = paths . map ( path => api . resolve ( path ) )
53+ entry [ name ] = paths . map ( ( path ) => api . resolve ( path ) )
5354 }
5455 }
55- webpackConfig . merge ( { entry} )
56+ webpackConfig . merge ( { entry } )
5657 webpackConfig . optimization . delete ( 'splitChunks' )
5758 } )
5859
@@ -64,16 +65,19 @@ module.exports = (api, options) => {
6465 if ( webpackConfig . performance === undefined ) {
6566 webpackConfig . performance = { }
6667 }
67- webpackConfig . performance . assetFilter = ( assetFilename ) => performanceAssetFilterList . every ( ( filter ) => filter ( assetFilename ) )
68+ webpackConfig . performance . assetFilter = ( assetFilename ) =>
69+ performanceAssetFilterList . every ( ( filter ) => filter ( assetFilename ) )
6870
6971 if ( pluginOptions . autoImportPolyfill ) {
70- webpackConfig . plugins . push ( new webpack . ProvidePlugin ( {
71- 'browser' : 'webextension-polyfill'
72- } ) )
72+ webpackConfig . plugins . push (
73+ new webpack . ProvidePlugin ( {
74+ browser : 'webextension-polyfill'
75+ } )
76+ )
7377
7478 // Workaround for https://github.com/mozilla/webextension-polyfill/issues/68
7579 webpackConfig . module . rules . push ( {
76- test : require . resolve ( 'webextension-polyfill' , { paths : [ appRootPath ] } ) ,
80+ test : require . resolve ( 'webextension-polyfill' , { paths : [ appRootPath ] } ) ,
7781 use : 'imports-loader?browser=>undefined'
7882 } )
7983 }
@@ -86,64 +90,73 @@ module.exports = (api, options) => {
8690 }
8791 }
8892
89- webpackConfig . plugins . push ( new CopyWebpackPlugin ( [ {
90- from : './src/manifest.json' ,
91- to : 'manifest.json' ,
92- transform : ( content ) => {
93- return new Promise ( ( resolve , reject ) => {
94- const jsonContent = JSON . parse ( content )
95- if ( pluginOptions . manifestSync . includes ( 'version' ) ) {
96- jsonContent . version = packageJson . version
97- }
98- if ( pluginOptions . manifestSync . includes ( 'description' ) ) {
99- jsonContent . description = packageJson . description
100- }
101-
102- if ( isProduction ) {
103- return resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
104- }
105-
106- jsonContent . content_security_policy = jsonContent . content_security_policy || "script-src 'self' 'unsafe-eval'; object-src 'self'"
107-
108- try {
109- fs . statSync ( keyFile )
93+ webpackConfig . plugins . push (
94+ new CopyWebpackPlugin ( [
95+ {
96+ from : './src/manifest.json' ,
97+ to : 'manifest.json' ,
98+ transform : ( content ) => {
99+ return new Promise ( ( resolve , reject ) => {
100+ const jsonContent = JSON . parse ( content )
101+ if ( pluginOptions . manifestSync . includes ( 'version' ) ) {
102+ jsonContent . version = packageJson . version
103+ }
104+ if ( pluginOptions . manifestSync . includes ( 'description' ) ) {
105+ jsonContent . description = packageJson . description
106+ }
110107
111- return exec ( `openssl rsa -in ${ keyFile } -pubout -outform DER | openssl base64 -A` , ( error , stdout ) => {
112- if ( error ) {
113- // node couldn't execute the command
114- reject ( error )
108+ if ( isProduction ) {
109+ return resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
115110 }
116111
117- jsonContent . key = stdout
118- resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
112+ jsonContent . content_security_policy =
113+ jsonContent . content_security_policy || "script-src 'self' 'unsafe-eval'; object-src 'self'"
114+
115+ try {
116+ fs . statSync ( keyFile )
117+
118+ return exec ( `openssl rsa -in ${ keyFile } -pubout -outform DER | openssl base64 -A` , ( error , stdout ) => {
119+ if ( error ) {
120+ // node couldn't execute the command
121+ reject ( error )
122+ }
123+
124+ jsonContent . key = stdout
125+ resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
126+ } )
127+ } catch ( error ) {
128+ logger . warn (
129+ 'No key.pem file found. This is fine for dev, however you may have problems publishing without one'
130+ )
131+ resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
132+ }
119133 } )
120- } catch ( error ) {
121- logger . warn ( 'No key.pem file found. This is fine for dev, however you may have problems publishing without one' )
122- resolve ( getManifestJsonString ( pluginOptions , jsonContent ) )
123134 }
124- } )
125- }
126- } ] ) )
135+ }
136+ ] )
137+ )
127138
128139 if ( pluginOptions . modesToZip . includes ( api . service . mode ) ) {
129- webpackConfig . plugins . push ( new ZipPlugin ( {
130- path : api . resolve ( `${ options . outputDir || 'dist' } -zip` ) ,
131- filename : `${ packageJson . name } -v${ packageJson . version } -${ api . service . mode } .zip`
132- } ) )
140+ webpackConfig . plugins . push (
141+ new ZipPlugin ( {
142+ path : api . resolve ( `${ options . outputDir || 'dist' } -zip` ) ,
143+ filename : `${ packageJson . name } -v${ packageJson . version } -${ api . service . mode } .zip`
144+ } )
145+ )
133146 }
134147
135- if ( options . api === 'chrome' && isDevelopment ) {
136- const entries = { }
148+ // configure webpack-extension-reloader for automatic reloading of extension when content and background scripts change (not HMR)
149+ // enabled only when webpack mode === 'development'
150+ const entries = { }
137151
138- if ( pluginOptions . components . background ) {
139- entries . background = 'background'
140- }
152+ if ( pluginOptions . components . background ) {
153+ entries . background = 'background'
154+ }
141155
142- if ( pluginOptions . components . contentScripts ) {
143- entries . contentScript = Object . keys ( componentOptions . contentScripts . entries )
144- }
145- const ChromeExtensionReloader = require ( 'webpack-chrome-extension-reloader' )
146- webpackConfig . plugins . push ( new ChromeExtensionReloader ( { entries } ) )
156+ if ( pluginOptions . components . contentScripts ) {
157+ entries . contentScript = Object . keys ( componentOptions . contentScripts . entries )
147158 }
159+ const ExtensionReloader = require ( 'webpack-extension-reloader' )
160+ webpackConfig . plugins . push ( new ExtensionReloader ( { entries } ) )
148161 } )
149162}
0 commit comments