summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorGravatar Matthew Phillips <matthew@skypack.dev> 2022-06-15 08:50:05 -0400
committerGravatar GitHub <noreply@github.com> 2022-06-15 08:50:05 -0400
commitfc52321a888920beebb6b84f56d2f41791666e13 (patch)
tree7e2985d711457a586bc0239a766de9841ac86069 /scripts
parenta7637e6b26c97497cf6de7088d0b3cdfb03eee8d (diff)
downloadastro-fc52321a888920beebb6b84f56d2f41791666e13.tar.gz
astro-fc52321a888920beebb6b84f56d2f41791666e13.tar.zst
astro-fc52321a888920beebb6b84f56d2f41791666e13.zip
Consolidate hydration scripts into just one (#3571)
* Remove redundant hydration scripts * Prebuild the island JS * Fix build * Updates to tests * Update more references * Custom element test now has two classic scripts * Account for non-default exports * Restructure hydration directives * Move nested logic into the island component * Remove try/catch
Diffstat (limited to 'scripts')
-rw-r--r--scripts/cmd/prebuild.js52
-rwxr-xr-xscripts/index.js5
2 files changed, 57 insertions, 0 deletions
diff --git a/scripts/cmd/prebuild.js b/scripts/cmd/prebuild.js
new file mode 100644
index 000000000..7f8733cfe
--- /dev/null
+++ b/scripts/cmd/prebuild.js
@@ -0,0 +1,52 @@
+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');
+ if(buildToString !== -1) {
+ args.splice(buildToString, 1);
+ buildToString = true;
+ }
+
+ 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',
+ minify: true
+ });
+ const rootURL = new URL('../../', import.meta.url);
+ const rel = path.relative(fileURLToPath(rootURL), filepath)
+ 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));
+}
diff --git a/scripts/index.js b/scripts/index.js
index dd789f032..249eac53d 100755
--- a/scripts/index.js
+++ b/scripts/index.js
@@ -13,6 +13,11 @@ export default async function run() {
await copy(...args);
break;
}
+ case 'prebuild': {
+ const { default: prebuild } = await import('./cmd/prebuild.js');
+ await prebuild(...args);
+ break;
+ }
}
}