2022-06-15 07:50:05 -05:00
|
|
|
import * as terser from 'terser';
|
|
|
|
import esbuild from 'esbuild';
|
|
|
|
import glob from 'tiny-glob';
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import { pathToFileURL, fileURLToPath } from 'url';
|
|
|
|
|
|
|
|
export default async function prebuild(...args) {
|
|
|
|
let buildToString = args.indexOf('--to-string');
|
2022-06-15 07:52:13 -05:00
|
|
|
if (buildToString !== -1) {
|
2022-06-15 07:50:05 -05:00
|
|
|
args.splice(buildToString, 1);
|
|
|
|
buildToString = true;
|
|
|
|
}
|
2022-06-16 08:54:09 -05:00
|
|
|
let minify = true;
|
|
|
|
let minifyIdx = args.indexOf('--no-minify');
|
2022-06-16 08:55:54 -05:00
|
|
|
if (minifyIdx !== -1) {
|
2022-06-16 08:54:09 -05:00
|
|
|
minify = false;
|
|
|
|
args.splice(minifyIdx, 1);
|
|
|
|
}
|
2022-06-15 07:50:05 -05:00
|
|
|
|
|
|
|
let patterns = args;
|
|
|
|
let entryPoints = [].concat(
|
|
|
|
...(await Promise.all(
|
|
|
|
patterns.map((pattern) => glob(pattern, { filesOnly: true, absolute: true }))
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
function getPrebuildURL(entryfilepath) {
|
|
|
|
const entryURL = pathToFileURL(entryfilepath);
|
|
|
|
const basename = path.basename(entryfilepath);
|
|
|
|
const ext = path.extname(entryfilepath);
|
|
|
|
const name = basename.slice(0, basename.indexOf(ext));
|
|
|
|
const outname = `${name}.prebuilt${ext}`;
|
|
|
|
const outURL = new URL('./' + outname, entryURL);
|
|
|
|
return outURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function prebuildFile(filepath) {
|
|
|
|
const tscode = await fs.promises.readFile(filepath, 'utf-8');
|
|
|
|
const esbuildresult = await esbuild.transform(tscode, {
|
|
|
|
loader: 'ts',
|
2022-06-16 08:54:09 -05:00
|
|
|
minify,
|
2022-06-15 07:50:05 -05:00
|
|
|
});
|
|
|
|
const rootURL = new URL('../../', import.meta.url);
|
2022-06-15 07:52:13 -05:00
|
|
|
const rel = path.relative(fileURLToPath(rootURL), filepath);
|
2022-06-15 07:50:05 -05:00
|
|
|
const mod = `/**
|
|
|
|
* This file is prebuilt from ${rel}
|
|
|
|
* Do not edit this directly, but instead edit that file and rerun the prebuild
|
|
|
|
* to generate this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default \`${esbuildresult.code.trim()}\`;`;
|
|
|
|
const url = getPrebuildURL(filepath);
|
|
|
|
await fs.promises.writeFile(url, mod, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(entryPoints.map(prebuildFile));
|
|
|
|
}
|