Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): allow passing entry options via script query #2500

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/core/src/helpers/web.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { IRunOptions } from '../types';

export function injectScript(win: Window, rootComId: string, scriptUrl: string) {
return new Promise<Window>((res, rej) => {
// This is the contract of the communication to get the root communication id
Expand All @@ -9,3 +11,20 @@ export function injectScript(win: Window, rootComId: string, scriptUrl: string)
win.document.head.appendChild(scriptEl);
});
}

interface EngineWebEntryGlobalObj {
document?: Document,
location?: Location

engineEntryOptions(options: { urlParams: URLSearchParams; envName: string }): IRunOptions;
}

export function getEngineEntryOptions(envName: string, globalObj: EngineWebEntryGlobalObj): IRunOptions {
const urlParams = new URLSearchParams(globalObj?.location?.search);
const currentScript = globalObj?.document?.currentScript;

const optionsFromScript = new URLSearchParams(currentScript && currentScript.dataset.engineRunOptions || undefined);
const injectedOptions = globalObj?.engineEntryOptions?.({ urlParams, envName }) ?? new URLSearchParams('');

return new Map([...optionsFromScript, ...urlParams, ...injectedOptions]);
}
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ export interface IRunOptions {
has(key: string): boolean;
get(key: string): string | string[] | boolean | null | undefined;
entries(): IterableIterator<[string, string | string[] | boolean | null | undefined]>;

[Symbol.iterator](): IterableIterator<[string, string | string[] | boolean | null | undefined]>;
}

export type RegisteringFeature<
Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect } from 'chai';
import { getEngineEntryOptions } from '@wixc3/engine-core';

describe('helpers', () => {
describe('getEngineEntryOptions', () => {
it('it gets engine entry options from query string, from script url, from engineEntryOptions', () => {
const document = { currentScript: { dataset: { engineRunOptions: 'http://localhost:3000?one=0&two=1&script=123' } } } as unknown as Document;
const location = { search: 'one=2&two=2&search=123' } as Location;
const engineEntryOptions = ({ envName }: { envName: string }) => {
if (envName === 'thisTest') {
return new URLSearchParams([['injected', '123'], ['one', '1']]);
}
return new URLSearchParams();
};
const result = getEngineEntryOptions('thisTest', { document, location, engineEntryOptions });

expect(result.get('one'), 'Priorities injected options over search string and script url').to.eql('1');
expect(result.get('two'), 'Priorities search params over script url').to.eql('2');
expect(result.get('script'), 'Gets options from script url').to.eql('123');
expect(result.get('search'), 'Gets options from search string').to.eql('123');
expect(result.get('injected'), 'Gets options from engineEntryOptions').to.eql('123');
});
});
});

5 changes: 2 additions & 3 deletions packages/scripts/src/create-web-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ export function createMainEntrypoint({
);

return `
import { main, COM } from '@wixc3/engine-core';
import { main, COM, getEngineEntryOptions } from '@wixc3/engine-core';

const urlParams = new URLSearchParams(globalThis.location.search);
const options = globalThis.engineEntryOptions?.({ urlParams, envName: ${stringify(env.name)} }) ?? urlParams;
const options = getEngineEntryOptions(${stringify(env.name)}, globalThis)
const runtimePublicPath = ${runtimePublicPath};
main({
featureName: ${stringify(featureName)},
Expand Down