-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.js
80 lines (75 loc) · 2.12 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
const path = require('path');
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ChromeExtensionReloader = require('webpack-chrome-extension-reloader');
const TerserPlugin = require('terser-webpack-plugin');
require('dotenv').config();
module.exports = (env, argv) => {
const isProductionRun = argv.mode === 'production';
return {
devtool: isProductionRun ? 'source-map' : 'cheap-source-map',
entry: {
background: './src/background.ts',
content: './src/content.ts',
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
},
],
},
resolve: {
extensions: ['.ts'],
},
optimization: {
minimizer: [new TerserPlugin()],
},
plugins: [
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false, // resolve conflict with `CopyWebpackPlugin`
}),
new CopyWebpackPlugin([
{
from: 'src/assets/_locales',
to: '_locales',
},
{
from: 'src/assets/icons',
to: 'icons',
},
{
from: 'src/assets/styles.css',
},
{
from: 'src/manifest.json',
transform(content, path) {
return processManifestFile(content, isProductionRun);
},
},
]),
new webpack.DefinePlugin({
__DEBUG__: isProductionRun
? JSON.stringify(false)
: JSON.stringify(true),
__CLIENT_ID__: JSON.stringify(process.env.CLIENT_ID),
__CLIENT_SECRET__: JSON.stringify(process.env.CLIENT_SECRET),
}),
argv.watch ? new ChromeExtensionReloader() : null,
].filter(plugin => !!plugin),
};
};
const processManifestFile = (manifest, removeUnnecessaryKey) => {
manifest = JSON.parse(manifest);
manifest.oauth2.client_id = process.env.CLIENT_ID;
if (removeUnnecessaryKey) {
delete manifest.key;
}
return JSON.stringify(manifest, null, 2);
};