Skip to content

Commit

Permalink
Merge pull request #22 from camptocamp/fix-worker-build
Browse files Browse the repository at this point in the history
Fix worker serialization in build script
  • Loading branch information
jahow authored Jan 4, 2024
2 parents 68636ab + 810f9a1 commit ab059d1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
14 changes: 9 additions & 5 deletions build/generate-dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ async function serializeWorker(entryPath) {
const bundle = await rollup({
input: entryPath,
plugins,
});
const { output } = await bundle.generate({
format: 'es',
inlineDynamicImports: true,
});
const { output } = await bundle.generate({ format: 'es' });

if (output.length !== 1) {
throw new Error(`Unexpected output length: ${output.length}`);
Expand All @@ -42,7 +44,9 @@ async function serializeAllWorkersInCode(fileName, code) {
srcRoot,
path.relative(distRoot, path.dirname(fileName))
);
const workerMatches = codeResult.matchAll(/new Worker\('([^']+)'/g);
const workerMatches = codeResult.matchAll(
/new Worker\(new URL\('(.+)', import\.meta\.url\)/g
);
for (const match of workerMatches) {
const workerPath = path.join(dir, match[1]);
console.log(
Expand All @@ -53,7 +57,7 @@ async function serializeAllWorkersInCode(fileName, code) {
);
const workerCode = await serializeWorker(workerPath);
codeResult = codeResult.replace(
`new Worker('${match[1]}'`,
`new Worker(new URL('${match[1]}', import.meta.url)`,
`new Worker(URL.createObjectURL(new Blob([${JSON.stringify(
workerCode
)}], {type: 'application/javascript'}))`
Expand Down Expand Up @@ -104,16 +108,16 @@ async function main() {
}),
typescript(),
],
inlineDynamicImports: true,
});
await nodeBundle.write({
file: path.join(distRoot, './dist-node.js'),
format: 'cjs',
inlineDynamicImports: true,
});

console.log(
`
> files successfully build in ${distRoot} folder.`
> files successfully built in ${distRoot} folder.`
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ogc-api/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ export default class OgcApiEndpoint {
url.pathname += `/${itemId}`;
return url.toString();
})
.then(fetchDocument);
.then(fetchDocument<OgcApiCollectionItem>);
}
}
6 changes: 4 additions & 2 deletions src/ogc-api/link-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { OgcApiDocument } from './model';
import { EndpointError } from '../shared/errors';
import { getFetchOptions } from '../shared/http-utils';

export function fetchDocument(url: string): Promise<OgcApiDocument> {
export function fetchDocument<T extends OgcApiDocument>(
url: string
): Promise<T> {
const urlObj = new URL(url, window.location.toString());
urlObj.searchParams.set('f', 'json');
const options = getFetchOptions();
const optionsHeaders = 'headers' in options ? options.headers : {};
return fetch(urlObj.toString(), {
...options,
headers: { ...optionsHeaders, Accept: 'application/json' },
}).then((resp) => resp.json());
}).then((resp) => resp.json() as Promise<T>);
}

export function fetchRoot(url: string) {
Expand Down
42 changes: 27 additions & 15 deletions src/ogc-api/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Feature } from 'geojson';
import { Feature, Geometry } from 'geojson';
import { BoundingBox, CrsCode } from '../shared/models';

export type ConformanceClass = string;
Expand Down Expand Up @@ -54,8 +54,6 @@ export interface OgcApiCollectionInfo {
sortables: CollectionParameter[];
}

export type OgcApiCollectionItem = Feature | unknown;

export interface OgcApiDocumentLinks {
rel: string;
type: string;
Expand Down Expand Up @@ -84,28 +82,42 @@ interface OgcApiRecordTheme {
concepts: OgcApiRecordThemeConcept[];
scheme: string;
}
interface OgcApiTime {
date?: string;
timestamp?: string;
interval?: [string, string];
resolution?: string;
}
export interface OgcApiRecordContact {
name: string;
links: OgcApiDocumentLinks[];
contactInstructions: string;
roles: string[];
}
export interface OgcApiRecordProperties {
recordCreated: Date;
recordUpdated: Date;
providers: string[];
type: string;
title: string;
description: string;
externalId: OgcApiItemExternalId[];
themes: OgcApiRecordTheme[];
keywords: string[];
language: string;
contacts: OgcApiRecordContact[];
formats: string[];
license: string;
recordCreated?: Date;
recordUpdated?: Date;
providers?: string[];
description?: string;
externalId?: OgcApiItemExternalId[];
themes?: OgcApiRecordTheme[];
keywords?: string[];
language?: string;
contacts?: OgcApiRecordContact[];
formats?: string[];
license?: string;
}

export type OgcRecord = Feature<OgcApiRecordProperties> & {
export type OgcApiRecord = {
id: string;
type: string;
time: OgcApiTime;
geometry: Geometry;
properties: OgcApiRecordProperties;
links: OgcApiDocumentLinks[];
conformsTo?: string[];
};

export type OgcApiCollectionItem = OgcApiRecord;

0 comments on commit ab059d1

Please sign in to comment.