-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwebpack.config.js
205 lines (195 loc) · 6.07 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
201
202
203
204
205
const path = require('path');
const fs = require('fs');
const { camelCase } = require('lodash');
const { getIfUtils, removeEmpty } = require('webpack-config-utils');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const ProgressBar = require('progress-bar-webpack-plugin');
const pkgJson = require('./package.json');
const ifDirIsNotEmpty = (dir, value) => {
return fs.readdirSync(dir).length !== 0 ? value : undefined;
};
/**
* @param SrcPath the folder/file name (eg 'popup') or the path relative to the 'src' dir (eg 'scripts/background.ts')
* @param value the value to return if the folder is found
*/
const ifDirExists = (SrcPath, value) => {
return fs.existsSync(path.join(__dirname, 'src', SrcPath))
? value
: undefined;
};
module.exports = (env) => {
const { ifProd, ifDev } = getIfUtils(env);
/**
* @param dirPath the path relative to src (eg 'scripts' not 'src/scripts')
*/
const getFolders = (dirPath) => {
return fs
.readdirSync(path.join(__dirname, 'src', dirPath), {
withFileTypes: true,
})
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
};
/**
* @param dirPath the path relative to src (eg 'scripts' not 'src/scripts')
* @param entryFile the entry point (eg 'index.ts' or 'index.tsx')
*/
const getEntries = (dirPath, entryFile = 'index.tsx') => {
const _e = {};
// get all folders
const folders = getFolders(dirPath);
folders.forEach((folderName) => {
_e[camelCase(folderName)] = path.join(
__dirname,
'src',
dirPath,
folderName,
entryFile,
);
});
return _e;
};
var fileExtensions = ["jpg", "jpeg", "png", "gif", "eot", "otf", "svg", "ttf", "woff", "woff2", "txt"];
return {
mode: ifProd('production', 'development'),
entry: removeEmpty({
popup: ifDirExists('popup', path.join(__dirname, 'src/popup/index.js')),
options: ifDirExists('options', './src/options/index.tsx'),
onboarding: ifDirExists('onboarding', './src/onboarding/index.tsx'),
newtab: ifDirExists('newtab', './src/newtab/index.tsx'),
serviceworker: ifDirExists('serviceworker/index.ts', {
import: './src/serviceworker/index.ts',
filename: 'serviceWorker.js',
}),
...getEntries('scripts', 'index.ts'),
}),
output: {
path: path.resolve(__dirname, 'build'),
filename: 'js/[name].js',
},
module: {
rules: [
{
test: /\.[jt]sx?$/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
['@babel/preset-react', { runtime: 'automatic' }],
'@babel/preset-typescript',
],
plugins: removeEmpty([ifDev('react-refresh/babel')]),
},
},
exclude: /node_modules/,
include: [path.resolve(__dirname, 'src')],
},
{
test: /\.(s[ac]|c)ss$/i,
use: removeEmpty([
ifProd(MiniCssExtractPlugin.loader, 'style-loader'),
'css-loader',
]),
},
{
test: new RegExp('\.(' + fileExtensions.join('|') + ')$'),
use: {
loader: "file-loader?name=[name].[ext]",
},
exclude: /node_modules/
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/i,
type: 'asset/resource',
generator: {
filename: 'fonts/[hash][ext][query]',
},
},
],
},
plugins: removeEmpty([
new CleanWebpackPlugin({
cleanStaleWebpackAssets: false, // don't remove index.html when using the flag watch
}),
ifProd(
new MiniCssExtractPlugin({
filename: 'css/[name].css',
}),
),
ifDirExists(
'popup',
new HtmlWebpackPlugin({
filename: 'popup.html',
template: 'src/popup/index.html',
chunks: ['popup'],
}),
),
ifDirExists(
'options',
new HtmlWebpackPlugin({
filename: 'options.html',
template: 'src/options/index.html',
chunks: ['options'],
}),
),
ifDirExists(
'newtab',
new HtmlWebpackPlugin({
filename: 'newtab.html',
template: 'src/newtab/index.html',
chunks: ['newtab'],
}),
),
ifDirExists(
'onboarding',
new HtmlWebpackPlugin({
filename: 'onboarding.html',
template: 'src/onboarding/index.html',
chunks: ['onboarding'],
}),
),
new CopyPlugin({
patterns: removeEmpty([
ifDirIsNotEmpty(path.join(__dirname, 'public', 'icons'), {
from: 'public/icons',
to: 'icons',
}),
{
from: 'public/manifest.json',
transform: (buffer) => {
const manifestJson = JSON.parse(buffer.toString());
return Buffer.from(JSON.stringify(manifestJson));
},
},
{
from: 'public/rules.json',
transform: (buffer) => {
const rulesJson = JSON.parse(buffer.toString());
return Buffer.from(JSON.stringify(rulesJson));
},
}
]),
}),
ifDev(new ReactRefreshWebpackPlugin()),
ifProd(new ProgressBar()),
]),
resolve: {
extensions: ['.tsx', '.ts', '.js', '.jsx', 'svg', 'png'],
},
devtool: ifProd(false, 'source-map'),
devServer: {
// index: 'index.html', // The filename that is considered the index file.
port: 3003,
host: 'localhost',
open: true, // open the browser after server had been started
compress: true,
// overlay: true, // show compiler errors in the browser
static: path.join(__dirname, 'public'),
},
};
};