-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
204 lines (191 loc) · 5.29 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
/* eslint-env node */
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// const CopyPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { EnvironmentPlugin, DefinePlugin } = require('webpack');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const pkg = require('./package.json');
// const { preprocess } = require('./svelte.config');
const devMode = process.env.NODE_ENV !== 'production';
// see https://webpack.js.org/plugins/mini-css-extract-plugin/#extracting-all-css-in-a-single-file
function recursiveIssuer(m, c) {
const issuer = c.moduleGraph.getIssuer(m);
// For webpack@4 issuer = m.issuer
if (issuer) {
return recursiveIssuer(issuer, c);
}
const chunks = c.chunkGraph.getModuleChunks(m);
// For webpack@4 chunks = m._chunks
for (const chunk of chunks) {
return chunk.name;
}
return false;
}
module.exports = () => {
return {
target: devMode ? 'web' : undefined,
entry: {
wrapper: './src/wrapper/index.js',
bundle: './src/index.js',
},
output: devMode
? {
path: path.resolve(__dirname, 'public'),
publicPath: '/',
}
: {
path: path.resolve(__dirname, 'public'),
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].js',
},
resolve: {
alias: {
svelte: path.resolve('node_modules', 'svelte'),
},
extensions: ['.ts', '.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'module', 'browser', 'main'],
},
optimization: {
minimizer: [
// For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line
`...`,
new CssMinimizerPlugin(),
],
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
type: 'css/mini-extract',
test: (m, c, entry = 'bundle') => m.constructor.name === 'CssModule' && recursiveIssuer(m, c) === entry,
// For webpack@4
// test: /\.css$/,
chunks: 'all',
enforce: true,
},
},
},
},
module: {
rules: [
!devMode && {
test: /\.m?js$/,
exclude: /node_modules[\\/](?!(svelte|mapbox-gl)[\\/])/,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false,
},
},
],
},
{
test: /\.svelte$/,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false,
},
},
{
loader: 'svelte-loader',
options: {
// preprocess,
compilerOptions: {
dev: devMode,
},
hotReload: devMode,
emitCss: true, // !devMode,
},
},
].slice(devMode ? 1 : 0),
},
{
test: /\.tsx?$/,
use: [
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
cacheCompression: false,
},
},
{
loader: 'ts-loader',
},
].slice(devMode ? 1 : 0),
},
{
// required to prevent errors from Svelte on Webpack 5+, omit on Webpack 4
test: /node_modules\/svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.(sass|css|scss)$/i,
use: [
devMode
? 'style-loader'
: {
loader: MiniCssExtractPlugin.loader,
options: {
// esModule: false,
},
},
'css-loader',
'sass-loader',
],
},
{
test: /\.(png|jpg|gif|svg|jpeg)$/i,
use: [
{
loader: 'url-loader',
options: {
limit: 8192,
},
},
],
},
{
test: /\.(txt|csv|tsv)$/i,
use: 'raw-loader',
},
].filter(Boolean),
},
devServer: {
static: {
directory: path.join(__dirname, 'public'),
publicPath: '/',
watch: true,
},
host: 'localhost',
hot: devMode,
},
plugins: [
devMode ? null : new CleanWebpackPlugin(),
new DefinePlugin({
__VERSION__: JSON.stringify(pkg.version),
}),
new EnvironmentPlugin({
COVIDCAST_ENDPOINT_URL: 'https://api.covidcast.cmu.edu/epidata/api.php',
}),
new HtmlWebpackPlugin({
title: 'COVIDcast Classic',
template: './src/index.html',
}),
!devMode &&
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
ignoreOrder: true,
chunkFilename: '[name].[contenthash].css',
}),
].filter(Boolean),
};
};