From 9b333e4c71823e41dfbf7377a62262e672b4650d Mon Sep 17 00:00:00 2001 From: eyelidlessness Date: Mon, 16 Dec 2024 09:44:02 -0800 Subject: [PATCH] Fix: Web Forms Preview demo form links MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The nature of this fix is that Vite’s `import.meta.glob` produces a build-time asset URL, which we used previously, but missed in the generalization of glob import loading. This was missed because the dev/test asset URL is identical to the relative path key which we did use in that generalization. Since this implicates an important difference betwen dev/test and build, an e2e test will follow to exercise loading demo forms directly from the Web Forms Preview build product. --- .../common/src/fixtures/import-glob-helper.ts | 20 ++++++++++++++----- .../common/src/fixtures/xform-attachments.ts | 6 +++--- packages/common/src/fixtures/xforms.ts | 15 +++++--------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/common/src/fixtures/import-glob-helper.ts b/packages/common/src/fixtures/import-glob-helper.ts index e89553863..708af1426 100644 --- a/packages/common/src/fixtures/import-glob-helper.ts +++ b/packages/common/src/fixtures/import-glob-helper.ts @@ -34,11 +34,16 @@ if (IS_NODE_RUNTIME) { type ImportMetaGlobURLRecord = Readonly>; -export type ImportMetaGlobLoader = (this: void) => Promise; +export type GlobFixtureLoader = (this: void) => Promise; -export type GlobLoaderEntry = readonly [absolutePath: string, loader: ImportMetaGlobLoader]; +export interface GlobFixture { + readonly url: URL; + readonly load: GlobFixtureLoader; +} + +export type GlobFixtureEntry = readonly [absolutePath: string, loader: GlobFixture]; -const globLoader = (globURL: string): ImportMetaGlobLoader => { +const globFixtureLoader = (globURL: string): GlobFixtureLoader => { return async () => { const response = await fetchGlobURL(globURL); @@ -49,12 +54,17 @@ const globLoader = (globURL: string): ImportMetaGlobLoader => { export const toGlobLoaderEntries = ( importMeta: ImportMeta, globObject: ImportMetaGlobURLRecord -): readonly GlobLoaderEntry[] => { +): readonly GlobFixtureEntry[] => { const parentPathURL = new URL('./', importMeta.url); return Object.entries(globObject).map(([relativePath, value]) => { const { pathname: absolutePath } = new URL(relativePath, parentPathURL); + const fixtureAssetURL = new URL(value, import.meta.url); + const fixture: GlobFixture = { + url: fixtureAssetURL, + load: globFixtureLoader(value), + }; - return [absolutePath, globLoader(value)]; + return [absolutePath, fixture]; }); }; diff --git a/packages/common/src/fixtures/xform-attachments.ts b/packages/common/src/fixtures/xform-attachments.ts index 22d06ce27..949965a3c 100644 --- a/packages/common/src/fixtures/xform-attachments.ts +++ b/packages/common/src/fixtures/xform-attachments.ts @@ -1,6 +1,6 @@ import { UpsertableMap } from '../lib/collections/UpsertableMap.ts'; import { UnreachableError } from '../lib/error/UnreachableError.ts'; -import type { ImportMetaGlobLoader } from './import-glob-helper.ts'; +import type { GlobFixtureLoader } from './import-glob-helper.ts'; import { toGlobLoaderEntries } from './import-glob-helper.ts'; /** @@ -60,7 +60,7 @@ export class XFormAttachmentFixture { constructor( readonly absolutePath: string, - readonly load: ImportMetaGlobLoader + readonly load: GlobFixtureLoader ) { const fileName = getFileName(absolutePath); const fileExtension = getFileExtension(fileName); @@ -97,7 +97,7 @@ type XFormAttachmentFixtureEntry = readonly [absolutePath: string, fixture: XFor type XFormAttachmentFixtureEntries = readonly XFormAttachmentFixtureEntry[]; const xformAttachmentFixtureEntries: XFormAttachmentFixtureEntries = - xformAttachmentFixtureLoaderEntries.map(([absolutePath, load]) => { + xformAttachmentFixtureLoaderEntries.map(([absolutePath, { load }]) => { const fixture = new XFormAttachmentFixture(absolutePath, load); return [absolutePath, fixture]; diff --git a/packages/common/src/fixtures/xforms.ts b/packages/common/src/fixtures/xforms.ts index 1c27e1f28..6270bda49 100644 --- a/packages/common/src/fixtures/xforms.ts +++ b/packages/common/src/fixtures/xforms.ts @@ -1,6 +1,7 @@ import { JRResourceService } from '../jr-resources/JRResourceService.ts'; import type { JRResourceURL } from '../jr-resources/JRResourceURL.ts'; import { UpsertableMap } from '../lib/collections/UpsertableMap.ts'; +import type { GlobFixture } from './import-glob-helper.ts'; import { toGlobLoaderEntries } from './import-glob-helper.ts'; type XFormResourceType = 'local' | 'remote'; @@ -92,12 +93,8 @@ const getNoopResourceService: ResourceServiceFactory = () => { }; export class XFormResource { - static forLocalFixture( - localPath: string, - resourceURL: URL, - loadXML?: LoadXFormXML - ): XFormResource<'local'> { - return new XFormResource('local', resourceURL, loadXML ?? xformURLLoader(resourceURL), { + static forLocalFixture(localPath: string, fixture: GlobFixture): XFormResource<'local'> { + return new XFormResource('local', fixture.url, fixture.load, { category: localFixtureDirectoryCategory(localPath), localPath, identifier: pathToFileName(localPath), @@ -168,10 +165,8 @@ const xformFixtureLoaderEntries = toGlobLoaderEntries( export type XFormFixture = XFormResource<'local'>; const buildXFormFixtures = (): readonly XFormFixture[] => { - return xformFixtureLoaderEntries.map(([path, loadXML]) => { - const resourceURL = new URL(path, SELF_URL); - - return XFormResource.forLocalFixture(path, resourceURL, loadXML); + return xformFixtureLoaderEntries.map(([path, fixture]) => { + return XFormResource.forLocalFixture(path, fixture); }); };