-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.ts
313 lines (291 loc) · 8.55 KB
/
webpack.config.ts
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import fs from 'fs'
import path from 'path'
import webpack from 'webpack'
import FilemanagerPlugin from 'filemanager-webpack-plugin'
import TerserPlugin from 'terser-webpack-plugin'
// eslint-disable-next-line import/default
import CopyWebpackPlugin from 'copy-webpack-plugin'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import { CleanWebpackPlugin } from 'clean-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import WextManifestWebpackPlugin from 'wext-manifest-webpack-plugin'
import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin'
type TargetBrowser = `chrome` | `firefox` | `opera`
const viewsPath = path.join(__dirname, `views`)
const sourcePath = path.join(__dirname, `src`)
const destinationPath = path.join(__dirname, `extension`)
const generatedAssetsPath = path.join(__dirname, `generated`)
const nodeEnvironment = process.env.NODE_ENV || `development`
const targetBrowser = process.env.TARGET_BROWSER as TargetBrowser
// const extensionReloaderPlugin =
// nodeEnvironment === `development`
// ? new (ExtensionReloader as any)({
// entries: {
// background: `background`,
// // TODO: reload manifest on update
// contentScript: `contentScript`,
// extensionPage: [`popup`, `options`, `massCitations`, `guide`],
// },
// port: 9090,
// reloadPage: true,
// })
// : () => {}
const getExtensionFileType = (browser: string) => {
if (browser === `opera`) {
return `crx`
}
if (browser === `firefox`) {
return `xpi`
}
return `zip`
}
if(!fs.existsSync(destinationPath)){
fs.mkdirSync(destinationPath)
}
if(!fs.existsSync(generatedAssetsPath)){
fs.mkdirSync(generatedAssetsPath)
}
const WebpackConfig = {
devtool: `source-map`,
entry: {
background: path.join(sourcePath, `Background.tsx`),
contentScript: path.join(sourcePath, `ContentScript`, `index.tsx`),
guide: path.join(sourcePath, `pages`, `Guide.tsx`),
manifest: path.join(sourcePath, `manifest.json`),
massCitations: path.join(sourcePath, `pages`, `MassCitations`, `index.tsx`),
options: path.join(sourcePath, `Options`, `index.tsx`),
popup: path.join(sourcePath, `Popup`, `index.tsx`),
updates: path.join(sourcePath, `pages`, `Updates.tsx`),
},
mode: nodeEnvironment,
module: {
rules: [
{
exclude: /node_modules/,
// prevent webpack handling json with its own loaders,
test: /manifest\.json$/,
type: `javascript/auto`,
use: {
loader: `wext-manifest-loader`,
options: {
usePackageJSONVersion: true, // set to false to not use package.json version for manifest
},
},
},
{
exclude: /node_modules/,
loader: `babel-loader`,
test: /\.(js|ts)x?$/,
},
{
exclude: /node_modules/,
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader, // It creates a CSS file per JS file which contains CSS
},
{
loader: `css-loader`, // Takes the CSS files and returns the CSS with imports and url(...) for Webpack
options: {
sourceMap: true,
},
},
{
loader: `postcss-loader`,
options: {
postcssOptions: {
plugins: [
[
`autoprefixer`,
{
// Options
},
],
],
},
sourceMap: true,
},
},
{
loader: `resolve-url-loader`, // Rewrites relative paths in url() statements
options: {
sourceMap: true,
},
},
],
},
{
generator: {
filename: `assets/fonts/[hash][ext][query]`,
},
test: /\.ttf$/,
type: `asset/resource`,
},
{
generator: {
filename: `assets/icons/[hash][ext][query]`,
},
test: /\.svg$/,
type: `asset/resource`,
},
{
generator: {
filename: `assets/[hash][ext][query]`,
},
test: /\.png$/,
type: `asset/resource`,
},
],
},
output: {
filename: `js/[name].bundle.js`,
path: path.join(destinationPath, targetBrowser),
publicPath: ``,
},
plugins: [
// Plugin to not generate js bundle for manifest entry
new WextManifestWebpackPlugin(),
new ForkTsCheckerWebpackPlugin(),
// environmental variables
new webpack.EnvironmentPlugin([`NODE_ENV`, `TARGET_BROWSER`]),
// delete previous build files
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: [
path.join(process.cwd(), `extension/${targetBrowser}`),
path.join(
process.cwd(),
`extension/${targetBrowser}.${getExtensionFileType(targetBrowser)}`,
),
],
cleanStaleWebpackAssets: false,
verbose: true,
}),
new HtmlWebpackPlugin({
chunks: [`background`],
filename: `background.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `background.html`),
}),
new HtmlWebpackPlugin({
chunks: [`popup`],
filename: `popup.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `popup.html`),
}),
new HtmlWebpackPlugin({
chunks: [`options`],
filename: `options.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `options.html`),
}),
new HtmlWebpackPlugin({
chunks: [`updates`],
filename: `updates.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `updates.html`),
}),
new HtmlWebpackPlugin({
chunks: [`massCitations`],
filename: `mass-citations.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `mass-citations.html`),
}),
new HtmlWebpackPlugin({
chunks: [`guide`],
filename: `guide.html`,
hash: true,
inject: `body`,
template: path.join(viewsPath, `guide.html`),
}),
// write css file(s) to build folder
new MiniCssExtractPlugin({ filename: `css/[name].css` }),
// copy static assets
new CopyWebpackPlugin({
patterns: [
{ from: `${generatedAssetsPath}`, to: `assets` },
{ from: path.join(__dirname, `assets`, `clerkent.png`), to: `assets` },
{ from: path.join(__dirname, `assets`, `icj.png`), to: `assets` },
{ from: path.join(__dirname, `assets`, `ecthr.png`), to: `assets` },
{ from: path.join(__dirname, `assets`, `chrome_toolbar_screenshot.png`), to: `assets` },
],
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || `development`),
}),
// plugin to enable browser reloading in development mode
// extensionReloaderPlugin,
],
resolve: {
/* eslint-disable sort-keys-fix/sort-keys-fix */
alias: {
react: `preact/compat`,
"react-dom/test-utils": `preact/test-utils`,
"react-dom": `preact/compat`,
"react/jsx-runtime": `preact/jsx-runtime`,
'webextension-polyfill-ts': path.resolve(
path.join(__dirname, `node_modules`, `webextension-polyfill-ts`),
),
},
/* eslint-enable sort-keys-fix/sort-keys-fix */
extensions: [`.ts`, `.tsx`, `.js`, `.json`],
fallback: {
http: false,
https: false,
timers: false,
url: false,
},
modules: [
path.resolve(__dirname, `src`),
`node_modules`,
],
},
stats: {
all: false,
builtAt: true,
errors: true,
hash: true,
},
...(nodeEnvironment === `production` ? ({
optimization: {
minimize: true,
minimizer: [
new TerserPlugin({
extractComments: false,
parallel: true,
terserOptions: {
format: {
comments: false,
},
},
}),
new FilemanagerPlugin({
events: {
onEnd: {
archive: [
{
destination: `${path.join(destinationPath, targetBrowser)}.${getExtensionFileType(targetBrowser)}`,
format: `zip`,
options: { zlib: { level: 6 } },
source: path.join(destinationPath, targetBrowser),
},
],
},
},
}),
],
},
}) : {}),
watchOptions: {
ignored: [
`node_modules/**`,
`extension/**`,
],
poll: 1000,
},
}
export default WebpackConfig