Files
astro/scripts/cmd/prebuild.js
Emanuele Stoppa 0eafe14b08 feat: experimental CSP (#13802)
* chore: build hashes of scripts (#13590)

* chore: build hashes of scripts

* chore: fix changes

* chore: fix changes

* chore: fix changes

* feat(csp): create hashes of tracked scripts and hashes (#13675)

Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>

* feat(csp): fix CSP header, inject astro island script/style (#13687)

* feat(csp): track client scripts and CSS (#13725)

Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>

* feat(csp): support view transitions (#13738)

Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>
Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>
fix CSP header, inject astro island script/style (#13687)

* feat(csp): server islands (#13775)

Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>

* feat(csp): customise algorithm (#13803)

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* chore: build hashes of scripts (#13590) (#13805)

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* feat(csp): allow additional directives (#13810)

Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>
Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>

* feat(csp): resources for script and styles directives (#13812)

Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>

* feat(csp): runtime APIs (#13824)

Co-authored-by: Matt Kane <m@mk.gg>

* feat(csp): add script-dynamic keyword support (#13834)

* update lockfile

* chore: docs and changeset (#13870)

* chore: add changeset

* grammar

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>

* Update JSDoc with examples to match docs

* Sarah's changeset edits

* Apply suggestions from code review

Thanks, @ArmandPhilippot

Co-authored-by: Armand Philippot <git@armand.philippot.eu>

* Fix indentation

* Update .changeset/crazy-doors-buy.md

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>

---------

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Co-authored-by: Matt Kane <m@mk.gg>
Co-authored-by: Armand Philippot <git@armand.philippot.eu>

* Update lockfile

* dedupe deps

* Lock

* Lock

* fix: server islands in mdx

---------

Co-authored-by: florian-lefebvre <69633530+florian-lefebvre@users.noreply.github.com>
Co-authored-by: ascorbic <213306+ascorbic@users.noreply.github.com>
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Matt Kane <m@mk.gg>
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Co-authored-by: Armand Philippot <git@armand.philippot.eu>
2025-06-04 13:47:39 +01:00

118 lines
3.5 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import esbuild from 'esbuild';
import { red } from 'kleur/colors';
import { glob } from 'tinyglobby';
function escapeTemplateLiterals(str) {
return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${');
}
export default async function prebuild(...args) {
let buildToString = args.indexOf('--to-string');
if (buildToString !== -1) {
args.splice(buildToString, 1);
buildToString = true;
}
let minify = true;
let minifyIdx = args.indexOf('--no-minify');
if (minifyIdx !== -1) {
minify = false;
args.splice(minifyIdx, 1);
}
let patterns = args;
// NOTE: absolute paths returned are forward slashes on windows
let entryPoints = [].concat(
...(await Promise.all(
patterns.map((pattern) => glob(pattern, { onlyFiles: true, absolute: true })),
)),
);
function getPrebuildURL(entryfilepath, dev = false) {
const entryURL = pathToFileURL(entryfilepath);
const basename = path.basename(entryfilepath);
const ext = path.extname(entryfilepath);
const name = basename.slice(0, basename.indexOf(ext));
const outname = dev ? `${name}.prebuilt-dev${ext}` : `${name}.prebuilt${ext}`;
const outURL = new URL('./' + outname, entryURL);
return outURL;
}
async function prebuildFile(filepath) {
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/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 esbuildOptions = {
stdin: {
contents: tscode,
resolveDir: path.dirname(filepath),
loader: 'ts',
sourcefile: filepath,
},
format: 'iife',
target: ['es2018'],
minify,
bundle: true,
write: false,
};
const results = await Promise.all(
[
{
build: await esbuild.build(esbuildOptions),
dev: false,
},
filepath.includes('astro-island')
? {
build: await esbuild.build({
...esbuildOptions,
define: { 'process.env.NODE_ENV': '"development"' },
}),
dev: true,
}
: undefined,
].filter((entry) => entry),
);
for (const result of results) {
const code = result.build.outputFiles[0].text.trim();
const rootURL = new URL('../../', import.meta.url);
const rel = path.relative(fileURLToPath(rootURL), filepath);
const generatedCode = escapeTemplateLiterals(code);
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 \`${generatedCode}\`;`;
const url = getPrebuildURL(filepath, result.dev);
await fs.promises.writeFile(url, mod, 'utf-8');
}
}
for (const entrypoint of entryPoints) {
await prebuildFile(entrypoint);
}
}