-
Notifications
You must be signed in to change notification settings - Fork 25
/
webpack.config.js
200 lines (195 loc) · 5.66 KB
/
webpack.config.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const path = require('path');
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const WebpackBar = require('webpackbar');
const SVGSpritemapPlugin = require('svg-spritemap-webpack-plugin');
module.exports = (env, arg) => {
const isDev = arg.mode === 'development'
return {
mode: arg.mode,
experiments: {
topLevelAwait: true
},
stats: {
assets: false
},
devtool: arg.mode === 'development' ? 'inline-source-map' : false,
entry: {
newtab: './src/js/newtab.js',
options: './src/js/options.js',
background: './src/js/background.js',
theme: './src/js/theme.js',
},
output: {
path: path.join(__dirname, process.env.BROWSER === 'firefox' ? '/extension_firefox' : '/extension_chrome'),
filename(pathData) {
return pathData.chunk.name === 'background' ? '[name].js' : 'js/[name].js';
},
chunkFilename: 'js/[name].js'
},
resolve: {
modules: ['node_modules']
},
module: {
rules: [
{
test: /\.html$/,
include: path.resolve(__dirname, 'src/js/components'),
use: {
loader: 'html-loader',
options: {
minimize: true,
sources: false
}
}
},
{
test: /\.js$/,
exclude: [/node_modules/],
// include: path.resolve(__dirname, 'src/js/components'),
use: [{
loader: 'babel-loader',
options: { presets: ['@babel/env'] }
}]
},
{
// ccs-loader for web-components
test: /vb-.*\/*\.css$/,
exclude: [/node_modules/],
use: [
{
loader: 'css-loader',
options: {
url: false
}
},
'postcss-loader'
]
},
{
test: /(newtab|options)\.css$/,
exclude: [/node_modules/],
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
url: false
}
},
'postcss-loader'
]
}
]
},
optimization: {
// splitChunks: {
// cacheGroups: {
// // defaultVendors: {
// // test: /[\\/]node_modules[\\/]/,
// // priority: -10,
// // chunks: 'initial',
// // }
// // vbComponents: {
// // test: /[\\/]src[\\/]js[\\/]components[\\/]vb-[a-z]+[\\/]/,
// // test: path.resolve(__dirname, 'src/js/components/vb-popup'),
// // name: 'vb-components',
// // chunks: 'all',
// // minSize: 10000,
// // reuseExistingChunk: true,
// // }
// }
// },
minimizer: [
new TerserPlugin({
// do not extract to separate file
extractComments: false,
terserOptions: {
output: { comments: /@?license/i, },
compress: { passes: 1 }
}
})
]
},
plugins: [
new WebpackBar(),
new CleanWebpackPlugin({
verbose: false,
cleanStaleWebpackAssets: false
}),
new CopyWebpackPlugin({
patterns: [
{
from: 'static',
transform(content, path) {
if (process.env.BROWSER === 'firefox' && path.includes('manifest.json')) {
const manifest = JSON.parse(content.toString());
delete manifest.background.service_worker
delete manifest.browser_action;
delete manifest.options_page;
delete manifest.minimum_chrome_version
manifest.background.scripts = ['background.js'];
manifest.permissions = manifest.permissions.filter(p => p !== 'background');
manifest.browser_specific_settings = {
gecko: {
id: '{876119d0-ddb9-47bb-9620-bc8d2489e857}',
strict_min_version: '128.0'
}
}
return JSON.stringify(manifest, null, 2);
}
return content
}
}
]
}),
new SVGSpritemapPlugin(`./src/icons/**/*.svg`, {
output: {
filename: 'img/symbol.svg',
svgo: {
plugins: [
'removeStyleElement',
{
name: 'removeAttrs',
params: {
attrs: 'class|style'
}
}
]
}
},
sprite: {
prefix: false
}
}),
new MiniCssExtractPlugin({
filename: 'css/[name].css'
}),
...['newtab', 'options'].map(name => {
return new HtmlWebpackPlugin({
template: `./src/${name}.html`,
filename: `${name}.html`,
scriptLoading: 'blocking',
minify: {
collapseWhitespace: true,
removeComments: true,
removeScriptTypeAttributes: true
},
chunks: [name]
})
}),
new webpack.EnvironmentPlugin({
BROWSER: 'chrome'
}),
process.env.BROWSER !== 'firefox' && new webpack.BannerPlugin({
banner: 'if (typeof browser === "undefined") { browser = chrome; }',
raw: true,
entryOnly: false
})
]
}
};