Skip to content

Commit 72863ee

Browse files
committed
Refactored Prompts into a single list
Storing results of generation as config values Consuming new config values
1 parent 8e4071d commit 72863ee

File tree

5 files changed

+72
-50
lines changed

5 files changed

+72
-50
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ yarn test
7777
- Add some generator options for other pieces of browser extensions. This includes scaffolding the components/dirs, and registering the build options into the build time hooks.
7878
- Dev Tools
7979
- Dedicated extension pages
80-
- Content scripts
81-
- More configurability in scaffolding, like [Kocal/vue-web-extension](https://github.com/Kocal/vue-web-extension) does
8280
- A preset
8381
- Key Generation
82+
- Cleanup the dist-zip directory
8483

8584
## Credits
8685
- [https://github.com/Kocal/vue-web-extension](https://github.com/Kocal/vue-web-extension) For inspiration on app and build structure

generator/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ module.exports = (api, options) => {
2020
dependencies: {
2121
'vue-router': '^3.0.1',
2222
'vuex': '^3.0.1'
23+
},
24+
vue: {
25+
pages: {},
26+
pluginOptions: {
27+
browserExtension: { options }
28+
}
2329
}
2430
}
25-
const renderConfig = {
26-
pages: {}
27-
}
2831

2932
if (api.hasPlugin('eslint')) {
3033
console.log('Adding eslint config stuffs')
@@ -37,7 +40,7 @@ module.exports = (api, options) => {
3740
if (options.popupPage) {
3841
api.render('./template/popup', { name, ...options })
3942

40-
renderConfig.pages['popup/popup'] = {
43+
pkg.vue.pages['popup/popup'] = {
4144
entry: 'src/popup/popup.js',
4245
title: 'Popup'
4346
}
@@ -46,7 +49,7 @@ module.exports = (api, options) => {
4649
if (options.optionsPage) {
4750
api.render('./template/options', { name, ...options })
4851

49-
renderConfig.pages['options/options'] = {
52+
pkg.vue.pages['options/options'] = {
5053
entry: 'src/options/options.js',
5154
title: 'Options'
5255
}
@@ -56,10 +59,6 @@ module.exports = (api, options) => {
5659
api.render('./template/content-script', { ...options })
5760
}
5861

59-
api.render((files) => {
60-
files['vue.config.js'] = api.genJSConfig(renderConfig)
61-
})
62-
6362
api.onCreateComplete(() => {
6463
const gitignore = fs.readFileSync(api.resolve('./.gitignore'), 'utf8')
6564
fs.writeFileSync(api.resolve('./.gitignore'), gitignore + gitignoreSnippet)

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99
"128": "icons/128.png"
1010
},
1111
"permissions": [
12+
<%_ if (options.components.contentScript) { -%>
13+
"activeTab",
14+
<%_ } -%>
1215
"<all_urls>",
13-
"*://*/*"<% if (options.contentScript) { %>,
14-
"activeTab"<% } %>
16+
"*://*/*"
1517
],
1618
"background": {
1719
"scripts": ["background.js"],
1820
"persistent": false
1921
},
2022
"browser_action": {
21-
<%_ if (options.popupPage) { -%>
23+
<%_ if (options.components.popup) { -%>
2224
"default_popup": "popup/popup.html",
2325
<%_ } -%>
2426
"default_title": "<%- name %>",
2527
"default_icon": {
2628
"19": "icons/19.png",
2729
"38": "icons/38.png"
2830
}
29-
}<% if (options.optionsPage) { %>,
31+
<%_ if (options.components.options) { -%>
32+
},
3033
"options_ui": {
3134
"page": "options/options.html",
3235
"chrome_style": true
33-
}<% } %>
36+
<%_ } -%>
37+
}
3438
}

index.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,27 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
66
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader')
77
const WebpackShellPlugin = require('webpack-shell-plugin-next')
88
const ZipPlugin = require('zip-webpack-plugin')
9+
const defaultOptions = { components: {} }
910

10-
module.exports = (api) => {
11+
module.exports = (api, options) => {
1112
const appRootPath = api.getCwd()
13+
const pluginOptions = options.pluginOptions.browserExtension ? options.pluginOptions.browserExtension : defaultOptions
1214
const { name, version } = require(path.join(appRootPath, 'package.json'))
1315
const isDevelopment = api.service.mode === 'development'
1416
const isProduction = api.service.mode === 'production'
15-
const outputDir = api.resolve(api.service.projectOptions.outputDir || 'dist')
17+
const outputDir = api.resolve(options.outputDir || 'dist')
1618
const packageScript = isProduction ? null : 'remove-evals.js'
1719
const keyFile = api.resolve('key.pem')
1820
const hasKeyFile = fs.existsSync(keyFile)
1921
const backgroundFile = api.resolve('src/background.js')
2022
const contentScriptFile = api.resolve('src/content-script.js')
21-
const hasBackgroundFile = fs.existsSync(backgroundFile)
22-
const hasContentScriptFile = fs.existsSync(contentScriptFile)
2323

2424
api.chainWebpack((webpackConfig) => {
25-
webpackConfig.entryPoints.delete('app').end()
26-
.when(hasBackgroundFile, (config) => {
27-
config.entry('background')
28-
.add(backgroundFile)
29-
.end()
30-
})
31-
.when(hasContentScriptFile, (config) => {
32-
config.entry('content-script')
33-
.add(contentScriptFile)
34-
.end()
25+
webpackConfig.entryPoints
26+
.delete('app').end()
27+
.entry('background').add(backgroundFile).end()
28+
.when(pluginOptions.components.contentScript, (config) => {
29+
config.entry('content-script').add(contentScriptFile).end()
3530
})
3631
})
3732

@@ -86,7 +81,7 @@ module.exports = (api) => {
8681

8782
if (packageScript === null) {
8883
webpackConfig.plugins.push(new ZipPlugin({
89-
path: api.resolve(`${api.service.projectOptions.outputDir || 'dist'}-zip`),
84+
path: api.resolve(`${options.outputDir || 'dist'}-zip`),
9085
filename: `${name}-v${version}.zip`
9186
}))
9287
} else {
@@ -100,11 +95,9 @@ module.exports = (api) => {
10095
}
10196

10297
if (isDevelopment) {
103-
const entries = {}
104-
if (hasBackgroundFile) {
105-
entries.background = 'background'
106-
}
107-
if (hasContentScriptFile) {
98+
const entries = { background: 'background' }
99+
100+
if (pluginOptions.components.contentScript) {
108101
entries.contentScript = 'content-script'
109102
}
110103

prompts.js

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
11
module.exports = [
22
{
3-
name: 'popupPage',
4-
type: 'confirm',
5-
default: true,
6-
message: 'Generate a browser action popup? (https://developer.chrome.com/extensions/user_interface#browser_action)'
7-
}, {
8-
name: 'optionsPage',
9-
type: 'confirm',
10-
default: false,
11-
message: 'Generate an options page? (https://developer.chrome.com/extensions/options)'
12-
}, {
13-
name: 'contentScript',
14-
type: 'confirm',
15-
default: false,
16-
message: 'Generate a content script? (https://developer.chrome.com/extensions/content_scripts)'
3+
name: 'components',
4+
type: 'checkbox',
5+
default: ['popup'],
6+
message: 'Which browser extension components do you wish to generate?',
7+
choices: [
8+
{
9+
name: 'Browser Action Popup',
10+
value: 'popup',
11+
short: 'popup',
12+
checked: true
13+
},
14+
{
15+
name: 'Options Page',
16+
value: 'options',
17+
short: 'options'
18+
},
19+
{
20+
name: 'Content Script',
21+
value: 'contentScript',
22+
short: 'content script'
23+
// },
24+
// {
25+
// name: 'Standalone Tab',
26+
// value: 'standalone',
27+
// short: 'standalone'
28+
// },
29+
// {
30+
// name: 'Dev Tools Tab',
31+
// value: 'devTools',
32+
// short: 'dev tools'
33+
}
34+
],
35+
filter: async (input) => {
36+
const componentMap = {}
37+
38+
input.forEach((component) => {
39+
componentMap[component] = true
40+
})
41+
42+
return componentMap
43+
}
1744
}
1845
]

0 commit comments

Comments
 (0)