Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/astro/src/core/csp-hashes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is code-generated, please don't change it manually
export default [
"GI/D8grziRZwfj/Mqmn+dcgU/i8sylHSR/IfobqcUT4=",
"HDWxd14AUw8OvjrhhRRyyZFHCGnzxXGDrg59Qi8ayhc=",
"XN6a2Vn8uvpBr/WhdYPdK0jVeCzlcOD2XYaP10veV4Y=",
"ZR0ZAU8UNTzLmo/ApeWH0y1mVLT+XtFkvZ5nw32W8jI=",
"cSNmhdbFlyTDRozeu9HPjo+B2S4QAeMp0RO41PqgAcA=",
"mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI=",
"mH3H4wSoDVWMXJKrmeBKYJQMdAZQ3dArB2N66JomkzI="
];
19 changes: 18 additions & 1 deletion scripts/cmd/prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath, pathToFileURL } from 'node:url';
import esbuild from 'esbuild';
import { red } from 'kleur/colors';
import { glob } from 'tinyglobby';
import crypto from 'node:crypto';

function escapeTemplateLiterals(str) {
return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${');
Expand Down Expand Up @@ -40,6 +41,8 @@ export default async function prebuild(...args) {
return outURL;
}

const hashes = [];

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`.
Expand Down Expand Up @@ -99,17 +102,31 @@ export default async function prebuild(...args) {
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 \`${escapeTemplateLiterals(code)}\`;`;
export default \`${generatedCode}\`;`;
const url = getPrebuildURL(filepath, result.dev);
await fs.promises.writeFile(url, mod, 'utf-8');
const hash = crypto.createHash('sha256').update(code).digest('base64');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use generatedCode instead of code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because that's not the code that is shipped

hashes.push(hash);
}
}

await Promise.all(entryPoints.map(prebuildFile));
hashes.sort();
const entries = hashes.map((hash) => `"${hash}"`);
const content = `// This file is code-generated, please don't change it manually
export default [
${entries.join(',\n ')}
];`;
await fs.promises.writeFile(
path.join(fileURLToPath(import.meta.url), '../../../packages/astro/src/core', 'csp-hashes.js'),
content,
'utf-8',
);
}