-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
179 lines (174 loc) · 5.5 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
'use strict';
const path = require('path');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackInjectPreload = require('@principalstudio/html-webpack-inject-preload');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
// const os = require('os');
// DEPLOY_PATH is set by the s3-deploy-action its value will be:
// `branch/[branch-name]/` or `version/[tag-name]/`
// See the following documentation for more detail:
// https://github.com/concord-consortium/s3-deploy-action/blob/main/README.md#top-branch-example
const DEPLOY_PATH = process.env.DEPLOY_PATH;
module.exports = (env, argv) => {
const devMode = argv.mode !== 'production';
return {
context: __dirname, // to automatically find tsconfig.json
devServer: {
static: 'dist',
hot: true,
},
devtool: devMode ? 'eval-cheap-module-source-map' : 'source-map',
entry: './src/index.tsx',
mode: 'development',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'assets/index.[contenthash].js',
},
performance: { hints: false },
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader'
},
// This code coverage instrumentation should only be added when needed. It makes
// the code larger and slower
process.env.CODE_COVERAGE ? {
test: /\.[tj]sx?$/,
loader: 'istanbul-instrumenter-loader',
options: { esModules: true },
enforce: 'post',
exclude: path.join(__dirname, 'node_modules'),
} : {},
{
test: /\.(sa|sc|le|c)ss$/i,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
modules: {
localIdentName: `[name]-[local]`
},
sourceMap: true,
importLoaders: 1
}
},
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [autoprefixer()]
}
}
},
'sass-loader'
]
},
{
test: /\.(png|woff|woff2|eot|ttf|mp3)$/,
type: 'asset/resource'
},
{ // disable svgo optimization for files ending in .nosvgo.svg
test: /\.nosvgo\.svg$/i,
loader: '@svgr/webpack',
options: {
svgo: false
}
},
// {
// test: /\.mp3$/,
// loader: 'file-loader'
// },
{
test: /\.svg$/i,
exclude: /\.nosvgo\.svg$/i,
oneOf: [
{
// Do not apply SVGR import in CSS files.
issuer: /\.(css|scss|less)$/,
type: 'asset'
},
{
issuer: /\.tsx?$/,
loader: '@svgr/webpack',
options: {
svgoConfig: {
plugins: [
{
// cf. https://github.com/svg/svgo/releases/tag/v2.4.0
name: 'preset-default',
params: {
overrides: {
// don't minify "id"s (i.e. turn randomly-generated unique ids into "a", "b", ...)
// https://github.com/svg/svgo/blob/master/plugins/cleanupIDs.js
cleanupIDs: { minify: false },
// leave <line>s, <rect>s and <circle>s alone
// https://github.com/svg/svgo/blob/master/plugins/convertShapeToPath.js
convertShapeToPath: false,
// leave "class"es and "id"s alone
// https://github.com/svg/svgo/blob/master/plugins/prefixIds.js
prefixIds: false,
// leave "stroke"s and "fill"s alone
// https://github.com/svg/svgo/blob/master/plugins/removeUnknownsAndDefaults.js
removeUnknownsAndDefaults: { defaultAttrs: false },
}
}
}
]
}
}
}
]
}
]
},
resolve: {
extensions: [ '.ts', '.tsx', '.js' ]
},
stats: {
// suppress "export not found" warnings about re-exported types
warningsFilter: /export .* was not found in/
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
favicon: 'src/public/favicon.ico',
publicPath: '.',
templateParameters: {
language: 'en'
},
}),
new HtmlWebpackPlugin({
filename: 'index-es.html',
template: 'src/index.html',
favicon: 'src/public/favicon.ico',
publicPath: '.',
templateParameters: {
language: 'es'
},
}),
new HtmlWebpackInjectPreload({
// Preload PNG and SVG images.
files: [
{
match: /\.(png|svg)$/,
attributes: {as: 'image'},
}
]
}),
...(DEPLOY_PATH ? [new HtmlWebpackPlugin({
filename: 'index-top.html',
template: 'src/index.html',
favicon: 'src/public/favicon.ico',
publicPath: DEPLOY_PATH,
templateParameters: {
language: 'en'
},
})] : []),
new CleanWebpackPlugin(),
]
};
};