11const path = require ( 'path' )
2+ const fs = require ( 'fs' )
3+ const { exec } = require ( 'child_process' )
4+ const isProduction = process . env . NODE_ENV === 'production'
5+ const appRootPath = process . cwd ( )
26const { log } = require ( '@vue/cli-shared-utils' )
37const CopyWebpackPlugin = require ( 'copy-webpack-plugin' )
48const ChromeExtensionReloader = require ( 'webpack-chrome-extension-reloader' )
59const WebpackShellPlugin = require ( 'webpack-shell-plugin-next' )
610const HtmlWebpackPlugin = require ( 'html-webpack-plugin' )
7- const { name, version } = require ( path . join ( process . cwd ( ) , 'package.json' ) )
11+ const { name, version } = require ( path . join ( appRootPath , 'package.json' ) )
812
913module . exports = ( api ) => {
1014 api . configureWebpack ( webpackConfig => {
@@ -15,20 +19,43 @@ module.exports = (api) => {
1519 webpackConfig . entry . background = './src/background.js'
1620 webpackConfig . entry [ 'popup/popup' ] = './src/popup/popup.js'
1721
22+ if ( isProduction ) {
23+ webpackConfig . plugins . push ( new CopyWebpackPlugin ( [ { from : './key.pem' , to : 'key.pem' } ] ) )
24+ }
25+
1826 webpackConfig . plugins . push ( new CopyWebpackPlugin ( [
1927 { from : './src/icons' , to : 'icons/[name].[ext]' , ignore : [ 'icon.xcf' ] } ,
2028 {
2129 from : './src/manifest.json' ,
2230 to : 'manifest.json' ,
2331 transform : ( content ) => {
24- const jsonContent = JSON . parse ( content )
25- jsonContent . version = version
32+ return new Promise ( ( resolve , reject ) => {
33+ const jsonContent = JSON . parse ( content )
34+ jsonContent . version = version
2635
27- if ( process . env . NODE_ENV === 'development' ) {
28- jsonContent [ 'content_security_policy' ] = "script-src 'self' 'unsafe-eval'; object-src 'self'"
29- }
36+ if ( isProduction ) {
37+ return resolve ( JSON . stringify ( jsonContent , null , 2 ) )
38+ }
39+
40+ jsonContent . content_security_policy = "script-src 'self' 'unsafe-eval'; object-src 'self'"
41+
42+ try {
43+ const keyfile = path . join ( appRootPath , 'key.pem' )
44+ fs . statSync ( keyfile )
45+
46+ return exec ( `openssl rsa -in ${ keyfile } -pubout -outform DER | openssl base64 -A` , ( error , stdout ) => {
47+ if ( error ) {
48+ // node couldn't execute the command
49+ reject ( error )
50+ }
3051
31- return JSON . stringify ( jsonContent , null , 2 )
52+ jsonContent . key = stdout
53+ resolve ( JSON . stringify ( jsonContent , null , 2 ) )
54+ } )
55+ } catch ( error ) {
56+ console . log ( 'No key.pem file found. This is fine for dev, you will have problems publishing without one' )
57+ }
58+ } )
3259 }
3360 }
3461 ] ) )
0 commit comments