From 9d85e259fd7908f2190682a77f37afdf917747c1 Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Sat, 18 Jan 2020 10:03:59 -0800 Subject: [PATCH] handle css bundles for subapps --- packages/subapp-web/lib/load.js | 59 +++++++++++++------ packages/subapp-web/lib/util.js | 12 ++-- .../poc-subapp-pmin/archetype/config/index.js | 1 + samples/poc-subapp-pmin/package.json | 3 +- .../poc-subapp-pmin/src/home/subapp-home.js | 2 + 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/packages/subapp-web/lib/load.js b/packages/subapp-web/lib/load.js index ee1d8445b..c3ef2c188 100644 --- a/packages/subapp-web/lib/load.js +++ b/packages/subapp-web/lib/load.js @@ -75,7 +75,14 @@ module.exports = function setup(setupContext, token) { if (!webpackDev) { // if we have to inline the subapp's JS bundle, we load it for production mode const src = Fs.readFileSync(Path.resolve("dist/js", bundleAsset.name)).toString(); - inlineSubAppJs = ``; + const ext = Path.extname(bundleAsset.name); + if (ext === ".js") { + inlineSubAppJs = ``; + } else if (ext === ".css") { + inlineSubAppJs = ``; + } else { + inlineSubAppJs = ``; + } } else { inlineSubAppJs = true; } @@ -109,7 +116,16 @@ module.exports = function setup(setupContext, token) { cdnJsBundles[ep] && [] .concat(cdnJsBundles[ep]) - .map(jsBundle => ``) + .map(jsBundle => { + const ext = Path.extname(jsBundle); + if (ext === ".js") { + return ``; + } else if (ext === ".css") { + return ``; + } else { + return ``; + } + }) .join("\n") ); } @@ -160,6 +176,15 @@ module.exports = function setup(setupContext, token) { subAppServer, options }; + + const { bundles, scripts } = await prepareSubAppSplitBundles(context); + outputSpot.add(`${comment}`); + if (bundles.length > 0) { + outputSpot.add(`${scripts} + +`); + } + if (options.serverSideRendering) { const lib = util.getFramework(ref); ssrContent = await lib.handleSSR(ref); @@ -168,27 +193,23 @@ module.exports = function setup(setupContext, token) { ssrContent = ``; } - let markBundlesLoadedJs = ""; - const { bundles, scripts } = await prepareSubAppSplitBundles(context); - outputSpot.add(`${comment}`); - if (bundles.length > 0) { - outputSpot.add(`${scripts}\n`); - markBundlesLoadedJs = `markBundlesLoaded(${JSON.stringify(bundles)});\n`; - } - // If user specified an element ID for a DOM Node to host the SSR content then // add the div for the Node and the SSR content to it, and add JS to start the // sub app on load. if (options.elementId) { - outputSpot.add(`
\n`); - outputSpot.add(ssrContent); - outputSpot.add(`\n
\n`); + outputSpot.add(`
+ ${ssrContent} +
+ +`); } else { outputSpot.add("\n"); } diff --git a/packages/subapp-web/lib/util.js b/packages/subapp-web/lib/util.js index 0b82a636b..5db1d2a49 100644 --- a/packages/subapp-web/lib/util.js +++ b/packages/subapp-web/lib/util.js @@ -114,6 +114,8 @@ const utils = { for (const id in bundlesById) { const bundles = bundlesById[id]; [].concat(bundles).forEach(bundleFile => { + if (!bundleFile) return; + for (const mapName in CDN_ASSETS) { if (mapName.endsWith(bundleFile)) { cdnBundles[id] = CDN_ASSETS[mapName]; @@ -124,7 +126,7 @@ const utils = { if (!cdnBundles[id]) { cdnBundles[id] = basePath.concat(bundleFile); } else { - cdnBundles[id] = [].concat(cdnBundles[id], bundleFile); + cdnBundles[id] = [].concat(cdnBundles[id], basePath.concat(bundleFile)); } }); } @@ -141,11 +143,13 @@ const utils = { // pack up entrypoints data from stats // - const { chunksById } = assets; + const { + chunksById: { js, css } + } = assets; const bundleBase = utils.getBundleBase(routeOptions); - - return (CDN_JS_BUNDLES = utils.mapCdnAssets(chunksById.js, bundleBase, cdnAssetsFile)); + const allChunks = _.mergeWith({}, js, css, (a, b) => (a && b ? [].concat(a, b) : a || b)); + return (CDN_JS_BUNDLES = utils.mapCdnAssets(allChunks, bundleBase, cdnAssetsFile)); }, getChunksById(stats) { diff --git a/samples/poc-subapp-pmin/archetype/config/index.js b/samples/poc-subapp-pmin/archetype/config/index.js index eb8924ea0..0a19e8a54 100644 --- a/samples/poc-subapp-pmin/archetype/config/index.js +++ b/samples/poc-subapp-pmin/archetype/config/index.js @@ -3,6 +3,7 @@ module.exports = { subapp: true, phantomjs: false, criticalCSS: false, + sass: true, reactLib: "preact" } }; diff --git a/samples/poc-subapp-pmin/package.json b/samples/poc-subapp-pmin/package.json index c4571a5c2..c8bb59249 100644 --- a/samples/poc-subapp-pmin/package.json +++ b/samples/poc-subapp-pmin/package.json @@ -47,7 +47,8 @@ "subapp-server": "../../packages/subapp-server" }, "devDependencies": { - "electrode-archetype-react-app-dev": "^6.3.0" + "electrode-archetype-react-app-dev": "^6.3.0", + "tachyons-sass": "^4.9.5" }, "fyn": { "dependencies": { diff --git a/samples/poc-subapp-pmin/src/home/subapp-home.js b/samples/poc-subapp-pmin/src/home/subapp-home.js index 18a050d36..0093a5c4e 100644 --- a/samples/poc-subapp-pmin/src/home/subapp-home.js +++ b/samples/poc-subapp-pmin/src/home/subapp-home.js @@ -1,5 +1,7 @@ import { h, loadSubApp } from "subapp-pbundle"; /** @jsx h */ +import "tachyons-sass/tachyons.scss"; + const Home = () => { return (