generated from freeCodeCamp/template
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathwebpack.config.js
More file actions
188 lines (177 loc) · 4.6 KB
/
webpack.config.js
File metadata and controls
188 lines (177 loc) · 4.6 KB
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
const path = require("path");
const webpack = require("webpack");
const formatExceptionBaseConfig = {
entry: {
"format-exception": path.resolve(
__dirname,
"packages/helpers/python/index.ts",
),
},
module: {
rules: [
{
test: /\.ts$/,
include: path.resolve(__dirname, "packages/helpers/"),
loader: "ts-loader",
options: {
projectReferences: true,
},
},
{
test: /\.py/,
type: "asset/source",
},
],
},
resolve: {
extensions: [".ts", ".js"],
extensionAlias: {
".ts": [".js", ".ts"],
},
},
};
const formatExceptionNodeConfig = (env = {}) => {
const isDev = env.development;
return {
...formatExceptionBaseConfig,
name: "format-exception-commonjs",
mode: isDev ? "development" : "production",
devtool: !isDev ? "source-map" : "inline-source-map",
target: "node",
output: {
library: {
type: "commonjs",
},
filename: "[name].cjs",
path: path.resolve(__dirname, "dist/curriculum-helpers"),
},
};
};
const formatExceptionBrowserConfig = (env = {}) => {
const isDev = env.development;
return {
...formatExceptionBaseConfig,
name: "format-exception-browser",
mode: isDev ? "development" : "production",
devtool: !isDev ? "source-map" : "inline-source-map",
target: "web",
experiments: {
outputModule: true,
},
output: {
library: {
type: "module",
},
filename: "[name].mjs",
path: path.resolve(__dirname, "dist/curriculum-helpers"),
},
};
};
// ts-loader doesn't seem to support multiple entry points, so we need to create
// multiple sets of rules for each entry point.
const entrypointSources = [
{
name: "index",
path: __dirname + "/packages/main/src",
},
{
name: "dom-test-evaluator",
path: __dirname + "/packages/dom-evaluator/src",
},
{
name: "javascript-test-evaluator",
path: __dirname + "/packages/javascript-evaluator/src",
isWorker: true,
},
{
name: "python-test-evaluator",
path: __dirname + "/packages/python-evaluator/src",
isWorker: true,
},
];
const sharedSources = [
{
name: "shared",
path: __dirname + "/packages/shared/src",
},
{
name: "helpers",
path: __dirname + "/packages/helpers",
},
];
const allSources = [...entrypointSources, ...sharedSources];
const getTSRules = (isDev) =>
allSources.map(({ name, path }) => ({
test: /\.ts$/,
include: path,
use: [
{
loader: "ts-loader",
options: {
projectReferences: true,
instance: name,
transpileOnly: isDev, // we use tsc for type checking, but we need the declaration files to be generated in production
},
},
],
exclude: /node_modules/,
}));
const testRunnerConfig =
(entry, name, isWorker) =>
(env = {}) => {
const isDev = env.development;
return {
name: `test-runner-${name}`,
mode: isDev ? "development" : "production",
cache: isDev ? { type: "filesystem" } : false,
entry,
output: {
filename: "[name].js",
// during testing, we need the files to be available for the test server:
path: isDev
? __dirname + "/__fixtures__/dist"
: __dirname + "/dist/test-runner",
...(isWorker && { chunkLoading: "import-scripts" }),
},
module: {
rules: [
{
test: /\.py/,
type: "asset/source",
},
...getTSRules(isDev),
],
},
resolve: {
fallback: {
// buffer: require.resolve("buffer"),
util: require.resolve("util"),
stream: false,
process: require.resolve("process/browser.js"),
timers: require.resolve("timers-browserify"),
},
extensions: [".ts", ".js"],
},
plugins: [
new webpack.ProvidePlugin({
process: "process/browser",
}),
// @sinon/fake-timers can use 'timers/promises' if it's available, but
// 'timers-browserify' does not include it. This means webpack has to be
// told to ignore it, otherwise it will throw an error when trying to
// build.
new webpack.IgnorePlugin({
resourceRegExp: /timers\/promises/,
}),
],
};
};
const testRunnerConfigs = entrypointSources.map(({ name, path, isWorker }) => {
const entry = { [name]: `${path}/${name}.ts` };
return testRunnerConfig(entry, name, isWorker);
});
module.exports = [
formatExceptionNodeConfig,
formatExceptionBrowserConfig,
...testRunnerConfigs,
];