From 089dcf39324e321031a58ce1f75a72e00554cfd8 Mon Sep 17 00:00:00 2001 From: Liam Bigelow Date: Tue, 22 Feb 2022 17:01:09 +1300 Subject: [PATCH] fix(hugo): bundle hugo correctly when running local component browser --- javascript-modules/browser/cli.js | 3 ++- javascript-modules/browser/main.js | 5 +++-- javascript-modules/browser/package.json | 4 ++-- javascript-modules/builder/main.js | 2 +- .../engines/hugo-engine/lib/builder.js | 4 ++-- .../engines/hugo-engine/lib/engine.js | 18 +++++++++++++++--- 6 files changed, 25 insertions(+), 11 deletions(-) diff --git a/javascript-modules/browser/cli.js b/javascript-modules/browser/cli.js index 6d075ef4..981a3266 100755 --- a/javascript-modules/browser/cli.js +++ b/javascript-modules/browser/cli.js @@ -7,7 +7,8 @@ const program = new Command(); async function run() { program.requiredOption("-b, --bookshop ", "Paths to bookshops (space seperated)"); - program.requiredOption("-o, --output ", "Output JS to given filename"); + program.option("-o, --output ", "Output JS to given filename"); + program.option("-p, --port ", "Host bookshop browser server on a local port"); program.option("--exclude ", "Component tags to exclude (space seperated)"); program.option("--only-engines ", "Only load the specified engines"); program.parse(process.argv); diff --git a/javascript-modules/browser/main.js b/javascript-modules/browser/main.js index 8b503957..02e678bd 100755 --- a/javascript-modules/browser/main.js +++ b/javascript-modules/browser/main.js @@ -11,7 +11,7 @@ import BrowserServer from "./lib/build/browserServer.js"; export const runner = async (options) => { const bookshopDirs = options.bookshop.map(d => path.join(process.cwd(), d)); - const outputFile = path.join(process.cwd(), options.output); + const outputFile = options.output ? path.join(process.cwd(), options.output) : null; const port = options.port ?? null; let server = null; const watch = outputFile ? null : { @@ -53,7 +53,8 @@ export const runner = async (options) => { }, exclude: JSON.stringify(options.exclude || []), onlyEngines: options.onlyEngines, - bookshopDirs: bookshopDirs + bookshopDirs: bookshopDirs, + hosted: !!outputFile, } const output = await Builder(builderOptions); diff --git a/javascript-modules/browser/package.json b/javascript-modules/browser/package.json index 5a821f29..66d0c249 100644 --- a/javascript-modules/browser/package.json +++ b/javascript-modules/browser/package.json @@ -11,7 +11,7 @@ "manual-test-watch": "node main.js -b .test/fixtures -p 7923" }, "bin": { - "bookshop-browser": "main.js" + "bookshop-browser": "cli.js" }, "files": [ "**/*.js", @@ -52,4 +52,4 @@ "engines": { "node": ">=14.16" } -} +} \ No newline at end of file diff --git a/javascript-modules/builder/main.js b/javascript-modules/builder/main.js index b517d0af..94278c9f 100644 --- a/javascript-modules/builder/main.js +++ b/javascript-modules/builder/main.js @@ -39,7 +39,7 @@ export default async (options) => { ...esbuildOptions.loader, ...(engine?.buildLoaders || {}) }; - engine?.esbuildConfigFn?.(esbuildOptions); + engine?.esbuildConfigFn?.(esbuildOptions, options); }); return await esbuild.build({ diff --git a/javascript-modules/engines/hugo-engine/lib/builder.js b/javascript-modules/engines/hugo-engine/lib/builder.js index 441266a1..31253ded 100644 --- a/javascript-modules/engines/hugo-engine/lib/builder.js +++ b/javascript-modules/engines/hugo-engine/lib/builder.js @@ -6,11 +6,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)); export const extensions = [".hugo.html"]; -export const esbuildConfigFn = (esbuildConfig) => { +export const esbuildConfigFn = (esbuildConfig, options) => { esbuildConfig.loader = { ...esbuildConfig.loader, ".hugo.html": "text", - ".wasm": "file" + ".wasm": options?.hosted ? "file" : "binary" }; const wasm_exec_banner = fs.readFileSync(path.join(__dirname, "../hugo-renderer/wasm_exec.js")); diff --git a/javascript-modules/engines/hugo-engine/lib/engine.js b/javascript-modules/engines/hugo-engine/lib/engine.js index 5f3b4c2f..f7ce7224 100644 --- a/javascript-modules/engines/hugo-engine/lib/engine.js +++ b/javascript-modules/engines/hugo-engine/lib/engine.js @@ -31,10 +31,15 @@ export class Engine { } async initializeHugo() { - if (window.CloudCannon?.isMocked) { - await this.initializeLocalHugo(); + // When this script is run locally, the hugo wasm is loaded as binary rather than output as a file. + if (hugoWasm?.constructor === Uint8Array) { + await this.initializeInlineHugo(); } else { - await this.initializeRemoteHugo(); + if (window.CloudCannon?.isMocked) { + await this.initializeLocalHugo(); + } else { + await this.initializeRemoteHugo(); + } } // TODO: Tidy @@ -75,6 +80,13 @@ export class Engine { go.run(result.instance); } + async initializeInlineHugo() { + const go = new Go(); + const buffer = hugoWasm.buffer; + const result = await WebAssembly.instantiate(buffer, go.importObject); + go.run(result.instance); + } + getShared(name) { const key = `shared/hugo/${name}.hugo.html` return this.files?.[key];