Skip to content

Commit 9703470

Browse files
committed
Improve configuration
- Make background script optional and allow setting path - Allow multiple content scripts - Move all options out of 'options' wrapper.
1 parent 1851c80 commit 9703470

File tree

6 files changed

+60
-21
lines changed

6 files changed

+60
-21
lines changed

generator/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@ const gitignoreSnippet = `
88
/dist-zip
99
`
1010

11-
module.exports = (api, options) => {
11+
module.exports = (api, _options) => {
12+
const options = Object.assign({}, _options)
13+
options.componentOptions = {}
14+
if (options.components.background) {
15+
options.componentOptions.background = {
16+
entry: 'src/background.js'
17+
}
18+
}
19+
if (options.components.contentScripts) {
20+
options.componentOptions.contentScripts = {
21+
entries: {
22+
'content_scripts/content-script': ['src/content_scripts/content-script.js']
23+
}
24+
}
25+
}
26+
1227
const appRootPath = process.cwd()
1328
const { name } = require(path.join(appRootPath, 'package.json'))
1429
const eslintConfig = { env: { webextensions: true } }
@@ -24,7 +39,7 @@ module.exports = (api, options) => {
2439
vue: {
2540
pages: {},
2641
pluginOptions: {
27-
browserExtension: { options }
42+
browserExtension: options
2843
}
2944
}
3045
}
@@ -44,6 +59,10 @@ module.exports = (api, options) => {
4459
api.extendPackage(pkg)
4560
api.render('./template/base-app', { name, ...options })
4661

62+
if (options.components.background) {
63+
api.render('./template/background', { name, ...options })
64+
}
65+
4766
if (options.components.popup) {
4867
api.render('./template/popup', { name, ...options })
4968

@@ -73,7 +92,7 @@ module.exports = (api, options) => {
7392
}
7493
}
7594

76-
if (options.components.contentScript) {
95+
if (options.components.contentScripts) {
7796
api.render('./template/content-script', { ...options })
7897
}
7998

File renamed without changes.

generator/template/base-app/src/manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
"128": "icons/128.png"
1010
},
1111
"permissions": [
12-
<%_ if (options.components.contentScript) { -%>
12+
<%_ if (options.components.contentScripts) { -%>
1313
"activeTab",
1414
<%_ } -%>
1515
"<all_urls>",
1616
"*://*/*"
1717
],
18+
<%_ if (options.components.background) { -%>
1819
"background": {
1920
"scripts": ["background.js"],
2021
"persistent": false
2122
},
23+
<%_ } -%>
2224
"browser_action": {
2325
<%_ if (options.components.popup) { -%>
2426
"default_popup": "popup/popup.html",

generator/template/content-script/src/content-script.js renamed to generator/template/content-script/src/content_scripts/content-script.js

File renamed without changes.

index.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,35 @@ const logger = require('@vue/cli-shared-utils')
55
const webpack = require('webpack')
66
const CopyWebpackPlugin = require('copy-webpack-plugin')
77
const ZipPlugin = require('zip-webpack-plugin')
8-
const defaultOptions = { components: {} }
8+
const defaultOptions = { components: {}, componentOptions: {} }
99

1010
module.exports = (api, options) => {
1111
const appRootPath = api.getCwd()
12-
const pluginOptions = options.pluginOptions.browserExtension ? options.pluginOptions.browserExtension.options : defaultOptions
12+
const pluginOptions = options.pluginOptions.browserExtension ? options.pluginOptions.browserExtension : defaultOptions
13+
const componentOptions = pluginOptions.componentOptions
1314
const { name, version } = require(path.join(appRootPath, 'package.json'))
1415
const isDevelopment = api.service.mode === 'development'
1516
const isProduction = api.service.mode === 'production'
1617
const keyFile = api.resolve('key.pem')
1718
const hasKeyFile = fs.existsSync(keyFile)
18-
const backgroundFile = api.resolve('src/background.js')
19-
const contentScriptFile = api.resolve('src/content-script.js')
2019

2120
api.chainWebpack((webpackConfig) => {
22-
webpackConfig.entryPoints
23-
.delete('app').end()
24-
.entry('background').add(backgroundFile).end()
25-
.when(pluginOptions.components.contentScript, (config) => {
26-
config.entry('content-script').add(contentScriptFile).end()
27-
})
21+
const config = webpackConfig.entryPoints.delete('app').end()
22+
const entry = {}
23+
if (pluginOptions.components.background) {
24+
entry['background'] = [api.resolve(componentOptions.background.entry)]
25+
}
26+
if (pluginOptions.components.contentScripts) {
27+
const entries = componentOptions.contentScripts.entries
28+
for (const name of Object.keys(entries)) {
29+
let paths = entries[name]
30+
if (!Array.isArray(paths)) {
31+
paths = [paths]
32+
}
33+
entry[name] = paths.map(path => api.resolve(path))
34+
}
35+
}
36+
config.merge({entry})
2837
})
2938

3039
api.configureWebpack((webpackConfig) => {
@@ -97,12 +106,15 @@ module.exports = (api, options) => {
97106
}
98107

99108
if (options.api === 'chrome' && isDevelopment) {
100-
const entries = { background: 'background' }
109+
const entries = {}
101110

102-
if (pluginOptions.components.contentScript) {
103-
entries.contentScript = 'content-script'
111+
if (pluginOptions.components.background) {
112+
entries.background = 'background'
104113
}
105114

115+
if (pluginOptions.components.contentScripts) {
116+
entries.contentScript = Object.keys(componentOptions.contentScripts.entries)
117+
}
106118
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader')
107119
webpackConfig.plugins.push(new ChromeExtensionReloader({ entries }))
108120
}

prompts.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ module.exports = [
22
{
33
name: 'components',
44
type: 'checkbox',
5-
default: ['popup'],
5+
defaults: ['popup', 'background'],
66
message: 'Which browser extension components do you wish to generate?',
77
choices: [
8+
{
9+
name: 'Background script',
10+
value: 'background',
11+
short: 'background',
12+
checked: true
13+
},
814
{
915
name: 'Browser Action Popup',
1016
value: 'popup',
@@ -17,9 +23,9 @@ module.exports = [
1723
short: 'options'
1824
},
1925
{
20-
name: 'Content Script',
21-
value: 'contentScript',
22-
short: 'content script'
26+
name: 'Content Scripts',
27+
value: 'contentScripts',
28+
short: 'content scripts'
2329
},
2430
{
2531
name: 'Standalone Tab',

0 commit comments

Comments
 (0)