Skip to content

Commit

Permalink
Merge pull request #897 from kgkars/CRA_chunk_support
Browse files Browse the repository at this point in the history
fix: allow varied number of CRA chunks (#895)
  • Loading branch information
izaera authored Mar 31, 2022
2 parents b5e6e78 + a0ec325 commit d94298d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 12 additions & 3 deletions projects/js-toolkit/packages/portal-adapt-base/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,20 @@ this may make adaptation fail.
}

function createReactAppTemplateVarsProvider(): object {
const splitFile = fs
const splitFiles = fs
.readdirSync(project.default.dir.join('build', 'static', 'js').asNative)
.filter((fileName) => fileName.endsWith('.chunk.js'))[0];
.filter((fileName) => fileName.endsWith('.chunk.js'));

if (!splitFiles.length) {
return {};
}

const chunkIds = [];
for (let i = 0; i < splitFiles.length; i++) {
chunkIds.push(splitFiles[i].split('.')[0]);
}

return {
splitId: splitFile.split('.')[0],
splitIds: chunkIds,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/

// Require webpack bundles generated by create-react-app build.
var split = require("./static/js/<%= splitId %>.chunk.js");
<% if (typeof splitIds == 'object' && splitIds){ %>
<% for (var i = 0; i < splitIds.length; i++) { %>
var chunk_<%= i %> = require("./static/js/<%= splitIds[i] %>.chunk.js");<% }} %>
var main = require("./static/js/main.js");

// Require our adapt runtime support module
Expand All @@ -19,7 +21,9 @@ var _ADAPT_RT_ = require("./adapt-rt");
// Invoke main module passing Liferay's standard entry point arguments.
module.exports = function(_LIFERAY_PARAMS_) {
// Load React and runtime engine modules.
split();
<% if (typeof splitIds == 'object' && splitIds){ %>
<% for (var i = 0; i < splitIds.length; i++) { %>
chunk_<%= i %>(); <% }} %>

// Call main
main(_LIFERAY_PARAMS_, _ADAPT_RT_);
Expand Down

0 comments on commit d94298d

Please sign in to comment.