Skip to content

Commit

Permalink
handle css bundles for subapps
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Jan 18, 2020
1 parent c24c68a commit 9d85e25
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
59 changes: 40 additions & 19 deletions packages/subapp-web/lib/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<script>/*${name}*/${src}</script>`;
const ext = Path.extname(bundleAsset.name);
if (ext === ".js") {
inlineSubAppJs = `<script>/*${name}*/${src}</script>`;
} else if (ext === ".css") {
inlineSubAppJs = `<style id="${name}">${src}</style>`;
} else {
inlineSubAppJs = `<!-- UNKNOWN bundle extension ${name} -->`;
}
} else {
inlineSubAppJs = true;
}
Expand Down Expand Up @@ -109,7 +116,16 @@ module.exports = function setup(setupContext, token) {
cdnJsBundles[ep] &&
[]
.concat(cdnJsBundles[ep])
.map(jsBundle => `<script src="${jsBundle}" async></script>`)
.map(jsBundle => {
const ext = Path.extname(jsBundle);
if (ext === ".js") {
return `<script src="${jsBundle}" async></script>`;
} else if (ext === ".css") {
return `<link rel="stylesheet" href="${jsBundle}">`;
} else {
return `<!-- UNKNOWN bundle extension ${jsBundle} -->`;
}
})
.join("\n")
);
}
Expand Down Expand Up @@ -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}
<script>markBundlesLoaded(${JSON.stringify(bundles)});</script>
`);
}

if (options.serverSideRendering) {
const lib = util.getFramework(ref);
ssrContent = await lib.handleSSR(ref);
Expand All @@ -168,27 +193,23 @@ module.exports = function setup(setupContext, token) {
ssrContent = `<!-- serverSideRendering flag is ${options.serverSideRendering} -->`;
}

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(`<div id="${options.elementId}">\n`);
outputSpot.add(ssrContent);
outputSpot.add(`\n</div><script>${markBundlesLoadedJs}startSubAppOnLoad({
name: "${name}",
elementId: "${options.elementId}",
serverSideRendering: ${Boolean(options.serverSideRendering)},
clientProps: ${clientProps},
initialState: ${initialStateStr || "{}"}
})</script>\n`);
outputSpot.add(`<div id="${options.elementId}">
${ssrContent}
</div>
<script>
startSubAppOnLoad({
name:"${name}",
elementId:"${options.elementId}",
serverSideRendering:${Boolean(options.serverSideRendering)},
clientProps:${clientProps},
initialState:${initialStateStr || "{}"}
})
</script>
`);
} else {
outputSpot.add("<!-- no elementId for starting subApp on load -->\n");
}
Expand Down
12 changes: 8 additions & 4 deletions packages/subapp-web/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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));
}
});
}
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions samples/poc-subapp-pmin/archetype/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {
subapp: true,
phantomjs: false,
criticalCSS: false,
sass: true,
reactLib: "preact"
}
};
3 changes: 2 additions & 1 deletion samples/poc-subapp-pmin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 2 additions & 0 deletions samples/poc-subapp-pmin/src/home/subapp-home.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { h, loadSubApp } from "subapp-pbundle"; /** @jsx h */

import "tachyons-sass/tachyons.scss";

const Home = () => {
return (
<div>
Expand Down

0 comments on commit 9d85e25

Please sign in to comment.