-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebpack.config.js
280 lines (274 loc) · 9.78 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
/* eslint-env node */
'use strict';
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const ESLintPlugin = require('eslint-webpack-plugin');
const packageJson = require('./package.json');
const fs = require('fs');
const path = require('path');
const rollbarSnippetPath = './node_modules/rollbar/dist/rollbar.snippet.js';
const rollbarSnippet = fs.readFileSync(path.join(__dirname, rollbarSnippetPath), { encoding: 'utf8' }).trim();
// 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;
const baseHtmlPluginConfig = {
template: 'src/index.html',
favicon: 'src/public/favicon.ico',
templateParameters: {
rollbarSnippet,
...packageJson.config
}
};
function configHtmlPlugins(config) {
const { filename } = config;
const numFolders = (filename.match(/\//g) || []).length;
const rootPath = "../".repeat(numFolders);
const plugins = [
new HtmlWebpackPlugin({
...baseHtmlPluginConfig,
...config,
publicPath: rootPath ? `${rootPath}` : ''
})
];
if (DEPLOY_PATH) {
plugins.push(
new HtmlWebpackPlugin({
...baseHtmlPluginConfig,
...config,
filename: filename.replace('.html', '-top.html'),
publicPath: `${rootPath}${DEPLOY_PATH}`
})
);
}
return plugins;
}
module.exports = (env, argv) => {
const devMode = argv.mode !== 'production';
return {
context: __dirname, // to automatically find tsconfig.json
// https://survivejs.com/webpack/building/source-maps/
devtool: devMode ? 'eval-cheap-module-source-map' : 'source-map',
// Performance: disabling these other entry points had no effect
// on the devServer start up time
entry: {
index: './src/index.tsx',
iframe: './src/iframe/iframe.tsx',
'doc-editor': './src/doc-editor.tsx'
},
mode: devMode ? 'development' : 'production',
output: {
clean: true,
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash:8].js'
},
performance: { hints: false },
externals: {
// ignore optional dependency of JSXGraph
'canvas': 'canvas'
},
module: {
rules: [
{
test: /react-data-grid\/lib\/bundle\.js$/,
loader: require.resolve('string-replace-loader'),
options: {
multiple: [
{ // react-data-grid doesn't currently provide a means of clearing cell selection
search: /if \(!isCellWithinBounds\(position\)\) return;/g,
replace:
"// [CC] (string-replace-loader) allow clearing the selection\n" +
" if (!(position.idx === -1 && position.rowIdx === -1) && !isCellWithinBounds(position)) return;",
strict: true // fail build if replacement not performed
}
]
}
},
{
test: /\.[tj]sx?$/i,
loader: 'ts-loader',
exclude: /node_modules/
},
// 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: '@jsdevtools/coverage-istanbul-loader',
options: { esModules: true },
enforce: 'post',
exclude: path.join(__dirname, 'node_modules'),
} : {},
{ // disable svgo optimization for files ending in .nosvgo.svg
test: /\.nosvgo\.svg$/i,
loader: "@svgr/webpack",
options: {
svgo: false
}
},
{
test: /\.svg$/i,
exclude: /\.nosvgo\.svg$/i,
oneOf: [
{
issuer: /\.[tj]sx?$/i,
loader: "@svgr/webpack",
options: {
svgoConfig: {
plugins: [
{
// cf. https://github.com/svg/svgo/releases/tag/v2.4.0
name: "preset-default",
params: {
overrides: {
// 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 }
}
}
}
]
}
}
},
{
// Do not apply SVGR import in CSS files.
issuer: /\.(sa|sc|le|c)ss$/i,
type: 'asset',
generator: {
filename: 'assets/images/[name].[contenthash:6][ext]'
}
}
]
},
{
test: /\.(sa|sc|le|c)ss$/i,
use: [
devMode ? 'style-loader' : MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
modules: {
// required for :import from scss files
// cf. https://github.com/webpack-contrib/css-loader#separating-interoperable-css-only-and-css-module-features
// v6 changed from `compileType` to `mode`
mode: 'icss'
}
}
},
'postcss-loader',
'sass-loader'
]
},
{
test: /\.(woff|woff2|eot|ttf)$/i,
type: 'asset',
generator: {
filename: 'assets/fonts/[name].[contenthash:6][ext]'
}
},
{
// store placeholder image as file not data URI
test: /image_placeholder\.png$/,
type: 'asset/resource',
generator: {
filename: 'assets/images/[name][ext]'
}
},
{
test: /\.png$/i,
// don't convert placeholder image to a data URI
exclude: /image_placeholder\.png$/,
type: 'asset',
generator: {
filename: 'assets/images/[name].[contenthash:6][ext]'
}
}
]
},
optimization: {
moduleIds: "deterministic",
splitChunks: {
chunks: 'all',
// In general only split modules out of chunks when the module is used
// in 2 or more chunks
minChunks: 2,
filename: (pathData) => {
// console.log("vendor filename", pathData.chunk.id,
// [...pathData.chunk._groups].map(group => group.options?.name),
// [...pathData.chunk._groups].map(group => group.chunks));
const groupsNames = [...pathData.chunk._groups].map(group => group.options?.name);
return `common-${groupsNames.join('-')}.[chunkhash:8].js`;
},
cacheGroups: {
// For the initial chunk, split modules from node_modules out even if
// they are only used by one initial chunk. Because we have multiple
// entry points this will result in a few different vendor files.
// The entry points share code, so some of the vendor files are used by
// multiple entry points.
initialVendors: {
chunks: 'initial',
test: /[\\/]node_modules[\\/]/,
minChunks: 1,
reuseExistingChunk: true,
filename: (pathData) => {
// console.log("vendor filename", pathData.chunk.id,
// [...pathData.chunk._groups].map(group => group.options?.name),
// [...pathData.chunk._groups].map(group => group.chunks));
const groupsNames = [...pathData.chunk._groups].map(group => group.options?.name);
return `vendor-${groupsNames.join('-')}.[chunkhash:8].js`;
},
},
}
}
},
resolve: {
alias: {
'mobx-state-tree': '@concord-consortium/mobx-state-tree',
'@cortex-js/compute-engine': '@concord-consortium/compute-engine',
// cf. https://github.com/facebook/react/issues/20235#issuecomment-732205073
'react/jsx-runtime': require.resolve('react/jsx-runtime'),
'react-modal-hook': '@concord-consortium/react-modal-hook',
'rete-react-render-plugin': '@concord-consortium/rete-react-render-plugin'
},
fallback: { crypto: false },
extensions: [ '.ts', '.tsx', '.js', '.jsx' ]
},
ignoreWarnings: [/export .* was not found in/],
plugins: [
new ESLintPlugin(),
new MiniCssExtractPlugin({
filename: devMode ? '[name].css' : '[name].[chunkhash:8].css',
ignoreOrder: true
}),
...configHtmlPlugins({
chunks: ['index'],
filename: 'index.html',
}),
...configHtmlPlugins({
chunks: ['doc-editor'],
filename: 'editor/index.html',
}),
new HtmlWebpackPlugin({
...baseHtmlPluginConfig,
chunks: ['iframe'],
filename: 'iframe.html',
publicPath: '.',
// Seems like we could just use the standard index.html here (it adds loading stuff but that should be harmless)
template: 'src/iframe/iframe.html'
}),
new CopyWebpackPlugin({
patterns: [
{from: 'src/public'}
]
})
]
};
};