This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
forked from adambullmer/vue-cli-plugin-browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
122 lines (112 loc) · 4.12 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
const logger = require('@vue/cli-shared-utils')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const ZipPlugin = require('zip-webpack-plugin')
const manifestTransformer = require('./lib/manifest')
const defaultOptions = {
components: {},
componentOptions: {},
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 packageJson = require(api.resolve('package.json'))
const isProduction = process.env.NODE_ENV === 'production'
const contentScriptEntries = Object.keys((componentOptions.contentScripts || {}).entries || {})
const entry = {}
if (componentOptions.background) {
entry.background = [api.resolve(componentOptions.background.entry)]
}
if (componentOptions.contentScripts) {
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 })
// Workaround for /~https://github.com/mozilla/webextension-polyfill/issues/68
webpackConfig.plugin('copy-manifest').use(CopyWebpackPlugin, [
{
patterns: [
{
from: './src/manifest.json',
to: 'manifest.json',
transform: manifestTransformer(api, pluginOptions, packageJson),
},
],
},
])
webpackConfig.plugin('provide-webextension-polyfill').use(webpack.ProvidePlugin, [
{
browser: 'webextension-polyfill',
},
])
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 (pluginOptions.modesToZip) {
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,
},
])
}
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.defaultVendors) {
webpackConfig.optimization.splitChunks.cacheGroups.defaultVendors.chunks = omitUserScripts
}
if (webpackConfig.optimization.splitChunks.cacheGroups.common) {
webpackConfig.optimization.splitChunks.cacheGroups.common.chunks = omitUserScripts
}
}
})
}