Skip to content

Commit

Permalink
fix: use loadjs to support CSS bundles (#1647)
Browse files Browse the repository at this point in the history
  • Loading branch information
divyakarippath authored May 19, 2020
1 parent 100c6b1 commit d1186b0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
6 changes: 3 additions & 3 deletions packages/subapp-web/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ module.exports = function setup(setupContext) {
const distDir = process.env.NODE_ENV === "production" ? "../dist/min" : "../dist/dev";
const clientJs = Fs.readFileSync(Path.join(__dirname, distDir, "subapp-web.js")).toString();
const cdnJs = Fs.readFileSync(Path.join(__dirname, distDir, "cdn-map.js")).toString();
const littleLoader = Fs.readFileSync(
require.resolve("little-loader/dist/little-loader.min.js"),
const loadJs = Fs.readFileSync(
require.resolve("loadjs/dist/loadjs.min.js"),
"utf8"
);

Expand All @@ -37,7 +37,7 @@ module.exports = function setup(setupContext) {
const webSubAppJs = `<script id="bundleAssets" type="application/json">
${JSON.stringify(bundleAssets)}
</script>
<script>/*LL*/${littleLoader}/*LL*/
<script>/*LJ*/${loadJs}/*LJ*/
${clientJs}
${cdnJs}
</script>
Expand Down
2 changes: 1 addition & 1 deletion packages/subapp-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@babel/runtime": "^7.8.3",
"history": "^4.9.0",
"jsesc": "^2.5.2",
"little-loader": "^0.2.0",
"loadjs": "^4.2.0",
"lodash": "^4.17.15",
"optional-require": "^1.0.0",
"request": "^2.88.0",
Expand Down
51 changes: 26 additions & 25 deletions packages/subapp-web/src/subapp-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,14 @@
const chunkIds = ba.entryPoints[name];
return chunkIds.reduce((a2, id) => {
// chunk could have multiple assets
return [].concat(ba.jsChunksById[id]).reduce((a3, asset) => {
return asset
? a3.concat({
name,
id,
asset
})
: a3;
}, a2);
const assets = ba.jsChunksById[id];
return assets
? a2.concat({
name,
id,
assets: [].concat(assets)
})
: a2;
}, a);
}, [])
.filter(({ id }) => {
Expand All @@ -337,22 +336,24 @@

return false;
});
assetsToLoad.forEach(({ name, id, asset }) => {
window._lload(`${ba.basePath}${asset}`, {
callback: err => {
if (err) {
console.error(`load asset ${name} (id: ${id}) failed`, err);
} else {
console.log(`loaded asset for ${name} (id: ${id}) - ${asset}`);
runtimeInfo.bundles[id]++;
}

loaded.push(asset);

if (loaded.length === assetsToLoad.length) {
console.log("all assets loaded", assetsToLoad);
done();
}
assetsToLoad.forEach(({ name, id, assets }) => {
const new_assets = assets.map(asset => `${ba.basePath}${asset}`);
const afterLoad = () => {
loaded.push(assets);
if (loaded.length === assetsToLoad.length) {
console.log("all assets loaded", assetsToLoad);
done();
}
};
loadjs(new_assets, id, {
success: () => {
console.log(`loaded asset for ${name} (id: ${id}) - ${assets}`);
runtimeInfo.bundles[id]++;
afterLoad();
},
error: () => {
console.error(`load asset ${name} (id: ${id}) failed`, err);
afterLoad();
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/subapp-web/test/spec/init.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("init", function() {
delete process.env.APP_SRC_DIR;
});

it("should return assets as JSON script and little loader", () => {
it("should return assets as JSON script and loadJs", () => {
// point subapp-util to look for subapps under a test dir
process.env.APP_SRC_DIR = "test/subapps";

Expand All @@ -27,6 +27,6 @@ describe("init", function() {
const initJs = initToken.process(context);
expect(context.user.assets).to.be.ok;
expect(initJs).contains(`<script id="bundleAssets" type="application/json">`);
expect(initJs).contains(`<script>/*LL*/`);
expect(initJs).contains(`<script>/*LJ*/`);
});
});

0 comments on commit d1186b0

Please sign in to comment.