2022-06-15 07:50:05 -05:00
|
|
|
import esbuild from 'esbuild';
|
2023-05-17 07:51:27 -05:00
|
|
|
import { red } from 'kleur/colors';
|
2023-07-17 19:17:59 -05:00
|
|
|
import fs from 'node:fs';
|
|
|
|
import path from 'node:path';
|
|
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
2022-06-15 07:50:05 -05:00
|
|
|
import glob from 'tiny-glob';
|
|
|
|
|
2022-06-28 10:54:59 -05:00
|
|
|
function escapeTemplateLiterals(str) {
|
|
|
|
return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${');
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:50:05 -05:00
|
|
|
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) {
|
2023-05-17 07:51:27 -05:00
|
|
|
let tscode = await fs.promises.readFile(filepath, 'utf-8');
|
|
|
|
// If we're bundling a client directive, modify the code to match `packages/astro/src/core/client-directive/build.ts`.
|
|
|
|
// If updating this code, make sure to also update that file.
|
|
|
|
if (filepath.includes(`runtime${path.sep}client`)) {
|
|
|
|
// `export default xxxDirective` is a convention used in the current client directives that we use
|
|
|
|
// to make sure we bundle this right. We'll error below if this convention isn't followed.
|
|
|
|
const newTscode = tscode.replace(
|
|
|
|
/export default (.*?)Directive/,
|
|
|
|
(_, name) =>
|
|
|
|
`(self.Astro || (self.Astro = {})).${name} = ${name}Directive;window.dispatchEvent(new Event('astro:${name}'))`
|
|
|
|
);
|
|
|
|
if (newTscode === tscode) {
|
|
|
|
console.error(
|
|
|
|
red(
|
|
|
|
`${filepath} doesn't follow the \`export default xxxDirective\` convention. The prebuilt output may be wrong. ` +
|
|
|
|
`For more information, check out ${fileURLToPath(import.meta.url)}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
tscode = newTscode;
|
|
|
|
}
|
|
|
|
const esbuildresult = await esbuild.build({
|
|
|
|
stdin: {
|
|
|
|
contents: tscode,
|
|
|
|
resolveDir: path.dirname(filepath),
|
|
|
|
loader: 'ts',
|
|
|
|
sourcefile: filepath,
|
|
|
|
},
|
|
|
|
format: 'iife',
|
2023-06-06 04:39:25 -05:00
|
|
|
target: ['es2018'],
|
2022-06-16 08:54:09 -05:00
|
|
|
minify,
|
2023-05-17 07:51:27 -05:00
|
|
|
bundle: true,
|
|
|
|
write: false,
|
2022-06-15 07:50:05 -05:00
|
|
|
});
|
2023-05-17 07:51:27 -05:00
|
|
|
const code = esbuildresult.outputFiles[0].text.trim();
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-05-17 07:51:27 -05:00
|
|
|
export default \`${escapeTemplateLiterals(code)}\`;`;
|
2022-06-15 07:50:05 -05:00
|
|
|
const url = getPrebuildURL(filepath);
|
|
|
|
await fs.promises.writeFile(url, mod, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(entryPoints.map(prebuildFile));
|
|
|
|
}
|