forked from rainbow-me/browser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
144 lines (139 loc) Β· 3.9 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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const { join, resolve } = require('path');
require('dotenv').config();
const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const Dotenv = require('dotenv-webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { ProgressPlugin, ProvidePlugin, IgnorePlugin } = require('webpack');
const BundleAnalyzerPlugin =
require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const allowList = require('./static/allowlist.json');
const manifest = require('./static/manifest.json');
const manifestFilePath = resolve(__dirname, './build/manifest.json');
const optionalPlugins = [];
if (process.env.ANALYZE_BUNDLE === 'true') {
optionalPlugins.push(
new BundleAnalyzerPlugin({
analyzerMode: 'static',
generateStatsFile: true,
openAnalyzer: true,
}),
);
}
const manifestOverride = manifest;
manifestOverride.content_security_policy.extension_pages = `${
manifestOverride.content_security_policy.extension_pages
} ${allowList.urls.join(' ')};`;
module.exports = {
devtool: 'cheap-module-eval-source-map',
entry: {
background: './src/entries/background/index.ts',
contentscript: './src/entries/content/index.ts',
inpage: './src/entries/inpage/index.ts',
popup: './src/entries/popup/index.ts',
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true,
},
},
],
exclude: /node_modules/,
},
{
test: /\.(woff2|png|svg|mp3)?$/,
use: 'file-loader',
},
{
test: /\.worker.js$/,
loader: 'worker-loader',
},
{
test: /\.vanilla\.css$/i, // Targets only CSS files generated by vanilla-extract
use: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
url: false, // Required as image imports should be handled via JS/TS import statements
},
},
],
},
],
},
plugins: [
...optionalPlugins,
new IgnorePlugin({
checkResource(resource) {
return /.*\/wordlists\/(?!english).*\.json/.test(resource);
},
}),
new Dotenv({ allowEmptyValues: true }),
new HtmlWebpackPlugin({
chunks: ['popup'],
template: './src/entries/popup/index.html',
filename: 'popup.html',
}),
new CopyPlugin({
patterns: [{ from: 'static', to: './' }],
}),
new MiniCssExtractPlugin(),
new ProgressPlugin(),
new VanillaExtractPlugin(),
// Work around for Buffer is undefined:
// https://github.com/webpack/changelog-v5/issues/10
new ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
// Custom plugin to apply the sandbox
{
apply: (compiler) => {
compiler.hooks.afterEmit.tap('AfterEmitPlugin', () => {
if (
fs.writeFileSync(
manifestFilePath,
JSON.stringify(manifestOverride, null, 2),
)
) {
process.stdout.write('manifest overwritten successfuly');
} else {
process.stderr.write('manifest override failed');
}
});
},
},
],
resolve: {
alias: {
'~': resolve(__dirname, 'src/'),
static: resolve(__dirname, 'static/'),
},
fallback: {
fs: false,
tls: false,
net: false,
path: false,
zlib: false,
http: false,
stream: 'stream-browserify',
https: 'agent-base',
crypto: false,
},
extensions: ['.tsx', '.ts', '.js', '.json'],
},
output: {
filename: '[name].js',
path: join(__dirname, 'build'),
publicPath: '/',
},
};