Repository navigation
Expand file tree
/
Copy pathbabel.config.js
More file actions
75 lines (65 loc) · 2.21 KB
/
Copy pathbabel.config.js
File metadata and controls
75 lines (65 loc) · 2.21 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
const path = require("node:path");
const MIN_BABEL_VERSION = 8;
// The middleware itself runs on the node.js version `engines` requires.
const NODE_TARGETS = { node: "20.9.0" };
// The hot client runs in the browser: it must stay parsable by an ES5 engine,
// so it is compiled down to ES5 (`ie: "11"` is preset-env's ES5 baseline).
// `modules: false` keeps the ESM syntax for webpack to tree-shake.
const CLIENT_TARGETS = { ie: "11" };
// The two webpack modules the client logs through, and the ES5 copies of them
// `scripts/build-client-logger.mjs` writes next to the built client.
const CLIENT_SRC = path.join(__dirname, "client-src");
const ES5_LOGGER = {
"webpack/lib/logging/Logger.js": "Logger.cjs",
"webpack/lib/logging/createConsoleLogger.js": "createConsoleLogger.cjs",
};
/**
* Point the built client at the ES5 copies of webpack's logger. Webpack's own
* are node-side source, which a bundle for an ES5 browser would carry as is.
* @returns {import("@babel/core").PluginObj} plugin
*/
function useES5Logger() {
return {
name: "use-es5-logger",
visitor: {
ImportDeclaration(declaration, state) {
const target = ES5_LOGGER[declaration.node.source.value];
if (!target) {
return;
}
const relative = path
.relative(
path.dirname(/** @type {string} */ (state.filename)),
path.join(CLIENT_SRC, "modules", "logger", target),
)
.split(path.sep)
.join("/");
declaration.node.source.value = relative.startsWith(".")
? relative
: `./${relative}`;
},
},
};
}
module.exports = (api) => {
api.assertVersion(MIN_BABEL_VERSION);
// Jest transforms the sources too — there everything, the client included,
// has to be CommonJS for the node.js running the tests.
if (api.env("test")) {
return {
presets: [["@babel/preset-env", { targets: NODE_TARGETS }]],
};
}
return {
presets: [["@babel/preset-env", { targets: NODE_TARGETS }]],
overrides: [
{
test: "./client-src",
presets: [
["@babel/preset-env", { modules: false, targets: CLIENT_TARGETS }],
],
plugins: [useES5Logger],
},
],
};
};