2021-02-05 16:35:52 +08:00
|
|
|
import { Shell } from "./shell";
|
|
|
|
import { createMergedSourceMapFromFiles } from "source-map-merger"
|
|
|
|
import fs from "fs"
|
|
|
|
import { glob } from "./util";
|
|
|
|
|
|
|
|
export async function build() {
|
|
|
|
await Shell.exec("node", ["node_modules/.bin/tsc", "-p", "."]);
|
|
|
|
await Shell.exec("node", ["node_modules/.bin/rollup", "-c"]);
|
|
|
|
const bundleFiles = await glob("bundle/**/*.js");
|
|
|
|
for (let bundleFile of bundleFiles) {
|
|
|
|
await doMerge(bundleFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function clean() {
|
|
|
|
await Shell.exec("rm", ["-rf", "build"]);
|
|
|
|
await Shell.exec("rm", ["-rf", "bundle"]);
|
|
|
|
}
|
|
|
|
|
2021-02-05 18:20:42 +08:00
|
|
|
async function doMerge(jsFile: string) {
|
|
|
|
const mapFile = `${jsFile}.map`;
|
2021-02-05 16:35:52 +08:00
|
|
|
console.log(`Bundle -> ${jsFile.green}`);
|
2021-02-05 18:20:42 +08:00
|
|
|
if (!fs.existsSync(mapFile)) {
|
2021-02-05 16:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
2021-02-05 18:20:42 +08:00
|
|
|
console.log(` -> ${mapFile.green}`);
|
|
|
|
await mergeMap(mapFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function mergeMap(mapFile: string) {
|
2021-02-05 16:35:52 +08:00
|
|
|
const mergedMap = createMergedSourceMapFromFiles([
|
2021-02-05 18:20:42 +08:00
|
|
|
mapFile.replace(/bundle\//, 'build/'),
|
|
|
|
mapFile,
|
2021-02-05 16:35:52 +08:00
|
|
|
], true);
|
2021-02-05 18:20:42 +08:00
|
|
|
await fs.promises.writeFile(mapFile, mergedMap);
|
|
|
|
}
|