forked from adambullmer/vue-cli-plugin-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (132 loc) · 5.39 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const logger = require('@vue/cli-shared-utils')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ExtensionReloader = require('webpack-extension-reloader')
const ZipPlugin = require('zip-webpack-plugin')
const { keyExists } = require('./lib/signing-key')
const manifestTransformer = require('./lib/manifest')
const defaultOptions = {
components: {},
componentOptions: {},
extensionReloaderOptions: {},
manifestSync: ['version'],
manifestTransformer: null
}
const performanceAssetFilterList = [
(file) => !/\.map$/.test(file),
(file) => !file.endsWith('.zip'),
(file) => !/^icons\//.test(file)
]
module.exports = (api, options) => {
const appRootPath = api.getCwd()
const pluginOptions = options.pluginOptions.browserExtension
? Object.assign(defaultOptions, options.pluginOptions.browserExtension)
: defaultOptions
const componentOptions = pluginOptions.componentOptions
const extensionReloaderOptions = pluginOptions.extensionReloaderOptions
const packageJson = require(api.resolve('package.json'))
const isProduction = process.env.NODE_ENV === 'production'
const keyFile = api.resolve('key.pem')
const hasKeyFile = keyExists(keyFile)
const contentScriptEntries = Object.keys((componentOptions.contentScripts || {}).entries || {})
if (pluginOptions.modesToZip !== undefined) {
logger.warn('Deprecation Notice: setting NODE_ENV should be used in favored of options.modesToZip')
}
const entry = {}
const entries = {}
if (componentOptions.background) {
entries.background = 'background'
entry.background = [api.resolve(componentOptions.background.entry)]
}
if (componentOptions.contentScripts) {
entries.contentScript = contentScriptEntries
for (const name of contentScriptEntries) {
let paths = componentOptions.contentScripts.entries[name]
if (!Array.isArray(paths)) {
paths = [paths]
}
entry[name] = paths.map((path) => api.resolve(path))
}
}
const userScripts = Object.keys(entry)
api.chainWebpack((webpackConfig) => {
const isLegacyBundle = process.env.VUE_CLI_MODERN_MODE && !process.env.VUE_CLI_MODERN_BUILD
// Ignore rewriting names for background and content scripts
webpackConfig.output.filename((file) =>
`js/[name]${isLegacyBundle ? `-legacy` : ``}${isProduction && options.filenameHashing && !userScripts.includes(file.chunk.name) ? '.[contenthash:8]' : ''}.js`
)
webpackConfig.merge({ entry })
webpackConfig.plugin('copy-manifest').use(CopyWebpackPlugin, [
[
{
from: './src/manifest.json',
to: 'manifest.json',
transform: manifestTransformer(api, pluginOptions, packageJson)
}
]
])
webpackConfig.plugin('provide-webextension-polyfill').use(webpack.ProvidePlugin, [
{
browser: 'webextension-polyfill'
}
])
// Workaround for https://github.com/mozilla/webextension-polyfill/issues/68
webpackConfig.module
.rule('provide-webextension-polyfill')
.test(require.resolve('webextension-polyfill', { paths: [appRootPath] }))
.use('imports')
.loader('imports-loader')
.options({ browser: '>undefined' })
if (isProduction) {
// Silence warnings of known large files, like images, sourcemaps, and the zip artifact
webpackConfig.performance.assetFilter((assetFilename) =>
performanceAssetFilterList.every((filter) => filter(assetFilename))
)
if (hasKeyFile) {
webpackConfig.plugin('copy-signing-key').use(CopyWebpackPlugin, [[{ from: keyFile, to: 'key.pem' }]])
} else {
logger.warn('No `key.pem` file detected. This is problematic only if you are publishing an existing extension')
}
}
if (isProduction) {
let filename
if (pluginOptions.artifactFilename) {
filename = pluginOptions.artifactFilename({
name: packageJson.name,
version: packageJson.version,
mode: api.service.mode
})
} else {
filename = `${packageJson.name}-v${packageJson.version}-${api.service.mode}.zip`
}
webpackConfig.plugin('zip-browser-extension').use(ZipPlugin, [
{
path: api.resolve(pluginOptions.artifactsDir || 'artifacts'),
filename: filename
}
])
}
// configure webpack-extension-reloader for automatic reloading of extension when content and background scripts change (not HMR)
// enabled only when webpack mode === 'development'
if (!isProduction) {
webpackConfig.plugin('extension-reloader').use(ExtensionReloader, [{ entries, ...extensionReloaderOptions }])
}
if (webpackConfig.plugins.has('copy')) {
webpackConfig.plugin('copy').tap(args => {
args[0].patterns[0].globOptions.ignore.push('browser-extension.html')
return args
})
}
})
api.configureWebpack((webpackConfig) => {
const omitUserScripts = ({ name }) => !userScripts.includes(name)
if (webpackConfig.optimization && webpackConfig.optimization.splitChunks && webpackConfig.optimization.splitChunks.cacheGroups) {
if (webpackConfig.optimization.splitChunks.cacheGroups.vendors) {
webpackConfig.optimization.splitChunks.cacheGroups.vendors.chunks = omitUserScripts
}
if (webpackConfig.optimization.splitChunks.cacheGroups.common) {
webpackConfig.optimization.splitChunks.cacheGroups.common.chunks = omitUserScripts
}
}
})
}