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 all 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
22 changes: 22 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,23 @@ export function injectScript(win: Window, rootComId: string, scriptUrl: string)
win.document.head.appendChild(scriptEl);
});
}

interface EngineWebEntryGlobalObj {
document?: { currentScript: { dataset: { engineRunOptions?: string | null } } | null },
location?: { search?: string }

engineEntryOptions?: (options: { currentRunOptions: IRunOptions; 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 optionsBeforeInject = new Map([...optionsFromScript, ...urlParams]);
const optionsAfterInject = globalObj?.engineEntryOptions?.({
currentRunOptions: optionsBeforeInject,
envName
});
return optionsAfterInject || optionsBeforeInject;
}
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
60 changes: 60 additions & 0 deletions packages/core/test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect } from 'chai';
import { getEngineEntryOptions, IRunOptions } 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' } },
};
const location = { search: 'one=2&two=2&search=123' };
const engineEntryOptions = ({
envName,
currentRunOptions,
}: {
envName: string;
currentRunOptions: IRunOptions;
}) => {
if (envName === 'thisTest') {
return new Map([...currentRunOptions, ['injected', '123'], ['one', '1']]);
}
return new Map();
};
const result = getEngineEntryOptions('thisTest', { document, location, engineEntryOptions });

expect(result.get('one'), 'Prioritises injected options over search string and script url').to.eql('1');
expect(result.get('two'), 'Prioritises 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');
});
it('works without inject function', () => {
const document = {
currentScript: { dataset: { engineRunOptions: 'http://localhost:3000?one=0&script=123' } },
};
const location = { search: 'one=1&search=123' };
const result = getEngineEntryOptions('thisTest', { document, location });

expect(result.get('one'), 'Prioritises injected options over search string and script url').to.eql('1');
expect(result.get('script'), 'Gets options from script url').to.eql('123');
expect(result.get('search'), 'Gets options from search string').to.eql('123');
});
it('allows to override options completely with getEngineEntryOptions', () => {
const document = {
currentScript: { dataset: { engineRunOptions: 'http://localhost:3000?script=123' } },
};
const location = { search: 'search=123' };
const engineEntryOptions = ({ envName }: { envName: string }) => {
if (envName === 'thisTest') {
return new Map([['injected', '123']]);
}
return new Map();
};
const result = getEngineEntryOptions('thisTest', { document, location, engineEntryOptions });

expect(result.get('script'), 'Ignores options from script url').to.eql(undefined);
expect(result.get('search'), 'Ignores options from search string').to.eql(undefined);
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