-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.config.js
299 lines (279 loc) · 10.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const path = require('path');
// the list of .js6 entry point files
// in addition to being ES2015 JavaScript code, these may require() the .src.html and .less files to also be compiled into their own outputs
// tip: require()ing other stuff, or even having JavaScript code in the file, is typical but optional
// you could have a .js6 file which effectively only serves to create a bundle of third-party code or a shared stylesheet
const GLOBAL_JS6_FILES = [
// home page and peripheral pages
'./index.js6',
'./error.js6',
'./sitewide.js6',
'./global.js6',
'./patternlibrary/index.js6',
'./patternlibrary_htmltemplate/index.js6',
'./about/index.js6',
'./about/historical-data/index.js6',
'./about/friends-resources/index.js6',
'./metrics/index.js6',
'./metrics/efficiencygap/index.js6',
'./metrics/meanmedian/index.js6',
'./metrics/partisanbias/index.js6',
'./metrics/declination/index.js6',
'./2022-redistricting-review/index.js6',
];
const STATE_JS6_FILES = [
// per state pages, which really just use the same _statetemplate template
'./alabama/index.js6',
'./alaska/index.js6',
'./arizona/index.js6',
'./arkansas/index.js6',
'./california/index.js6',
'./colorado/index.js6',
'./connecticut/index.js6',
'./delaware/index.js6',
'./florida/index.js6',
'./georgia/index.js6',
'./hawaii/index.js6',
'./idaho/index.js6',
'./illinois/index.js6',
'./indiana/index.js6',
'./iowa/index.js6',
'./kansas/index.js6',
'./kentucky/index.js6',
'./louisiana/index.js6',
'./maine/index.js6',
'./maryland/index.js6',
'./massachusetts/index.js6',
'./michigan/index.js6',
'./minnesota/index.js6',
'./mississippi/index.js6',
'./missouri/index.js6',
'./montana/index.js6',
'./nebraska/index.js6',
'./nevada/index.js6',
'./new_hampshire/index.js6',
'./new_jersey/index.js6',
'./new_mexico/index.js6',
'./new_york/index.js6',
'./north_carolina/index.js6',
'./north_dakota/index.js6',
'./ohio/index.js6',
'./oklahoma/index.js6',
'./oregon/index.js6',
'./pennsylvania/index.js6',
'./rhode_island/index.js6',
'./south_carolina/index.js6',
'./south_dakota/index.js6',
'./tennessee/index.js6',
'./texas/index.js6',
'./utah/index.js6',
'./vermont/index.js6',
'./virginia/index.js6',
'./washington/index.js6',
'./west_virginia/index.js6',
'./wisconsin/index.js6',
'./wyoming/index.js6',
];
const LIBRARY_JS6_FILES = [
'./library/index.js6',
'./library/alabama/index.js6',
'./library/alaska/index.js6',
'./library/arizona/index.js6',
'./library/arkansas/index.js6',
'./library/california/index.js6',
'./library/colorado/index.js6',
'./library/delaware/index.js6',
'./library/florida/index.js6',
'./library/georgia/index.js6',
'./library/hawaii/index.js6',
'./library/idaho/index.js6',
'./library/illinois/index.js6',
'./library/indiana/index.js6',
'./library/iowa/index.js6',
'./library/kansas/index.js6',
'./library/kentucky/index.js6',
'./library/maine/index.js6',
'./library/maryland/index.js6',
'./library/massachusetts/index.js6',
'./library/michigan/index.js6',
'./library/minnesota/index.js6',
'./library/montana/index.js6',
'./library/nebraska/index.js6',
'./library/nevada/index.js6',
'./library/new_jersey/index.js6',
'./library/new_mexico/index.js6',
'./library/new_york/index.js6',
'./library/north_carolina/index.js6',
'./library/ohio/index.js6',
'./library/oklahoma/index.js6',
'./library/oregon/index.js6',
'./library/pennsylvania/index.js6',
'./library/rhode_island/index.js6',
'./library/south_carolina/index.js6',
'./library/south_dakota/index.js6',
'./library/texas/index.js6',
'./library/utah/index.js6',
'./library/virginia/index.js6',
'./library/washington/index.js6',
'./library/west_virginia/index.js6',
'./library/wisconsin/index.js6',
'./library/no_plans/index.js6'
]
const JS6_FILES = [
...GLOBAL_JS6_FILES,
...STATE_JS6_FILES,
...LIBRARY_JS6_FILES
]
/////////////////////////////////////////////////////////////////////////////////////////////////////////
const StringReplacePlugin = require("string-replace-webpack-plugin");
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const HTML_PARTIALS = {
footer: require("./htmlpartials/footer"),
navbar: require("./htmlpartials/navbar"),
headtags: require("./htmlpartials/head"),
};
module.exports = {
mode: 'development',
/*
* multiple entry points, one per entry
* the [name] for each is the basename, e.g. some/path/to/thing so we can add .js and .css suffixes
* the values are the files with their .js6 suffixes retained
*/
entry: JS6_FILES.reduce((o, key) => { o[key.replace(/\.js6$/, '')] = key; return o; }, {}),
output: {
path: path.resolve(__dirname, 'WEBSITE_OUTPUT'),
publicPath: "/WEBSITE_OUTPUT/",
chunkFilename: '[name].js',
},
module: {
rules: [
/*
* Plain JS files
* just kidding; Webpack already does those without any configuration :)
* but we do not want to lump them in with ES6 files: they would be third-party and then run through JSHint and we can't waste time linting third-party JS
*/
/*
* CSS files and also SASS-to-CSS all go into one bundled X.css
*/
{
test: /\.s[ac]ss$/i,
use: [
MiniCssExtractPlugin.loader,
// Translates CSS into CommonJS
"css-loader?url=false",
// Compiles Sass to CSS
"sass-loader",
],
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader?url=false"],
},
/*
* HTML Files
* replace [hash] entities in the .src.html to generate .html
* typically used on .js and .css filenames to include a random hash for cache-busting
* though could be used to cache-bust nearly anything such as images
* tip: HTML file basenames (like any) should be fairly minimal: letters and numbers, - _ . characters
*/
{
test: /\.src.html$/,
use: [
{
loader: 'file-loader',
options: {
// replace .src.html with just .html
name: '[path][1].html',
regExp: '([\\w\\-\.]+)\\.src\\.html$',
},
},
{
loader: StringReplacePlugin.replace({
replacements: [
{
pattern: /\[hash\]/g,
replacement: function (match, p1, offset, string) {
const randomhash = Date.now().toString();
return randomhash;
}
},
// a series of HTML partials to be interpolated
{
pattern: /\<!--\[include_footer\]-->/g,
replacement: function (match, p1, offset, string) {
return HTML_PARTIALS.footer;
}
},
{
pattern: /\<!--\[include_navbar\]-->/g,
replacement: function (match, p1, offset, string) {
return HTML_PARTIALS.navbar;
},
},
{
pattern: /\<!--\[include_head\]-->/g,
replacement: function (match, p1, offset, string) {
return HTML_PARTIALS.headtags;
},
},
]})
},
]
},
/*
* Files to ignore
* Notably from CSS, e.g. background-image SVG, PNGs, JPEGs, fonts, ...
* we do not need them processed; our stylesheets etc. will point to them in their proper place
* webpack scans the HTML files and will throw a fit if we don't account for every single file it finds
*/
{
test: /\.(svg|gif|jpg|jpeg|png)$/,
loader: 'ignore-loader'
},
{
test: /\.(woff|woff2|ttf|eot)$/,
loader: 'ignore-loader'
}
]
},
/*
* enable source maps, applicable to both JS and CSS
*/
devtool: "nosources-source-map",
/*
* plugins for the above
*/
plugins: [
// Lint our JavaScript files
new ESLintPlugin({
extensions: ['js', 'js6'],
exclude: [
'node_modules',
'_common/jslibs',
...STATE_JS6_FILES.slice(1) // lint just one of the states
],
overrideConfig: require('./.eslintrc.js')
}),
// CSS output from the CSS + Sass handlers above
new MiniCssExtractPlugin({
filename: '[name].css'
}),
// for doing string replacements on files
new StringReplacePlugin(),
],
/*
* plugins for the above
*/
devServer: {
contentBase: './WEBSITE_OUTPUT',
host: '0.0.0.0',
port: 8000,
disableHostCheck: true,
writeToDisk: true,
headers: {
// When running PlanScore repo alongside, ACAO lets webfonts served by the dev server
"Access-Control-Allow-Origin": "*",
}
}
};