-
Notifications
You must be signed in to change notification settings - Fork 83
/
build.js
executable file
·144 lines (121 loc) · 3.86 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const esbuild = require("esbuild");
const glob = require("glob").globSync;
const packageJson = require("./package.json");
const SRC_FILES = glob("./src/**/*.ts");
const OUT_DIR = path.resolve("build");
const npmArgs = process.argv.slice(2);
// .npmignore
const NPM_IGNORE = [".*", "*.tgz", "node_modules", "package-lock.json"];
/** Builds */
function build() {
// generate minified output
for (const format of ["esm", "cjs"]) {
esbuild.buildSync({
entryPoints: SRC_FILES,
outdir: path.join(OUT_DIR, "dist", format),
format: format,
platform: "node"
});
}
}
/**
* Create module in OUT_DIR
*/
function createModule() {
console.log("Creating module at " + OUT_DIR);
// ensure directory exists
if (!fs.existsSync(OUT_DIR)) fs.mkdirSync(OUT_DIR);
// write ignore file
fs.writeFileSync(path.join(OUT_DIR, ".npmignore"), NPM_IGNORE.join("\n"));
// copy all the allowed files to the lib directory
packageJson.files = ["LICENSE", "README.md"].reduce(
(files, p) => {
fs.copyFileSync(path.resolve(p), path.join(OUT_DIR, p));
files.push(p);
return files;
},
["**/*.js", "**/*.ts", "**/*.json"]
);
// clear all scripts
packageJson.scripts = {};
packageJson.devDependencies = {};
// add exports explicitly
const files = Array.from(
new Set(
SRC_FILES.filter(s => !s.includes("_")).map(s => {
const d = path.dirname(s);
if (d === "src") return path.basename(s).slice(0, -3);
if (d.includes("init")) return s.slice(4, -3);
return d.slice(4);
})
)
);
packageJson.exports = {
"./package.json": "./package.json"
};
files.forEach(name => {
const [outFile, key] =
name == "index" // root
? [name, "."]
: name.startsWith("init") // side-effects
? [name, `./${name}`]
: !name.includes("/") // top-level
? [name, `./${name}`]
: [`${name}/index`, `./${name}`]; //nested parents
const typesPath = `./dist/types/${outFile}.d.ts`;
const cjsPath = `./dist/cjs/${outFile}.js`;
const esmPath = `./dist/esm/${outFile}.js`;
if (key != ".") {
// create subpackage package.json
const subPackagePath = path.join(OUT_DIR, name);
if (!fs.existsSync(subPackagePath)) {
fs.mkdirSync(subPackagePath, { recursive: true });
}
const subPackageJson = {
main: path.relative(subPackagePath, path.join(OUT_DIR, cjsPath)),
module: path.relative(subPackagePath, path.join(OUT_DIR, esmPath)),
types: path.relative(subPackagePath, path.join(OUT_DIR, typesPath)),
sideEffects: outFile.startsWith("init")
};
fs.writeFileSync(
path.join(subPackagePath, "package.json"),
JSON.stringify(subPackageJson, null, 2)
);
}
packageJson.exports[key] = {
types: typesPath,
node: cjsPath,
require: cjsPath,
default: esmPath
};
});
const data = JSON.stringify(packageJson, null, 2);
// write new package.json for lib
fs.writeFileSync(path.join(OUT_DIR, "package.json"), data);
}
function main() {
build();
createModule();
if (npmArgs.length) {
// execute within lib dir
console.log("\nExecuting command:", `npm ${npmArgs.join(" ")}`, "\n");
// execute command
cp.spawnSync("npm", npmArgs, {
cwd: OUT_DIR,
env: process.env,
stdio: "inherit"
});
console.log("\nCompleted command\n");
// if we created a tar file, copy to parent directory
let tarball = packageJson.name + "-" + packageJson.version + ".tgz";
let tarballPath = path.join(OUT_DIR, tarball);
if (fs.existsSync(tarballPath)) {
console.log("Copying", tarball, "to correct folder");
fs.renameSync(tarballPath, path.join(path.dirname(OUT_DIR), tarball));
}
}
}
main();