-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
webpack.config.js
156 lines (152 loc) · 3.96 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
const autoprefixer = require('autoprefixer');
const CompressionPlugin = require('compression-webpack-plugin');
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const path = require('path');
const zopfli = require('@gfx/zopfli');
const nodeEnv = process.env.NODE_ENV || 'development';
const isProduction = nodeEnv.toLocaleLowerCase() === 'production';
const context = __dirname;
function removeSrcDirectory (data) {
let relativePath = path.relative(context, data.filename);
if (relativePath.startsWith('src' + path.sep)) {
relativePath = relativePath.slice(4);
}
return relativePath;
}
module.exports = {
context,
mode: isProduction ? 'production' : 'development',
devtool: isProduction ? 'source-map' : 'inline-source-map',
entry: {
'bundle': './src/scripts/main.jsx',
},
output: {
// TODO: add hashes for production (server needs to be able to handle them)
// filename: isProduction ? '[name]-[hash].js' : '[name].js',
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
sourceMapFilename: 'sourceMaps/[file].map',
},
resolve: {
extensions: ['.js', '.jsx', '.json'],
},
module: {
rules: [
{
test: /.*\.(png|svg|jpg|gif|ttc|ttf|woff|woff2|eot)$/,
type: 'asset/resource'
},
{
test: /\.css$/,
// Exclude legacy CSS from CSS modules pipeline
exclude: [
path.resolve(__dirname, 'src/css/styles.css'),
path.resolve(__dirname, 'src/css/diff.css'),
path.resolve(__dirname, 'node_modules')
],
use: [
{ loader: 'style-loader' },
{
loader: 'css-loader',
options: {
modules: {
localIdentName: '[path]___[name]__[local]___[hash:base64:5]',
},
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
postcssOptions: {
plugins: [
autoprefixer()
]
}
},
},
],
},
// TODO: remove this entire pipeline when all legacy CSS is refactored
{
test: /\.css$/,
type: 'asset/resource',
generator: {
filename: removeSrcDirectory
},
exclude: [
path.resolve(__dirname, 'node_modules')
],
// Only process legacy CSS files
include: [
path.resolve(__dirname, 'src/css/styles.css'),
path.resolve(__dirname, 'src/css/diff.css')
],
use: [
{
loader: 'extract-loader',
},
{
loader: 'css-loader',
options: {
import: false,
sourceMap: true
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
postcssOptions: {
plugins: [
autoprefixer()
]
}
},
},
],
},
{
test: /node_modules\/.*\.css$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
}
],
},
plugins: [
// Strip locales from Moment.js (we only use English)
new MomentLocalesPlugin()
],
};
// Production-specific additions
if (isProduction) {
module.exports.plugins.push(
new CompressionPlugin({
filename: '[path][base].gz[query]',
test: /\.(js|css|svg|map)$/i,
compressionOptions: {
numiterations: 15
},
algorithm (input, compressionOptions, callback) {
return zopfli.gzip(input, compressionOptions, callback);
}
})
);
}