forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
230 lines (226 loc) · 6.15 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
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
const path = require('path');
const webpack = require('webpack');
const CopyPlugin = require('copy-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const VersionPlugin = require('./build/version_plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webJsOptions = {
babelrc: false,
presets: [
[
'@babel/preset-env',
{
bugfixes: true,
useBuiltIns: 'entry',
corejs: 3,
},
],
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'module:nanohtml',
['@babel/plugin-proposal-class-properties', { loose: false }],
],
};
const serviceWorker = {
target: 'webworker',
entry: {
serviceWorker: './app/serviceWorker.js',
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.(png|jpg)$/,
loader: 'file-loader',
options: {
name: '[name].[contenthash:8].[ext]',
},
},
{
test: /\.svg$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[contenthash:8].[ext]',
},
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ removeViewBox: false }, // true causes stretched images
{ convertStyleToAttrs: true }, // for CSP, no unsafe-eval
{ removeTitle: true }, // for smallness
],
},
},
],
},
{
// loads all assets from assets/ for use by common/assets.js
test: require.resolve('./common/generate_asset_map.js'),
use: ['babel-loader', 'val-loader'],
},
],
},
plugins: [new webpack.IgnorePlugin(/\.\.\/dist/)],
};
const web = {
target: 'web',
entry: {
app: ['./app/main.js'],
// android: ['./android/android.js'],
// ios: ['./ios/ios.js']
},
output: {
chunkFilename: '[name].[contenthash:8].js',
filename: '[name].[contenthash:8].js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/,
oneOf: [
{
loader: 'babel-loader',
include: [
path.resolve(__dirname, 'app'),
path.resolve(__dirname, 'common'),
// some dependencies need to get re-babeled because we
// have different targets than their default configs
path.resolve(
__dirname,
'node_modules/@dannycoates/webcrypto-liner'
),
path.resolve(__dirname, 'node_modules/@fluent'),
path.resolve(__dirname, 'node_modules/intl-pluralrules'),
],
options: webJsOptions,
},
{
// Strip asserts from our deps, mainly choojs family
include: [path.resolve(__dirname, 'node_modules')],
exclude: [
path.resolve(__dirname, 'node_modules/crc'),
path.resolve(__dirname, 'node_modules/@fluent'),
path.resolve(__dirname, 'node_modules/@sentry'),
path.resolve(__dirname, 'node_modules/tslib'),
path.resolve(__dirname, 'node_modules/webcrypto-core'),
path.resolve(__dirname, 'node_modules/asmcrypto.js'),
],
loader: 'webpack-unassert-loader',
},
],
},
{
test: /\.(png|jpg)$/,
loader: 'file-loader',
options: {
name: '[name].[contenthash:8].[ext]',
},
},
{
test: /\.svg$/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[contenthash:8].[ext]',
},
},
{
loader: 'svgo-loader',
options: {
plugins: [
{ cleanupIDs: false },
{ removeViewBox: false }, // true causes stretched images
{ convertStyleToAttrs: true }, // for CSP, no unsafe-eval
{ removeTitle: true }, // for smallness
],
},
},
],
},
{
// creates style.css with all styles
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
'postcss-loader',
],
},
{
test: /\.ftl$/,
use: 'raw-loader',
},
{
// creates test.js for /test
test: require.resolve('./test/frontend/index.js'),
use: ['babel-loader', 'val-loader'],
},
{
// loads all assets from assets/ for use by common/assets.js
test: require.resolve('./common/generate_asset_map.js'),
use: ['babel-loader', 'val-loader'],
},
],
},
plugins: [
new CopyPlugin({
patterns: [
{
context: 'public',
from: '*.*',
},
],
}),
new webpack.EnvironmentPlugin(['NODE_ENV', 'TOKEN_AUTH_CLIENT_SALT']),
new webpack.IgnorePlugin(/\.\.\/dist/), // used in common/*.js
new MiniCssExtractPlugin({
filename: '[name].[contenthash:8].css',
}),
new VersionPlugin(), // used for the /__version__ route
new ManifestPlugin(), // used by server side to resolve hashed assets
],
devtool: 'source-map',
devServer: {
before:
process.env.NODE_ENV === 'development' && require('./server/bin/dev'),
compress: true,
hot: false,
host: '0.0.0.0',
proxy: {
'/api/ws': {
target: 'ws://localhost:1338',
ws: true,
secure: false,
},
},
},
};
module.exports = (env, argv) => {
const mode = argv.mode || 'production';
// eslint-disable-next-line no-console
console.error(`mode: ${mode}`);
process.env.NODE_ENV = web.mode = serviceWorker.mode = mode;
if (mode === 'development') {
// istanbul instruments the source for code coverage
webJsOptions.plugins.push('istanbul');
web.entry.tests = ['./test/frontend/index.js'];
}
return [web, serviceWorker];
};