2021-02-05 16:35:52 +08:00
|
|
|
import { Shell } from "./shell";
|
|
|
|
import { createMergedSourceMapFromFiles } from "source-map-merger"
|
2021-02-23 10:55:00 +08:00
|
|
|
import fs from "fs";
|
2021-02-05 16:35:52 +08:00
|
|
|
import { glob } from "./util";
|
2021-02-23 10:55:00 +08:00
|
|
|
import path from "path";
|
2021-02-05 16:35:52 +08:00
|
|
|
export async function build() {
|
2021-02-25 17:47:50 +08:00
|
|
|
let ret = await Shell.exec("node", ["node_modules/.bin/tsc", "-p", "."], {
|
|
|
|
env: process.env,
|
|
|
|
consoleHandler: (info) => {
|
|
|
|
console.log(info);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ret !== 0) {
|
|
|
|
console.log("Compile error".red);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = await Shell.exec("node", ["node_modules/.bin/rollup", "-c",], {
|
|
|
|
env: process.env,
|
|
|
|
consoleHandler: (info) => {
|
|
|
|
console.log(info);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ret !== 0) {
|
|
|
|
console.log("Compile error".red);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-05 16:35:52 +08:00
|
|
|
const bundleFiles = await glob("bundle/**/*.js");
|
|
|
|
for (let bundleFile of bundleFiles) {
|
|
|
|
await doMerge(bundleFile);
|
|
|
|
}
|
2021-02-23 10:55:00 +08:00
|
|
|
if (fs.existsSync("assets")) {
|
|
|
|
const assets = await fs.promises.readdir("assets")
|
|
|
|
for (let asset of assets) {
|
|
|
|
const assetFile = path.resolve("assets", asset);
|
|
|
|
const stat = await fs.promises.stat(assetFile);
|
|
|
|
await Shell.exec("cp", ["-rf", assetFile, "bundle"]);
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
console.log(`Asset -> ${asset.yellow}`);
|
|
|
|
} else {
|
|
|
|
console.log(`Asset -> ${asset.green}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 16:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-03-02 15:29:43 +08:00
|
|
|
const mapContent = await fs.promises.readFile(mapFile, "utf-8");
|
2021-03-03 14:39:32 +08:00
|
|
|
try {
|
|
|
|
if (JSON.parse(mapContent).merged) {
|
|
|
|
console.log("Already merged");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-03-02 15:29:43 +08:00
|
|
|
}
|
2021-03-02 11:50:12 +08:00
|
|
|
const lockFile = `${mapFile}.lock`;
|
|
|
|
if (fs.existsSync(lockFile)) {
|
|
|
|
console.log("In mergeMap,skip")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await fs.promises.writeFile(lockFile, (new Date).toString(), "utf-8")
|
|
|
|
try {
|
|
|
|
const buildMap = mapFile.replace(/bundle\//, 'build/')
|
|
|
|
if (fs.existsSync(buildMap)) {
|
|
|
|
const mergedMap = createMergedSourceMapFromFiles([
|
|
|
|
buildMap,
|
|
|
|
mapFile,
|
2021-03-02 15:29:43 +08:00
|
|
|
], true) as string;
|
|
|
|
const mapObj = JSON.parse(mergedMap);
|
|
|
|
mapObj.merged = true
|
|
|
|
await fs.promises.writeFile(mapFile, JSON.stringify(mapObj), "utf-8");
|
2021-03-02 11:50:12 +08:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await fs.promises.unlink(lockFile)
|
2021-02-07 16:33:31 +08:00
|
|
|
}
|
2021-03-02 11:50:12 +08:00
|
|
|
|
2021-02-05 18:20:42 +08:00
|
|
|
}
|