-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-build.js
124 lines (101 loc) · 3.42 KB
/
post-build.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
const fs = require("fs");
const path = require("path");
/*
Note (16.12.2023) on how to build both CommonJS and ESModule versions:
- Compile to ESModule in one directory:
tsc --module esnext --outDir "./dist/es"
- Compile to CommonJS in another directory:
tsc --module commonjs --outDir "./dist/commonjs"
- Package.json should have main-field, module-field and exports-field pointing
to the two entry points as follows:
"main": "dist/commonjs/index.js",
"module": "dist/es/index.js",
"exports": {
".": {
"import": "./dist/es/index.js",
"require": "./dist/commonjs/index.js"
}
},
Post-build steps (handled by this script after `npm run build`):
- Add additional package.json files to dist:
- ./dist/commonjs/package.json with content { "type": "commonjs" }
- ./dist/es/package.json with content { "type": "module" }
- Fix import and re-export paths to use standard format:
- Add .js file extension, e.g.
import { foo } from "./bar" --> import { foo } from "./bar.js"
- Add path to index.js for directory imports (not supported by ESM), e.g.
import { foo } from ".." --> import { foo } from "../index.js"
export * from "./dir" --> export * from "./dir/index.js"
*/
console.log("Running post-build script");
const commonjsDir = path.join(__dirname, "dist", "commonjs");
const esDir = path.join(__dirname, "dist", "es");
// Add package.json files:
fs.writeFileSync(
path.join(commonjsDir, "package.json"),
`{
"type": "commonjs"
}
`,
"utf-8"
);
console.log(`Added package.json to ${commonjsDir}`);
fs.writeFileSync(
path.join(esDir, "package.json"),
`{
"type": "module"
}
`,
"utf-8"
);
console.log(`Added package.json to ${esDir}`);
/*
Fix imports and re-exports for ES modules, by adding .js extension and by
changing directory imports to refer to index.js. This code is mostly generated
with GPT 4.0.
*/
readDirectory(esDir);
// Function to recursively read through the directory
function readDirectory(dir) {
const files = fs.readdirSync(dir);
files.forEach((file) => {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
readDirectory(filePath);
} else if (stats.isFile() && filePath.endsWith(".js")) {
updateModuleReferences(filePath);
}
});
}
// Function to update import and export statements in a file
function updateModuleReferences(filePath) {
let content = fs.readFileSync(filePath, "utf8");
// Regular expression to find and update import/export statements
const moduleRegex =
/(import\s+[\s\S]*?\s+from\s+|export\s+.*?\s+from\s+)['"]([^'"]+)['"]/gm;
let updatedContent = content.replace(
moduleRegex,
(match, prefix, modulePath) => {
if (!modulePath.startsWith(".")) {
// Skip if it's not a relative path
return match;
}
// Check if the module path is a directory and add /index if necessary
let fullPath = path.join(path.dirname(filePath), modulePath);
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
if (!modulePath.endsWith("/")) {
modulePath += "/";
}
modulePath += "index";
}
// Append .js extension to the module path
modulePath += ".js";
return `${prefix || ""}'${modulePath}'`;
}
);
if (content !== updatedContent) {
fs.writeFileSync(filePath, updatedContent, "utf8");
console.log(`Updated module references in ${filePath}`);
}
}