Skip to content

Commit f77d886

Browse files
committed
refactor manifest transformat into own file
1 parent ecd96a8 commit f77d886

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

index.js

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const webpack = require('webpack')
33
const CopyWebpackPlugin = require('copy-webpack-plugin')
44
const ExtensionReloader = require('webpack-extension-reloader')
55
const ZipPlugin = require('zip-webpack-plugin')
6-
const { keyExists, hashKey } = require('./lib/signing-key')
6+
const { keyExists } = require('./lib/signing-key')
7+
const manifestTransformer = require('./lib/manifest')
78
const defaultOptions = {
89
components: {},
910
componentOptions: {},
@@ -17,14 +18,6 @@ const performanceAssetFilterList = [
1718
(file) => !/^icons\//.test(file)
1819
]
1920

20-
function getManifestJsonString (pluginOptions, jsonContent) {
21-
if (pluginOptions.manifestTransformer) {
22-
const jsonContentCopy = Object.assign({}, jsonContent)
23-
jsonContent = pluginOptions.manifestTransformer(jsonContentCopy)
24-
}
25-
return JSON.stringify(jsonContent, null, 2)
26-
}
27-
2821
module.exports = (api, options) => {
2922
const appRootPath = api.getCwd()
3023
const pluginOptions = options.pluginOptions.browserExtension
@@ -68,41 +61,7 @@ module.exports = (api, options) => {
6861
{
6962
from: './src/manifest.json',
7063
to: 'manifest.json',
71-
transform: async (content) => {
72-
const jsonContent = JSON.parse(content)
73-
if (pluginOptions.manifestSync.includes('version')) {
74-
jsonContent.version = packageJson.version
75-
}
76-
if (pluginOptions.manifestSync.includes('description')) {
77-
jsonContent.description = packageJson.description
78-
}
79-
80-
jsonContent.content_security_policy =
81-
jsonContent.content_security_policy || "script-src 'self' 'unsafe-eval'; object-src 'self'"
82-
83-
// If building for production (going to web store) abort early.
84-
// The browser extension store will hash your signing key and apply CSP policies.
85-
if (isProduction) {
86-
jsonContent.content_security_policy = jsonContent.content_security_policy.replace(/'unsafe-eval'/, '')
87-
88-
return getManifestJsonString(pluginOptions, jsonContent)
89-
}
90-
91-
if (hasKeyFile) {
92-
try {
93-
jsonContent.key = await hashKey(keyFile)
94-
} catch (error) {
95-
logger.error('Unexpected error hashing keyfile:', error)
96-
}
97-
}
98-
if (!jsonContent.key) {
99-
logger.warn(
100-
'No key.pem file found. This is fine for dev, however you may have problems publishing without one'
101-
)
102-
}
103-
104-
return getManifestJsonString(pluginOptions, jsonContent)
105-
}
64+
transform: manifestTransformer(api, pluginOptions, packageJson)
10665
}
10766
]
10867
])

lib/manifest.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const logger = require('@vue/cli-shared-utils')
2+
const { keyExists, hashKey } = require('./signing-key')
3+
const validPackageSync = ['version', 'description']
4+
5+
function getManifestJsonString ({ manifestTransformer }, manifest) {
6+
if (manifestTransformer && typeof manifestTransformer === 'function') {
7+
const manifestCopy = JSON.parse(JSON.stringify(manifest))
8+
manifest = manifestTransformer(manifestCopy)
9+
}
10+
return JSON.stringify(manifest, null, 2)
11+
}
12+
13+
function syncManifestWithPackageJson ({ manifestSync }, packageJson, manifest) {
14+
validPackageSync.forEach((property) => {
15+
if (manifestSync.includes(property)) {
16+
manifest[property] = packageJson[property]
17+
}
18+
})
19+
}
20+
21+
module.exports = (api, pluginOptions, packageJson) => async (content) => {
22+
const manifest = JSON.parse(content)
23+
const keyFile = api.resolve('key.pem')
24+
const hasKeyFile = keyExists(keyFile)
25+
const isProduction = api.service.mode === 'production'
26+
27+
syncManifestWithPackageJson(pluginOptions, packageJson, manifest)
28+
29+
manifest.content_security_policy =
30+
manifest.content_security_policy || "script-src 'self' 'unsafe-eval'; object-src 'self'"
31+
32+
// validate manifest
33+
34+
// If building for production (going to web store) abort early.
35+
// The browser extension store will hash your signing key and apply CSP policies.
36+
if (isProduction) {
37+
manifest.content_security_policy = manifest.content_security_policy.replace(/'unsafe-eval'/, '')
38+
39+
// validate minimum options
40+
41+
return getManifestJsonString(pluginOptions, manifest)
42+
}
43+
44+
if (hasKeyFile) {
45+
try {
46+
manifest.key = await hashKey(keyFile)
47+
} catch (error) {
48+
logger.error('Unexpected error hashing keyfile:', error)
49+
}
50+
}
51+
52+
// It's possible to specify the hashed key property manually without a keyfile local in the repo
53+
if (!manifest.key) {
54+
logger.warn('No key.pem file found. This is fine for dev, however you may have problems publishing without one')
55+
}
56+
57+
return getManifestJsonString(pluginOptions, manifest)
58+
}

0 commit comments

Comments
 (0)