-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
144 lines (131 loc) · 5.09 KB
/
webpack.config.js
File metadata and controls
144 lines (131 loc) · 5.09 KB
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
// Safari and Firefox both imply MV2
const VALID_BUILD_TARGETS = ['chrome-mv2', 'chrome-mv3', 'safari', 'firefox'];
const package = require('./package.json');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { DefinePlugin } = require('webpack');
const isWebkitOrBlink = target => target !== 'firefox';
const supportsManifestV3 = target => target === 'chrome-mv3';
const supportsExtensionNotifications = target => ['chrome-mv2', 'firefox'].includes(target);
const getResolveConfig = target => { return {
alias: {
'get-browser$': isWebkitOrBlink(target)
? path.resolve(__dirname, './src/common/get-browser-chrome.js')
: path.resolve(__dirname, './src/common/get-browser-browser.js'),
'display-notifications$': supportsExtensionNotifications(target)
? path.resolve(__dirname, './src/chrome-mv2-firefox/display-notifications.js')
: path.resolve(__dirname, './src/chrome-mv3-safari/display-notifications.js'),
'create-context-menu$': isWebkitOrBlink(target)
? path.resolve(__dirname, './src/chrome-mv2/create-context-menu.js')
: path.resolve(__dirname, './src/firefox/create-context-menu.js'),
'setup-message-handling$': ['chrome-mv2', 'firefox'].includes(target)
? path.resolve(__dirname, './src/chrome-mv2-firefox/setup-message-handling.js')
: path.resolve(__dirname, './src/chrome-mv3-safari/setup-message-handling.js'),
'startup-background-script$': ['chrome-mv2', 'firefox'].includes(target)
? path.resolve(__dirname, './src/chrome-mv2-firefox/startup-background-script.js')
: path.resolve(__dirname, './src/chrome-mv3-safari/startup-background-script.js'),
'get-browser-action$': supportsManifestV3(target)
? path.resolve(__dirname, './src/chrome-mv3-safari/get-browser-action.js')
: path.resolve(__dirname, './src/chrome-mv2-firefox/get-browser-action.js'),
'tab-send-message$': target === 'firefox'
? path.resolve(__dirname, './src/firefox/tab-send-message.js')
: path.resolve(__dirname, './src/chrome-mv2-mv3-safari/tab-send-message.js')
}
} };
const getManifestPath = target => supportsManifestV3(target) ? './src/common/manifest-v3.json' : './src/common/manifest-v2.json';
module.exports = env => {
if (!env.build_target || !VALID_BUILD_TARGETS.includes(env.build_target)) {
throw new Error(`Invalid or no build target given. Expected one of: ${VALID_BUILD_TARGETS.join(', ')} - --env build_target=...`);
}
const mode = env.hasOwnProperty('environment') && env.environment === 'production' ? 'production' : 'development';
const destPath = mode == 'production'
? path.resolve(__dirname, ['build', env.build_target, package.version].join('-'))
: path.resolve(__dirname, 'dist', env.build_target)
;
const generateExtensionManifest = content => {
const manifest = JSON.parse(content);
manifest.name = package.name;
manifest.version = package.version;
manifest.description = package.description;
if (env.build_target === 'firefox') {
manifest.permissions.push('menus');
} else {
delete manifest.browser_specific_settings;
}
return JSON.stringify(manifest, null, 2);
};
return {
mode: 'production',
optimization: {
minimize: mode === 'production',
// don't generate license.txt files -- https://github.com/webpack-contrib/terser-webpack-plugin/issues/229
minimizer: [new TerserPlugin({
extractComments: false,
})],
},
resolve: getResolveConfig(env.build_target),
entry: {
index: './src/common/index.js',
popup: './src/common/popup.js',
options: './src/common/options.js',
background: './src/common/background.js',
},
output: {
filename: '[name].js',
path: destPath,
clean: true
},
plugins: [
new DefinePlugin({
PACKAGE_NAME: JSON.stringify(package.name),
PACKAGE_VERSION: JSON.stringify(package.version),
BUILD_TARGET: JSON.stringify(env.build_target),
environment: JSON.stringify({
isDevelopment: mode === 'development',
isProduction: mode === 'production',
name: mode
})
}),
new HtmlWebpackPlugin({
filename: 'options.html',
minify: false,
chunks: ['options'],
template: './src/common/options.html'
}),
new HtmlWebpackPlugin({
filename: 'popup.html',
minify: false,
chunks: ['popup'],
template: './src/common/popup.html'
}),
new CopyWebpackPlugin({
patterns: [
{
from: getManifestPath(env.build_target),
to: "./manifest.json",
transform(content, _path) {
return generateExtensionManifest(content.toString());
}
},
{
from: './icons/*'
}
]
}),
],
module: {
rules: [
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
],
},
}
};