Skip to content

Commit 18872cf

Browse files
committed
Add WebExtension polyfill option
1 parent 4c893fb commit 18872cf

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

generator/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ module.exports = (api, options) => {
2929
}
3030
}
3131

32+
if (options.usePolyfill) {
33+
pkg.dependencies['webextension-polyfill'] = '^0.3.0'
34+
pkg.devDependencies = {
35+
'imports-loader': '^0.8.0'
36+
}
37+
}
38+
3239
if (api.hasPlugin('eslint')) {
3340
console.log('Adding eslint config stuffs')
3441
pkg.eslintConfig = eslintConfig

generator/template/base-app/src/background.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import store from './store'
2+
<%_ if (options.usePolyfill && !options.autoImportPolyfill) { -%>
3+
import browser from 'webextension-polyfill'
4+
<%_ } -%>
25

36
<%_ if (options.popupPage) { -%>
47
<%- options.api %>.runtime.onMessage.addListener(function (request, sender, sendResponse) {<%_ } else { -%>

generator/template/popup/src/popup/router/pages/Index.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
</template>
44

55
<script>
6+
<%_ if (options.usePolyfill && !options.autoImportPolyfill) { -%>
7+
import browser from 'webextension-polyfill'
8+
<%_ } -%>
9+
610
export default {
711
data () {
812
return {}

generator/template/standalone/src/standalone/router/pages/Index.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
</template>
44

55
<script>
6+
<%_ if (options.usePolyfill && !options.autoImportPolyfill) { -%>
7+
import browser from 'webextension-polyfill'
8+
<%_ } -%>
9+
610
export default {
711
data () {
812
return {}

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path')
22
const fs = require('fs')
33
const { exec } = require('child_process')
44
const logger = require('@vue/cli-shared-utils')
5+
const webpack = require('webpack')
56
const CopyWebpackPlugin = require('copy-webpack-plugin')
67
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader')
78
const ZipPlugin = require('zip-webpack-plugin')
@@ -32,6 +33,18 @@ module.exports = (api, options) => {
3233
webpackConfig.output.chunkFilename = 'js/[id].[name].js?[hash:8]'
3334
webpackConfig.node.global = false
3435

36+
if (pluginOptions.autoImportPolyfill) {
37+
webpackConfig.plugins.push(new webpack.ProvidePlugin({
38+
'browser': 'webextension-polyfill'
39+
}))
40+
41+
// Workaround for https://github.com/mozilla/webextension-polyfill/issues/68
42+
webpackConfig.module.rules.push({
43+
test: require.resolve('webextension-polyfill', {paths: [appRootPath]}),
44+
use: 'imports-loader?browser=>undefined'
45+
})
46+
}
47+
3548
if (isProduction) {
3649
if (hasKeyFile) {
3750
webpackConfig.plugins.push(new CopyWebpackPlugin([{ from: keyFile, to: 'key.pem' }]))

prompts.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,19 @@ module.exports = [
5757
value: 'chrome'
5858
}
5959
]
60+
},
61+
{
62+
name: 'usePolyfill',
63+
type: 'confirm',
64+
message: 'Add WebExtension polyfill?',
65+
default: true,
66+
when: answers => answers.api === 'browser'
67+
},
68+
{
69+
name: 'autoImportPolyfill',
70+
type: 'confirm',
71+
message: 'Make polyfill available without import?',
72+
default: true,
73+
when: answers => answers.usePolyfill
6074
}
6175
]

0 commit comments

Comments
 (0)