chore: escape template literals in prebuild script (#3745)

Co-authored-by: Nate Moore <nate@astro.build>
This commit is contained in:
Nate Moore 2022-06-28 10:54:59 -05:00 committed by GitHub
parent 8829cc7ab6
commit 199fd91e8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,10 +1,13 @@
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';
function escapeTemplateLiterals(str) {
return str.replace(/\`/g, '\\`').replace(/\$\{/g, '\\${');
}
export default async function prebuild(...args) {
let buildToString = args.indexOf('--to-string');
if (buildToString !== -1) {
@ -49,7 +52,7 @@ export default async function prebuild(...args) {
* to generate this file.
*/
export default \`${esbuildresult.code.trim()}\`;`;
export default \`${escapeTemplateLiterals(esbuildresult.code.trim())}\`;`;
const url = getPrebuildURL(filepath);
await fs.promises.writeFile(url, mod, 'utf-8');
}