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

refactor(scripts): use parent to fetch config in entrypoint #909

Merged
merged 4 commits into from
Aug 24, 2021
Merged
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
68 changes: 60 additions & 8 deletions packages/scripts/src/create-entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ async function main() {
const currentWindow = typeof self !== 'undefined' ? self : window;
const topWindow = getTopWindow(currentWindow);
const options = new URLSearchParams(topWindow.location.search);
const isMainEntrypoint = topWindow && currentWindow === topWindow;

const publicPath = options.has('publicPath') ? options.get('publicPath') : ${
typeof publicPath === 'string' ? JSON.stringify(publicPath) : '__webpack_public_path__'
Expand All @@ -136,11 +137,9 @@ async function main() {
const featureName = options.get('${FEATURE_QUERY_PARAM}') || ${stringify(featureName)};
const configName = options.get('${CONFIG_QUERY_PARAM}') || ${stringify(configName)};
const config = [];

${staticBuild ? importStaticConfigs() : ''}
${staticBuild && config ? addOverrideConfig(config) : ''}

${publicConfigsRoute ? fetchConfigs(publicConfigsRoute, envName) : ''}

${populateConfig(envName, staticBuild, publicConfigsRoute, config)}

const rootFeatureLoader = featureLoaders.get(featureName);
if(!rootFeatureLoader) {
throw new Error("cannot find feature '" + featureName + "'. available features:\\n" + Array.from(featureLoaders.keys()).join('\\n'));
Expand Down Expand Up @@ -365,10 +364,29 @@ function loadConfigFile(filePath: string, scopedName: string, configEnvName: str
//#endregion

//#region configs
function populateConfig(envName: string, staticBuild?: boolean, publicConfigsRoute?: string, config?: TopLevelConfig) {
return `${staticBuild ? importStaticConfigs() : ''}
${staticBuild && config ? addOverrideConfig(config) : ''}

${publicConfigsRoute ? getRemoteConfigs(publicConfigsRoute, envName) : ''}

${publicConfigsRoute ? `${addConfigsEventListenerForParentEnvironments(publicConfigsRoute)}` : ''}`;
RomanYarik marked this conversation as resolved.
Show resolved Hide resolved
}

function getRemoteConfigs(publicConfigsRoute: string, envName: string) {
return `config.push(...await (async () =>{
if(!isMainEntrypoint) {
${getConfigsFromParent(publicConfigsRoute, envName)}
} else {
${fetchConfigs(publicConfigsRoute, envName)}
}
})());`;
}

function fetchConfigs(publicConfigsRoute: string, envName: string) {
return `config.push(...await (await fetch('${normalizeRoute(
return `return (await fetch('${normalizeRoute(
publicConfigsRoute
)!}' + configName + '?env=${envName}&feature=' + featureName)).json());`;
)!}' + configName + '?env=${envName}&feature=' + featureName)).json();`;
}

function addOverrideConfig(config: TopLevelConfig) {
Expand All @@ -386,13 +404,47 @@ function importStaticConfigs() {
}`;
}

function addConfigsEventListenerForParentEnvironments(publicConfigsRoute: string) {
return `if(isMainEntrypoint) {
const fetchedConfigs = {};
const configsEventListener = async ({ data: { id, envName }, source }) => {
if(source && id === '${publicConfigsRoute}') {
RomanYarik marked this conversation as resolved.
Show resolved Hide resolved
if(!fetchedConfigs[envName]) {
const config = await (await fetch('configs/' + configName + '?env=' + envName + '&feature=' + featureName)).json();
fetchedConfigs[envName] = config;
}
source.postMessage({
id,
config: fetchedConfigs[envName]
});
}
}
currentWindow.addEventListener('message', configsEventListener);
}`;
}

function getConfigsFromParent(publicConfigsRoute: string, envName: string) {
return `return new Promise((res) => {
const configsHandler = ({ data: { id, config } }) => {
if(id === '${publicConfigsRoute}') {
currentWindow.removeEventListener('message', configsHandler);
res(config);
}
};
currentWindow.addEventListener('message', configsHandler)
topWindow.postMessage({
id: '${publicConfigsRoute}',
envName: '${envName}'
});
});`;
}

//#endregion

//#region loading 3rd party features
function loadExternalFeatures(target: 'web' | 'webworker' | 'electron-renderer', externalsFilePath: string) {
return `self.runtimeFeatureLoader = featureLoader;
const externalFeatures = [];
const isMainEntrypoint = topWindow && currentWindow === topWindow;

${addExternalsEventListenerForParentEnvironments(externalsFilePath)}

Expand Down