Skip to content

Commit

Permalink
subapp2 fixes and updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jan 2, 2021
1 parent ab7a3e4 commit e020d24
Show file tree
Hide file tree
Showing 20 changed files with 252 additions and 137 deletions.
11 changes: 11 additions & 0 deletions packages/xarc-app-dev/src/config/opt2/xarc-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,15 @@ export type XarcOptions = {
* e.g: loadXarcDevTasks(null, { cwd: "/actual-dir-of-app" });
*/
cwd?: string;

/**
* Specify a namespace prefix for your application's JS/Style asset bundles
* For example, instead of `main.bundle.js`, it's `{ns}.main.bundle.js`
*
* This is useful if you want to be able to mix subapps from different apps
* on the same page, or if you want to implement a top level app shell that
* loads subapps from different sources and render them on the page.
*
*/
namespace?: string;
};
5 changes: 2 additions & 3 deletions packages/xarc-react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"scripts": {
"build": "xrun user/build",
"test": "xrun xarc/test-only",
"check": "xrun xarc/check",
"coverage": "xrun xarc/check",
"prepublishOnly": "xrun [[build, docs], xarc/check]",
"docs": "xrun xarc/docs",
"coverage": "xrun xarc/test-cov"
"docs": "xrun xarc/docs"
},
"keywords": [],
"author": "WalmartLabs GTP.js team",
Expand Down
5 changes: 2 additions & 3 deletions packages/xarc-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"scripts": {
"build": "xrun user/build",
"test": "xrun xarc/test-only",
"check": "xrun xarc/check",
"coverage": "xrun xarc/check",
"prepublishOnly": "xrun [[build, docs], xarc/check]",
"docs": "xrun xarc/docs",
"coverage": "xrun xarc/test-cov"
"docs": "xrun xarc/docs"
},
"keywords": [],
"author": "Electrode",
Expand Down
39 changes: 18 additions & 21 deletions packages/xarc-react/src/node/render-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
isSubAppReady,
subAppReady,
LoadSubAppOptions,
AssetPathMap,
InitProps,
NonceInfo
} from "./index";

Expand Down Expand Up @@ -39,7 +39,7 @@ export type TemplateInserts = {
};
};

export type PageOptions = {
export type PageOptions = InitProps & {
/**
* Name of subapps to load and render on the page
*/
Expand All @@ -58,21 +58,6 @@ export type PageOptions = {
/** meta charset, default: "UTF-8", set to false to disable */
charSet?: string | boolean;

/**
* Mapping of base path for static assets. default: `{ base: "/js" }`
*
* @remarks Only used when env `WEBPACK_DEV` is not defined
*
*/
assetPathMap?: AssetPathMap;

/**
* path to JSON file with CDN mapping data or the actual mapping data
*
* @remarks Only used when `NODE_ENV` is `production`
*/
cdnMap?: string | Record<string, string>;

/**
* Allows you to insert template tags at some predefined locations within the
* main template. You can create template tags with the createTemplateTags API.
Expand Down Expand Up @@ -105,25 +90,32 @@ export type RenderOptions = {
* Renderer to render a page with xarc subapps on it
*/
export class PageRenderer {
private _options: PageOptions;
private _template: any[];
private _renderer: TagRenderer;

constructor(options: PageOptions) {
this._options = options;
const { subApps } = options;
const { cdnMap, templateInserts: { head = {}, body = {} } = {} } = options;
const {
nonce,
prodAssetData,
devAssetData,
templateInserts: { head = {}, body = {} } = {}
} = options;

const { charSet = "UTF-8" } = options;

const charSetStr = charSet ? `\n<meta charset="${charSet}">\n` : "";

const assetPathMap = (!process.env.WEBPACK_DEV && options.assetPathMap) || { base: "/js" };
const initProps: InitProps = { prodAssetData, devAssetData, nonce };

this._template = createTemplateTags`<!doctype html>
<html>
<head>${charSetStr}
${head.begin}
${options.pageTitle && `<title>${options.pageTitle}</title>`}
${TokenInvoke(initSubApp, { assetPathMap, cdnMap, nonce: options.nonce })}
${TokenInvoke(initSubApp, initProps)}
${head.afterInit}
${head.end}
</head>
Expand All @@ -141,14 +133,19 @@ ${body.end}
this._renderer = new TagRenderer({ templateTags: this._template });
}

_getSSRSubAppNames() {
const { subApps } = this._options;
return subApps.map(s => s.ssr && s.name).filter(x => x);
}

/**
* Render index.html with subapps
*
* @returns Promise<RenderContext>
*/
async render(options: RenderOptions): Promise<RenderContext> {
if (!isSubAppReady()) {
await subAppReady();
await subAppReady(this._getSSRSubAppNames());
}

return await this._renderer.render(options);
Expand Down
5 changes: 2 additions & 3 deletions packages/xarc-redux/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"scripts": {
"build": "xrun user/build",
"test": "xrun xarc/test-only",
"check": "xrun xarc/check",
"coverage": "xrun xarc/check",
"prepublishOnly": "xrun [[build, docs], xarc/check]",
"docs": "xrun xarc/docs",
"coverage": "xrun xarc/test-cov"
"docs": "xrun xarc/docs"
},
"keywords": [],
"author": "WalmartLabs GTP.js team",
Expand Down
5 changes: 2 additions & 3 deletions packages/xarc-subapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
"scripts": {
"build": "xrun user/build",
"test": "xrun xarc/test-only",
"check": "xrun xarc/check",
"coverage": "xrun xarc/check",
"prepublishOnly": "xrun [[build, docs], xarc/check]",
"docs": "xrun xarc/docs",
"coverage": "xrun xarc/test-cov"
"docs": "xrun xarc/docs"
},
"files": [
"dist",
Expand Down
8 changes: 4 additions & 4 deletions packages/xarc-subapp/src/browser/xarc-cdn-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-env browser */
/* global globalThis */

import { XarcSubAppClientV2 } from "../subapp/index";
import { XarcSubAppClientV2, CDNData } from "../subapp/index";
//
// add CDN mapping support to xarc subapp client side lib
//
Expand All @@ -17,11 +17,11 @@ export function xarcCdnMap(
if (!xv2) return;

// cdnUpdate({md: {}}) - add mapping data to the CDN mapping resource
xv2.cdnUpdate = data => {
xv2.cdnUpdate = (data: CDNData, replace: boolean) => {
const md = data.md;
for (const k in md) {
if (xv2.rt.md[k]) {
console.error("CDN map already exist:", k);
if (xv2.rt.md[k] && !replace) {
console.error("replace falsy but CDN map already exist:", k);
} else {
xv2.rt.md[k] = md[k];
}
Expand Down
20 changes: 6 additions & 14 deletions packages/xarc-subapp/src/browser/xarc-subapp-v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-console, no-return-assign, prefer-const, prefer-spread, prefer-rest-params */
/* eslint-disable max-statements */
/* eslint-env browser */
/* global globalThis */
/**
Expand All @@ -9,7 +10,8 @@ import {
LoadSubAppOptions,
SubAppDef,
SubAppStartOptions,
XarcSubAppClientV2
XarcSubAppClientV2,
_xarcV2RunTimeInfo
} from "../subapp/index";

//
Expand All @@ -35,13 +37,12 @@ export function xarcV2Client(

if (w.xarcV2 && w.xarcV2.version >= version) return w.xarcV2;

const runtimeInfo = {
const runtimeInfo: _xarcV2RunTimeInfo = {
instId: 1,
subApps: {},
bundles: {},
onLoadStart: {},
groups: {},
started: false
started: false,
md: {}
};

let xv2: XarcSubAppClientV2;
Expand Down Expand Up @@ -97,15 +98,6 @@ export function xarcV2Client(
ols[name] = [];
}

const group = options.group;
if (group) {
let groupInfo = runtimeInfo.groups[group];
if (!groupInfo) {
groupInfo = runtimeInfo.groups[group] = { group: group, total: 0, queue: [] };
}
groupInfo.total++;
}

ols[name].push(Object.assign({}, options, data));
},

Expand Down
11 changes: 5 additions & 6 deletions packages/xarc-subapp/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
// re-exports
//
export * from "../subapp/index";
export * from "./types";
export * from "./xarc-subapp-v2-node";
export { loadSubApp } from "./load-v2";
export { initSubApp, AssetPathMap, NonceInfo } from "./init-v2";
export { initSubApp } from "./init-v2";
export { startSubApp } from "./start-v2";

const CONTAINER: SubAppContainer = {};
Expand Down Expand Up @@ -110,17 +111,15 @@ export async function subAppReady(list: boolean | string[] = false): Promise<any
}
}

for (const promise of subappModules) {
await promise;
}
await Promise.all(subappModules);

readySubAppCount = beforeWaitCount;

// if loading a subapp module triggered more subapps to be declared, then
// need to ensure those are ready also.
if (beforeWaitCount !== declareSubAppCount) {
await subAppReady();
}

readySubAppCount = declareSubAppCount;
}

/**
Expand Down
Loading

0 comments on commit e020d24

Please sign in to comment.