Skip to content

Commit

Permalink
Fix: Web Forms Preview demo form links
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eyelidlessness committed Dec 16, 2024
1 parent 0acbbc2 commit 9b333e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
20 changes: 15 additions & 5 deletions packages/common/src/fixtures/import-glob-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ if (IS_NODE_RUNTIME) {

type ImportMetaGlobURLRecord = Readonly<Record<string, string>>;

export type ImportMetaGlobLoader = (this: void) => Promise<string>;
export type GlobFixtureLoader = (this: void) => Promise<string>;

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);

Expand All @@ -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];
});
};
6 changes: 3 additions & 3 deletions packages/common/src/fixtures/xform-attachments.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
15 changes: 5 additions & 10 deletions packages/common/src/fixtures/xforms.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -92,12 +93,8 @@ const getNoopResourceService: ResourceServiceFactory = () => {
};

export class XFormResource<Type extends XFormResourceType> {
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),
Expand Down Expand Up @@ -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);
});
};

Expand Down

0 comments on commit 9b333e4

Please sign in to comment.