forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
babel-worker.cjs
45 lines (40 loc) · 1.15 KB
/
babel-worker.cjs
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
const { transformAsync } = require("@babel/core");
const { mkdirSync, statSync, readFileSync, writeFileSync } = require("fs");
const { dirname } = require("path");
const { log } = require("./scripts/utils/logger.cjs");
let chalk;
const chalkP = import("chalk").then(ns => {
chalk = ns.default;
});
function needCompile(src, dest) {
let destStat;
try {
destStat = statSync(dest);
} catch (err) {
if (err.code === "ENOENT") {
return true;
} else {
throw err;
}
}
const srcStat = statSync(src);
return srcStat.mtimeMs > destStat.mtimeMs;
}
exports.transform = async function transform(src, dest) {
if (!chalk) await chalkP;
mkdirSync(dirname(dest), { recursive: true });
if (!needCompile(src, dest)) {
return;
}
log(`Compiling '${chalk.cyan(src)}'...`);
const content = readFileSync(src, { encoding: "utf8" });
const { code } = await transformAsync(content, {
filename: src,
caller: {
// We have wrapped packages/babel-core/src/config/files/configuration.js with feature detection
supportsDynamicImport: true,
name: "babel-worker",
},
});
writeFileSync(dest, code, "utf8");
};