From 84fd214db0b5bc72584d006864559f1db64dd175 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Wed, 8 Jul 2020 17:13:46 -0700 Subject: [PATCH 01/12] Changes to support loading subapps from a different repo --- .../subapp-server/lib/setup-hapi-routes.js | 6 ++- packages/subapp-util/lib/index.js | 50 +++++++++++++++---- packages/xarc-webpack/lib/partials/entry.js | 14 ++++-- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 3916eb3af..0528634a4 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -108,12 +108,16 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { routeOptions.__internals.subApps = [].concat(routeOptions.subApps).map(x => { let options = {}; if (Array.isArray(x)) { - options = x[1]; + options = x[1] || {}; x = x[0]; } // absolute: use as path // else: assume dir under srcDir // TBD: handle it being a module + if (x.indexOf("/") === -1) { + const xSrcDir = options.srcDir || "lib"; + x = Path.resolve("node_modules", x, xSrcDir); + } return { subapp: subAppsByPath[Path.isAbsolute(x) ? x : Path.resolve(srcDir, x)], options diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 098c19581..1c6381dfe 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -122,11 +122,12 @@ function scanSubAppsFromDir(srcDir, maxLevel = Infinity) { const fullSrcDir = Path.resolve(srcDir); const subApps = { [MAP_BY_PATH_SYM]: {} }; - const { maniFiles = [], files = [] } = scanDir.sync({ + const { maniFiles = [], files = [], modFiles = [] } = scanDir.sync({ dir: srcDir, grouping: true, maxLevel, filter: (f, p, ex) => { + if (ex.noExt === "subapps-modules") return "modFiles"; if (ex.noExt === "subapp-manifest") return "maniFiles"; return f.startsWith("subapp"); }, @@ -162,7 +163,25 @@ function scanSubAppsFromDir(srcDir, maxLevel = Infinity) { return null; }); - const errors = [].concat(errors1, errors2).filter(x => x); + // process subapps modules + const errors3 = modFiles.map(module => { + try { + const manifests = es6Require(Path.join(fullSrcDir, module)); + Object.keys(manifests).forEach(modName => { + const manifest = manifests[modName]; + const modSrcDir = manifest.srcDir || "lib"; + const modFullSrcDir = Path.resolve("node_modules", modName, modSrcDir); + const subapp = Object.assign({ subAppDir: modName, module: true }, manifest); + scanSubAppAdditions(modFullSrcDir, subapp); + subApps[manifest.name] = subApps[MAP_BY_PATH_SYM][modFullSrcDir] = subapp; + }); + return null; + } catch (error) { + return error; + } + }); + + const errors = [].concat(errors1, errors2, errors3).filter(x => x); if (errors.length > 0) { console.error("Loading SubApps failed"); errors.forEach(err => { @@ -242,7 +261,12 @@ function loadSubAppByName(name) { const container = getSubAppContainer(); const subAppDir = manifest.subAppDir; // load subapp's entry - xrequire(Path.resolve(appSrcDir(), subAppDir, manifest.entry)); + const modSrcDir = manifest.srcDir || "lib"; + const fullSubappDir = manifest.module + ? Path.resolve("node_modules", subAppDir, modSrcDir) + : Path.resolve(appSrcDir(), subAppDir); + + xrequire(Path.join(fullSubappDir, manifest.entry)); // if subapp did not register itself then register it if (!container[name]) { @@ -257,14 +281,19 @@ function loadSubAppServerByName(name) { const manifest = subAppManifest()[name]; const { subAppDir, serverEntry } = manifest; + const modSrcDir = manifest.srcDir || "lib"; + const fullSubappDir = manifest.module + ? Path.resolve("node_modules", subAppDir, modSrcDir) + : Path.resolve(appSrcDir(), subAppDir); + if (serverEntry) { - return es6Require(Path.resolve(appSrcDir(), subAppDir, serverEntry)); + return es6Require(Path.join(fullSubappDir, serverEntry)); } else if (serverEntry === false) { return {}; } // generate a server from subapp's main file - const subapp = es6Require(Path.resolve(appSrcDir(), subAppDir, manifest.entry)); + const subapp = es6Require(Path.join(fullSubappDir, manifest.entry)); return { StartComponent: subapp.Component @@ -275,12 +304,15 @@ function refreshSubAppByName(name) { const manifest = subAppManifest()[name]; const { subAppDir } = manifest; - const entryFullPath = xrequire.resolve(Path.resolve(appSrcDir(), subAppDir, manifest.entry)); + const modSrcDir = manifest.srcDir || "lib"; + const fullSubappDir = manifest.module + ? Path.resolve("node_modules", subAppDir, modSrcDir) + : Path.resolve(appSrcDir(), subAppDir); + + const entryFullPath = xrequire.resolve(Path.join(fullSubappDir, manifest.entry)); if (!xrequire.cache[entryFullPath] && manifest.serverEntry) { // also reload server side module - const serverEntryFullPath = xrequire.resolve( - Path.resolve(appSrcDir(), subAppDir, manifest.serverEntry) - ); + const serverEntryFullPath = xrequire.resolve(Path.join(fullSubappDir, manifest.serverEntry)); console.log("reloading server side subapp", name, serverEntryFullPath); delete xrequire.cache[serverEntryFullPath]; } diff --git a/packages/xarc-webpack/lib/partials/entry.js b/packages/xarc-webpack/lib/partials/entry.js index b3eb2760c..f90023731 100644 --- a/packages/xarc-webpack/lib/partials/entry.js +++ b/packages/xarc-webpack/lib/partials/entry.js @@ -52,21 +52,27 @@ function makeEntryPartial() { } function genSubAppHmrEntry(hmrDir, isDev, manifest) { - let subAppReq = `${manifest.subAppDir}/${manifest.entry}`; + const modOutputDir = manifest.outputDir || "dist"; + let subAppReq = manifest.module + ? Path.resolve("node_modules", `${manifest.subAppDir}/${modOutputDir}/${manifest.entry}`) + : `${manifest.subAppDir}/${manifest.entry}`; + // subapp has built-in code to handle HMR accept // or not running in webpack dev mode // => do not generate HMR accept code if (manifest.hmrSelfAccept || !isDev) { - return `./${subAppReq}`; + return manifest.module ? subAppReq : `./${subAppReq}`; } const hmrEntry = `hmr-${manifest.subAppDir.replace(/[\/\\]/g, "-")}.js`; - subAppReq = `../${subAppReq}`; + subAppReq = manifest.module ? subAppReq : `../${subAppReq}`; let reducerHmrCode = ""; if (manifest.reducers) { - const subAppReducers = `../${manifest.subAppDir}/reducers`; + const subAppReducers = manifest.module + ? Path.resolve("node_modules", `${manifest.subAppDir}/${modOutputDir}/reducers`) + : `../${manifest.subAppDir}/reducers`; reducerHmrCode = ` import { getReduxCreateStore } from "subapp-redux"; import reducers from "${subAppReducers}"; From be865087e6463d50d4da1bdd821ac593d1f82a4f Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Fri, 10 Jul 2020 17:29:08 -0700 Subject: [PATCH 02/12] Minor updates --- packages/subapp-server/lib/setup-hapi-routes.js | 3 +-- packages/subapp-util/lib/index.js | 16 ++++++---------- packages/xarc-webpack/lib/partials/entry.js | 11 ++++++++--- packages/xarc-webpack/package.json | 1 + 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 0528634a4..dd14495c7 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -115,8 +115,7 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { // else: assume dir under srcDir // TBD: handle it being a module if (x.indexOf("/") === -1) { - const xSrcDir = options.srcDir || "lib"; - x = Path.resolve("node_modules", x, xSrcDir); + x = Path.dirname(require.resolve(`${x}`)); } return { subapp: subAppsByPath[Path.isAbsolute(x) ? x : Path.resolve(srcDir, x)], diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 1c6381dfe..25620c60a 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -169,11 +169,10 @@ function scanSubAppsFromDir(srcDir, maxLevel = Infinity) { const manifests = es6Require(Path.join(fullSrcDir, module)); Object.keys(manifests).forEach(modName => { const manifest = manifests[modName]; - const modSrcDir = manifest.srcDir || "lib"; - const modFullSrcDir = Path.resolve("node_modules", modName, modSrcDir); + const modFullDir = Path.dirname(require.resolve(`${modName}`)); const subapp = Object.assign({ subAppDir: modName, module: true }, manifest); - scanSubAppAdditions(modFullSrcDir, subapp); - subApps[manifest.name] = subApps[MAP_BY_PATH_SYM][modFullSrcDir] = subapp; + scanSubAppAdditions(modFullDir, subapp); + subApps[manifest.name] = subApps[MAP_BY_PATH_SYM][modFullDir] = subapp; }); return null; } catch (error) { @@ -261,9 +260,8 @@ function loadSubAppByName(name) { const container = getSubAppContainer(); const subAppDir = manifest.subAppDir; // load subapp's entry - const modSrcDir = manifest.srcDir || "lib"; const fullSubappDir = manifest.module - ? Path.resolve("node_modules", subAppDir, modSrcDir) + ? Path.dirname(require.resolve(`${subAppDir}`)) : Path.resolve(appSrcDir(), subAppDir); xrequire(Path.join(fullSubappDir, manifest.entry)); @@ -281,9 +279,8 @@ function loadSubAppServerByName(name) { const manifest = subAppManifest()[name]; const { subAppDir, serverEntry } = manifest; - const modSrcDir = manifest.srcDir || "lib"; const fullSubappDir = manifest.module - ? Path.resolve("node_modules", subAppDir, modSrcDir) + ? Path.dirname(require.resolve(`${subAppDir}`)) : Path.resolve(appSrcDir(), subAppDir); if (serverEntry) { @@ -304,9 +301,8 @@ function refreshSubAppByName(name) { const manifest = subAppManifest()[name]; const { subAppDir } = manifest; - const modSrcDir = manifest.srcDir || "lib"; const fullSubappDir = manifest.module - ? Path.resolve("node_modules", subAppDir, modSrcDir) + ? Path.dirname(require.resolve(`${subAppDir}`)) : Path.resolve(appSrcDir(), subAppDir); const entryFullPath = xrequire.resolve(Path.join(fullSubappDir, manifest.entry)); diff --git a/packages/xarc-webpack/lib/partials/entry.js b/packages/xarc-webpack/lib/partials/entry.js index f90023731..36d6c3524 100644 --- a/packages/xarc-webpack/lib/partials/entry.js +++ b/packages/xarc-webpack/lib/partials/entry.js @@ -11,6 +11,7 @@ const AppMode = archetype.AppMode; const chalk = require("chalk"); const logger = require("@xarc/app/lib/logger"); const mkdirp = require("mkdirp"); +const requireFromPath = require("require-from-path"); const DEV_HMR_DIR = ".__dev_hmr"; @@ -52,9 +53,13 @@ function makeEntryPartial() { } function genSubAppHmrEntry(hmrDir, isDev, manifest) { - const modOutputDir = manifest.outputDir || "dist"; + let moduleDir; + if (manifest.module) { + moduleDir = Path.dirname(require.resolve(`${manifest.subAppDir}`)); + } + let subAppReq = manifest.module - ? Path.resolve("node_modules", `${manifest.subAppDir}/${modOutputDir}/${manifest.entry}`) + ? requireFromPath(moduleDir).resolve(`./${manifest.entry}`) : `${manifest.subAppDir}/${manifest.entry}`; // subapp has built-in code to handle HMR accept @@ -71,7 +76,7 @@ function makeEntryPartial() { if (manifest.reducers) { const subAppReducers = manifest.module - ? Path.resolve("node_modules", `${manifest.subAppDir}/${modOutputDir}/reducers`) + ? requireFromPath(moduleDir).resolve(`./${manifest.reducers}`) : `../${manifest.subAppDir}/reducers`; reducerHmrCode = ` import { getReduxCreateStore } from "subapp-redux"; diff --git a/packages/xarc-webpack/package.json b/packages/xarc-webpack/package.json index bab20e4bc..1ec3e2415 100644 --- a/packages/xarc-webpack/package.json +++ b/packages/xarc-webpack/package.json @@ -42,6 +42,7 @@ "optimize-css-assets-webpack-plugin": "^5.0.1", "optional-require": "^1.0.0", "require-at": "^1.0.2", + "require-from-path": "^1.0.4", "url-loader": "^0.6.2", "webpack": "^4.41.0", "webpack-config-composer": "^1.1.3", From 19ce9aae9c3605be26de8a00464ba70978045bee Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Mon, 13 Jul 2020 16:48:06 -0700 Subject: [PATCH 03/12] use absolute path for manifest entries --- .../subapp-server/lib/setup-hapi-routes.js | 2 +- packages/subapp-util/lib/index.js | 65 +++++++++++-------- packages/xarc-webpack/lib/partials/entry.js | 11 +--- 3 files changed, 40 insertions(+), 38 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index dd14495c7..c8b12dce5 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -115,7 +115,7 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { // else: assume dir under srcDir // TBD: handle it being a module if (x.indexOf("/") === -1) { - x = Path.dirname(require.resolve(`${x}`)); + x = Path.dirname(require.resolve(`${x}/package.json`)); } return { subapp: subAppsByPath[Path.isAbsolute(x) ? x : Path.resolve(srcDir, x)], diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 25620c60a..a7d3bc110 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -5,6 +5,7 @@ const Url = require("url"); const Path = require("path"); const assert = require("assert"); +const _ = require("lodash"); const optionalRequire = require("optional-require")(require); const scanDir = require("filter-scan-dir"); const appMode = optionalRequire(Path.resolve(".prod/.app-mode.json"), { default: {} }); @@ -29,6 +30,15 @@ function removeExt(f) { return ix > 0 ? f.substring(0, ix) : f; } +const validateModuleManifest = manifest => { + const entries = _.pick(manifest, ["entry", "serverEntry", "reducers"]); + Object.keys(entries).forEach(x => { + if (!Path.isAbsolute(entries[x])) { + throw new Error(`Error: manifest ${x} requires absolute path`); + } + }); +}; + // // scan a subapp's directory for additional things: // - server side entry in server.js, server-{name}.js, server-{verbatimName}.js @@ -164,21 +174,22 @@ function scanSubAppsFromDir(srcDir, maxLevel = Infinity) { }); // process subapps modules - const errors3 = modFiles.map(module => { - try { - const manifests = es6Require(Path.join(fullSrcDir, module)); - Object.keys(manifests).forEach(modName => { + let errors3 = []; + if (modFiles.length > 0) { + const manifests = es6Require(Path.join(fullSrcDir, modFiles[0])); + errors3 = Object.keys(manifests).map(modName => { + try { const manifest = manifests[modName]; - const modFullDir = Path.dirname(require.resolve(`${modName}`)); + validateModuleManifest(manifest); + const modFullDir = Path.dirname(require.resolve(`${modName}/package.json`)); const subapp = Object.assign({ subAppDir: modName, module: true }, manifest); - scanSubAppAdditions(modFullDir, subapp); subApps[manifest.name] = subApps[MAP_BY_PATH_SYM][modFullDir] = subapp; - }); + } catch (error) { + return error; + } return null; - } catch (error) { - return error; - } - }); + }); + } const errors = [].concat(errors1, errors2, errors3).filter(x => x); if (errors.length > 0) { @@ -260,11 +271,7 @@ function loadSubAppByName(name) { const container = getSubAppContainer(); const subAppDir = manifest.subAppDir; // load subapp's entry - const fullSubappDir = manifest.module - ? Path.dirname(require.resolve(`${subAppDir}`)) - : Path.resolve(appSrcDir(), subAppDir); - - xrequire(Path.join(fullSubappDir, manifest.entry)); + xrequire(manifest.module ? manifest.entry : Path.resolve(appSrcDir(), subAppDir, manifest.entry)); // if subapp did not register itself then register it if (!container[name]) { @@ -279,18 +286,18 @@ function loadSubAppServerByName(name) { const manifest = subAppManifest()[name]; const { subAppDir, serverEntry } = manifest; - const fullSubappDir = manifest.module - ? Path.dirname(require.resolve(`${subAppDir}`)) - : Path.resolve(appSrcDir(), subAppDir); - if (serverEntry) { - return es6Require(Path.join(fullSubappDir, serverEntry)); + return es6Require( + manifest.module ? serverEntry : Path.resolve(appSrcDir(), subAppDir, serverEntry) + ); } else if (serverEntry === false) { return {}; } // generate a server from subapp's main file - const subapp = es6Require(Path.join(fullSubappDir, manifest.entry)); + const subapp = es6Require( + manifest.module ? manifest.entry : Path.resolve(appSrcDir(), subAppDir, manifest.entry) + ); return { StartComponent: subapp.Component @@ -301,14 +308,16 @@ function refreshSubAppByName(name) { const manifest = subAppManifest()[name]; const { subAppDir } = manifest; - const fullSubappDir = manifest.module - ? Path.dirname(require.resolve(`${subAppDir}`)) - : Path.resolve(appSrcDir(), subAppDir); - - const entryFullPath = xrequire.resolve(Path.join(fullSubappDir, manifest.entry)); + const entryFullPath = xrequire.resolve( + manifest.module ? manifest.entry : Path.resolve(appSrcDir(), subAppDir, manifest.entry) + ); if (!xrequire.cache[entryFullPath] && manifest.serverEntry) { // also reload server side module - const serverEntryFullPath = xrequire.resolve(Path.join(fullSubappDir, manifest.serverEntry)); + const serverEntryFullPath = xrequire.resolve( + manifest.module + ? manifest.serverEntry + : Path.resolve(appSrcDir(), subAppDir, manifest.serverEntry) + ); console.log("reloading server side subapp", name, serverEntryFullPath); delete xrequire.cache[serverEntryFullPath]; } diff --git a/packages/xarc-webpack/lib/partials/entry.js b/packages/xarc-webpack/lib/partials/entry.js index 36d6c3524..82fb47efc 100644 --- a/packages/xarc-webpack/lib/partials/entry.js +++ b/packages/xarc-webpack/lib/partials/entry.js @@ -53,14 +53,7 @@ function makeEntryPartial() { } function genSubAppHmrEntry(hmrDir, isDev, manifest) { - let moduleDir; - if (manifest.module) { - moduleDir = Path.dirname(require.resolve(`${manifest.subAppDir}`)); - } - - let subAppReq = manifest.module - ? requireFromPath(moduleDir).resolve(`./${manifest.entry}`) - : `${manifest.subAppDir}/${manifest.entry}`; + let subAppReq = manifest.module ? manifest.entry : `${manifest.subAppDir}/${manifest.entry}`; // subapp has built-in code to handle HMR accept // or not running in webpack dev mode @@ -76,7 +69,7 @@ function makeEntryPartial() { if (manifest.reducers) { const subAppReducers = manifest.module - ? requireFromPath(moduleDir).resolve(`./${manifest.reducers}`) + ? manifest.reducers : `../${manifest.subAppDir}/reducers`; reducerHmrCode = ` import { getReduxCreateStore } from "subapp-redux"; From a65312bc49ff57df93702974029b0d81ee9b61f1 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Mon, 13 Jul 2020 17:12:40 -0700 Subject: [PATCH 04/12] Remove unused dependencies --- packages/xarc-webpack/lib/partials/entry.js | 1 - packages/xarc-webpack/package.json | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/xarc-webpack/lib/partials/entry.js b/packages/xarc-webpack/lib/partials/entry.js index 82fb47efc..84d7d784b 100644 --- a/packages/xarc-webpack/lib/partials/entry.js +++ b/packages/xarc-webpack/lib/partials/entry.js @@ -11,7 +11,6 @@ const AppMode = archetype.AppMode; const chalk = require("chalk"); const logger = require("@xarc/app/lib/logger"); const mkdirp = require("mkdirp"); -const requireFromPath = require("require-from-path"); const DEV_HMR_DIR = ".__dev_hmr"; diff --git a/packages/xarc-webpack/package.json b/packages/xarc-webpack/package.json index 1ec3e2415..bab20e4bc 100644 --- a/packages/xarc-webpack/package.json +++ b/packages/xarc-webpack/package.json @@ -42,7 +42,6 @@ "optimize-css-assets-webpack-plugin": "^5.0.1", "optional-require": "^1.0.0", "require-at": "^1.0.2", - "require-from-path": "^1.0.4", "url-loader": "^0.6.2", "webpack": "^4.41.0", "webpack-config-composer": "^1.1.3", From 773dad1601ef50979e5bf1f7dd0e7a9cef158af0 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Mon, 13 Jul 2020 18:09:10 -0700 Subject: [PATCH 05/12] Fixing tests --- samples/react-jest-app/package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/react-jest-app/package.json b/samples/react-jest-app/package.json index 70fd71e4b..a85adc8f4 100644 --- a/samples/react-jest-app/package.json +++ b/samples/react-jest-app/package.json @@ -62,7 +62,7 @@ "@xarc/app-dev": "^5.3.4", "electrode-archetype-opt-critical-css": "^1.0.3", "electrode-archetype-opt-eslint": "^1.0.3", - "electrode-archetype-opt-jest": "^25.0.0", + "electrode-archetype-opt-jest": "^26.0.0", "electrode-archetype-opt-less": "^1.0.2", "electrode-archetype-opt-mocha": "^1.0.3", "electrode-archetype-opt-phantomjs": "^1.0.2", @@ -80,8 +80,7 @@ "electrode-auto-ssr": "../../packages/electrode-auto-ssr" }, "devDependencies": { - "@xarc/app-dev": "../../packages/xarc-app-dev", - "electrode-archetype-opt-jest": "../../packages/electrode-archetype-opt-jest" + "@xarc/app-dev": "../../packages/xarc-app-dev" } }, "optionalDependencies": {} From bc757e6a7bd9709f0ef1694eb1eaaa341a62b702 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Tue, 14 Jul 2020 11:28:21 -0700 Subject: [PATCH 06/12] support module:true option --- packages/subapp-server/lib/setup-hapi-routes.js | 4 ++-- packages/subapp-util/lib/index.js | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index c8b12dce5..54c165993 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -112,9 +112,9 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { x = x[0]; } // absolute: use as path + // is module: resolve module path // else: assume dir under srcDir - // TBD: handle it being a module - if (x.indexOf("/") === -1) { + if (options.module) { x = Path.dirname(require.resolve(`${x}/package.json`)); } return { diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index a7d3bc110..164247bc9 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -34,7 +34,9 @@ const validateModuleManifest = manifest => { const entries = _.pick(manifest, ["entry", "serverEntry", "reducers"]); Object.keys(entries).forEach(x => { if (!Path.isAbsolute(entries[x])) { - throw new Error(`Error: manifest ${x} requires absolute path`); + throw new Error( + `Could not resolve subapp ${x} "${entries[x]}". Please provide absolute path to the ${x} in subapp manifest` + ); } }); }; From adaf55a3eb361fc7a13d2b60516e3482d44dd3a9 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Tue, 14 Jul 2020 11:50:04 -0700 Subject: [PATCH 07/12] fixing eslint error --- packages/subapp-util/lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 164247bc9..02bfa4eb3 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -35,7 +35,8 @@ const validateModuleManifest = manifest => { Object.keys(entries).forEach(x => { if (!Path.isAbsolute(entries[x])) { throw new Error( - `Could not resolve subapp ${x} "${entries[x]}". Please provide absolute path to the ${x} in subapp manifest` + `Could not resolve subapp ${x} "${entries[x]}".\ + Please provide absolute path to the ${x} in subapp manifest` ); } }); From 62b7aa2ca5b8079fa979bd0438b885156766ea34 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Tue, 14 Jul 2020 11:50:04 -0700 Subject: [PATCH 08/12] fixing eslint error --- packages/subapp-util/lib/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 164247bc9..02bfa4eb3 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -35,7 +35,8 @@ const validateModuleManifest = manifest => { Object.keys(entries).forEach(x => { if (!Path.isAbsolute(entries[x])) { throw new Error( - `Could not resolve subapp ${x} "${entries[x]}". Please provide absolute path to the ${x} in subapp manifest` + `Could not resolve subapp ${x} "${entries[x]}".\ + Please provide absolute path to the ${x} in subapp manifest` ); } }); From 77cbd8ec3498b9c4a56a8fdcb5aabb11fdf40555 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Tue, 14 Jul 2020 13:00:43 -0700 Subject: [PATCH 09/12] minor update --- packages/subapp-server/lib/setup-hapi-routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 54c165993..3efd859d0 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -112,7 +112,7 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { x = x[0]; } // absolute: use as path - // is module: resolve module path + // module: resolve module path // else: assume dir under srcDir if (options.module) { x = Path.dirname(require.resolve(`${x}/package.json`)); From 4ee40d9b8988c9aab788a1da45328bf697e86018 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Tue, 14 Jul 2020 16:54:09 -0700 Subject: [PATCH 10/12] remove module:true flag --- packages/subapp-server/lib/setup-hapi-routes.js | 11 ++++++++--- packages/subapp-util/lib/index.js | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 3efd859d0..93341d1a6 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -108,14 +108,19 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { routeOptions.__internals.subApps = [].concat(routeOptions.subApps).map(x => { let options = {}; if (Array.isArray(x)) { - options = x[1] || {}; + options = x[1]; x = x[0]; } // absolute: use as path // module: resolve module path // else: assume dir under srcDir - if (options.module) { - x = Path.dirname(require.resolve(`${x}/package.json`)); + if (!x.startsWith(".") && !x.startsWith("/")) { + const subAppPath = optionalRequire.resolve(x); + if (subAppPath) { + const { manifest, subAppOptions } = require(x); + x = manifest ? Path.dirname(subAppPath) : x; + options = subAppOptions || {}; + } } return { subapp: subAppsByPath[Path.isAbsolute(x) ? x : Path.resolve(srcDir, x)], diff --git a/packages/subapp-util/lib/index.js b/packages/subapp-util/lib/index.js index 02bfa4eb3..dc0b6574e 100644 --- a/packages/subapp-util/lib/index.js +++ b/packages/subapp-util/lib/index.js @@ -184,7 +184,7 @@ function scanSubAppsFromDir(srcDir, maxLevel = Infinity) { try { const manifest = manifests[modName]; validateModuleManifest(manifest); - const modFullDir = Path.dirname(require.resolve(`${modName}/package.json`)); + const modFullDir = Path.dirname(require.resolve(modName)); const subapp = Object.assign({ subAppDir: modName, module: true }, manifest); subApps[manifest.name] = subApps[MAP_BY_PATH_SYM][modFullDir] = subapp; } catch (error) { From 992bce2fec422f2ef1a44dd121085238638c0274 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Wed, 15 Jul 2020 15:40:05 -0700 Subject: [PATCH 11/12] subapp modules: add support to pass subapp options from main app --- packages/subapp-server/lib/setup-hapi-routes.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/subapp-server/lib/setup-hapi-routes.js b/packages/subapp-server/lib/setup-hapi-routes.js index 93341d1a6..299743c93 100644 --- a/packages/subapp-server/lib/setup-hapi-routes.js +++ b/packages/subapp-server/lib/setup-hapi-routes.js @@ -106,7 +106,7 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { // load subapps for the route if (routeOptions.subApps) { routeOptions.__internals.subApps = [].concat(routeOptions.subApps).map(x => { - let options = {}; + let options; if (Array.isArray(x)) { options = x[1]; x = x[0]; @@ -119,12 +119,12 @@ function setupRouteRender({ subAppsByPath, srcDir, routeOptions }) { if (subAppPath) { const { manifest, subAppOptions } = require(x); x = manifest ? Path.dirname(subAppPath) : x; - options = subAppOptions || {}; + options = options || subAppOptions; } } return { subapp: subAppsByPath[Path.isAbsolute(x) ? x : Path.resolve(srcDir, x)], - options + options: options || {} }; }); } From 7cb8c236be36adc8e4da2d99ebc8d3a32b7382f2 Mon Sep 17 00:00:00 2001 From: Divya Karippath Date: Wed, 15 Jul 2020 17:00:34 -0700 Subject: [PATCH 12/12] locking intl-format-cache version to fix the CI tests --- samples/demo-tree-shaking/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/demo-tree-shaking/package.json b/samples/demo-tree-shaking/package.json index 3d65dc2ce..2b9b1ccc3 100644 --- a/samples/demo-tree-shaking/package.json +++ b/samples/demo-tree-shaking/package.json @@ -55,6 +55,7 @@ "lodash": "^4.17.10", "milligram": "^1.3.0", "react-intl": "^3.11.0", + "intl-format-cache": "4.2.46", "react-notify-toast": "^0.4.1", "react-router-config": "^5.1.1" },