This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathesm-modules.js
52 lines (42 loc) · 1.62 KB
/
esm-modules.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
const fs = require("fs").promises;
const path = require("path");
async function run(rootDir) {
console.log("Root Directory", path.resolve(rootDir));
await processDir("./dist/esm");
}
async function processDir(dir) {
const entries = await fs.readdir(dir);
for (const entry of entries) {
const fullEntry = path.join(dir, entry);
const stat = await fs.stat(fullEntry);
if (stat.isFile()) {
console.log("Processing File", fullEntry);
let content = await fs.readFile(fullEntry, "utf-8");
if (!content.includes(".mjs")) {
content = content.replace(/import(.*)\"\.(.*)\";/g, 'import$1".$2.mjs";');
content = content.replace(/export(.*)\"\.(.*)\";/g, 'export$1".$2.mjs";');
const sourceMapUrlRegexp = new RegExp("//# sourceMappingURL=data:application/json;base64,(.*)");
content = content.replace(sourceMapUrlRegexp, "");
await fs.writeFile(fullEntry, content, "utf-8");
}
if (/\.js$/.test(fullEntry)) {
const newName = fullEntry.replace(/\.js$/, ".mjs");
await fs.rename(fullEntry, newName);
}
} else if (stat.isDirectory()) {
console.log("Processing Dir", fullEntry);
await processDir(fullEntry);
}
}
}
console.error("ESM Modules");
if (process.argv.length < 3) {
console.error("Error: Not enough command line arguments.");
process.exit(1);
}
run(process.argv[2])
.then(() => console.log("Done"))
.catch(e => {
console.error(e);
process.exit(1);
});