From 4e001d5199f804e76eb637362a2902f1a4b8eb25 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 31 May 2023 12:04:58 +0200 Subject: [PATCH] fix: buildIdPlugin breaks when Fresh is hosted (#1230) Only file:// URLs were supported previously, which breaks if you are importing Fresh via HTTPS URLs. --- src/server/bundle.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/server/bundle.ts b/src/server/bundle.ts index e39135bf..aba02f62 100644 --- a/src/server/bundle.ts +++ b/src/server/bundle.ts @@ -170,12 +170,20 @@ export class Bundler { function buildIdPlugin(buildId: string): esbuildTypes.Plugin { const file = import.meta.resolve("../runtime/build_id.ts"); const url = new URL(file); - const path = fromFileUrl(url); + let options: esbuildTypes.OnLoadOptions; + if (url.protocol === "file:") { + const path = fromFileUrl(url); + options = { filter: new RegExp(escape(path)), namespace: "file" }; + } else { + const namespace = url.protocol.slice(0, -1); + const path = url.href.slice(namespace.length + 1); + options = { filter: new RegExp(escape(path)), namespace }; + } return { name: "fresh-build-id", setup(build) { build.onLoad( - { filter: new RegExp(escape(path)), namespace: "file" }, + options, () => ({ contents: `export const BUILD_ID = "${buildId}";` }), ); },