Skip to content

Commit df526e7

Browse files
authored
ability to merge user defined context with implicitely generated one
1 parent 4b722ff commit df526e7

File tree

2 files changed

+132
-108
lines changed

2 files changed

+132
-108
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lastui/rocker",
3-
"version": "0.19.16",
3+
"version": "0.19.17",
44
"license": "Apache-2.0",
55
"author": "[email protected]",
66
"homepage": "https://github.com/lastui/rocker#readme",

webpack/config/module/development.js

+131-107
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ config.plugins.push(
239239
inject: false,
240240
scriptLoading: "defer",
241241
templateContent: (props) => {
242-
let entrypoints = [];
242+
const entrypoints = [];
243243

244244
for (const entryPoint of props.compilation.entrypoints.values()) {
245245
for (const chunk of entryPoint.chunks) {
@@ -259,120 +259,125 @@ config.plugins.push(
259259
return item;
260260
});
261261

262-
let manifest;
262+
let customManifest;
263+
263264
try {
264-
manifest = fs.readFileSync(path.resolve(process.env.INIT_CWD, "manifest.json"), "utf8");
265+
customManifest = JSON.parse(fs.readFileSync(path.resolve(process.env.INIT_CWD, "manifest.json"), "utf8"));
265266
} catch (_) {
266-
let entrypoint = null;
267-
268-
if (entrypoints.length === 1) {
269-
entrypoint = entrypoints[0].id;
270-
} else {
271-
const dependencyGraph = {};
272-
273-
for (const chunk of entrypoints) {
274-
props.compilation.chunkGraph.getChunkModulesIterable(chunk).forEach((fragment) => {
275-
const matchedImports = fragment.dependencies.filter(
276-
(item) => item.request === "@lastui/rocker/platform" && item.name === "Module",
277-
);
278-
279-
if (matchedImports.length > 0) {
280-
fragment._source._sourceMapAsObject.sourcesContent.forEach((sourceCode) => {
281-
const ast = parser.parse(sourceCode, {
282-
sourceType: "module",
283-
plugins: ["jsx"],
284-
});
285-
286-
traverse(ast, {
287-
CallExpression: (path) => {
288-
if (path.node.callee.type !== "MemberExpression") {
289-
return;
290-
}
291-
if (path.node.callee.object.name !== "React") {
292-
return;
293-
}
294-
if (path.node.callee.property.name !== "createElement") {
295-
return;
296-
}
297-
if (path.node.arguments.length < 2) {
298-
return;
299-
}
300-
if (path.node.arguments[0].type !== "Identifier" && path.node.arguments[0].name !== "Module") {
301-
return;
302-
}
303-
if (path.node.arguments[1].type !== "ObjectExpression") {
304-
return;
305-
}
267+
customManifest = {};
268+
}
269+
270+
const implicitContext = {};
271+
272+
if (entrypoints.length === 1) {
273+
implicitContext.entrypoint = entrypoints[0].id;
274+
} else {
275+
const dependencyGraph = {};
276+
277+
for (const chunk of entrypoints) {
278+
props.compilation.chunkGraph.getChunkModulesIterable(chunk).forEach((fragment) => {
279+
const matchedImports = fragment.dependencies.filter(
280+
(item) => item.request === "@lastui/rocker/platform" && item.name === "Module",
281+
);
306282

307-
(path.node.arguments[1].properties ?? []).forEach((attribute) => {
308-
if (attribute.key.type === "Identifier" && attribute.key.name === "name") {
309-
if (!dependencyGraph[chunk.id]) {
310-
dependencyGraph[chunk.id] = [];
311-
}
312-
if (!dependencyGraph[attribute.value.value]) {
313-
dependencyGraph[attribute.value.value] = [];
314-
}
315-
if (!dependencyGraph[attribute.value.value].includes(chunk.id)) {
316-
dependencyGraph[attribute.value.value].push(chunk.id);
317-
}
283+
if (matchedImports.length > 0) {
284+
fragment._source._sourceMapAsObject.sourcesContent.forEach((sourceCode) => {
285+
const ast = parser.parse(sourceCode, {
286+
sourceType: "module",
287+
plugins: ["jsx"],
288+
});
289+
290+
traverse(ast, {
291+
CallExpression: (path) => {
292+
if (path.node.callee.type !== "MemberExpression") {
293+
return;
294+
}
295+
if (path.node.callee.object.name !== "React") {
296+
return;
297+
}
298+
if (path.node.callee.property.name !== "createElement") {
299+
return;
300+
}
301+
if (path.node.arguments.length < 2) {
302+
return;
303+
}
304+
if (path.node.arguments[0].type !== "Identifier" && path.node.arguments[0].name !== "Module") {
305+
return;
306+
}
307+
if (path.node.arguments[1].type !== "ObjectExpression") {
308+
return;
309+
}
310+
311+
(path.node.arguments[1].properties ?? []).forEach((attribute) => {
312+
if (attribute.key.type === "Identifier" && attribute.key.name === "name") {
313+
if (!dependencyGraph[chunk.id]) {
314+
dependencyGraph[chunk.id] = [];
315+
}
316+
if (!dependencyGraph[attribute.value.value]) {
317+
dependencyGraph[attribute.value.value] = [];
318+
}
319+
if (!dependencyGraph[attribute.value.value].includes(chunk.id)) {
320+
dependencyGraph[attribute.value.value].push(chunk.id);
318321
}
319-
});
320-
},
321-
JSXElement: (path) => {
322-
if (path.node.openingElement.name.name !== "Module") {
323-
return;
324322
}
325-
(path.node.openingElement.attributes ?? []).forEach((attribute) => {
326-
if (attribute.name.type === "JSXIdentifier" && attribute.name.name === "name") {
327-
if (!dependencyGraph[chunk.id]) {
328-
dependencyGraph[chunk.id] = [];
329-
}
330-
if (!dependencyGraph[attribute.value.value]) {
331-
dependencyGraph[attribute.value.value] = [];
332-
}
333-
if (!dependencyGraph[attribute.value.value].includes(chunk.id)) {
334-
dependencyGraph[attribute.value.value].push(chunk.id);
335-
}
323+
});
324+
},
325+
JSXElement: (path) => {
326+
if (path.node.openingElement.name.name !== "Module") {
327+
return;
328+
}
329+
(path.node.openingElement.attributes ?? []).forEach((attribute) => {
330+
if (attribute.name.type === "JSXIdentifier" && attribute.name.name === "name") {
331+
if (!dependencyGraph[chunk.id]) {
332+
dependencyGraph[chunk.id] = [];
336333
}
337-
});
338-
},
339-
});
334+
if (!dependencyGraph[attribute.value.value]) {
335+
dependencyGraph[attribute.value.value] = [];
336+
}
337+
if (!dependencyGraph[attribute.value.value].includes(chunk.id)) {
338+
dependencyGraph[attribute.value.value].push(chunk.id);
339+
}
340+
}
341+
});
342+
},
340343
});
341-
}
342-
});
343-
}
344+
});
345+
}
346+
});
347+
}
344348

345-
const executionOrder = [];
346-
const visited = {};
347-
const completed = {};
348-
const trail = {};
349+
const executionOrder = [];
350+
const visited = {};
351+
const completed = {};
352+
const trail = {};
349353

350-
function walk(node) {
351-
if (completed[node]) {
352-
return;
353-
}
354-
if (trail[node]) {
355-
return executionOrder.unshift(node);
356-
}
357-
visited[node] = true;
358-
trail[node] = true;
359-
dependencyGraph[node].forEach(walk);
360-
delete trail[node];
361-
completed[node] = true;
362-
executionOrder.unshift(node);
354+
function walk(node) {
355+
if (completed[node]) {
356+
return;
363357
}
364-
365-
for (const node of Object.keys(dependencyGraph)) {
366-
if (visited[node]) {
367-
continue;
368-
}
369-
walk(node);
358+
if (trail[node]) {
359+
return executionOrder.unshift(node);
370360
}
361+
visited[node] = true;
362+
trail[node] = true;
363+
dependencyGraph[node].forEach(walk);
364+
delete trail[node];
365+
completed[node] = true;
366+
executionOrder.unshift(node);
367+
}
371368

372-
entrypoint = executionOrder[executionOrder.length - 1];
369+
for (const node of Object.keys(dependencyGraph)) {
370+
if (visited[node]) {
371+
continue;
372+
}
373+
walk(node);
373374
}
374375

375-
const available = entrypoints.map((chunk) => {
376+
implicitContext.entrypoint = executionOrder[executionOrder.length - 1];
377+
}
378+
379+
if (entrypoints.length > 0) {
380+
implicitContext.available = entrypoints.map((chunk) => {
376381
const entry = {
377382
name: chunk.id,
378383
program: {
@@ -391,11 +396,30 @@ config.plugins.push(
391396
}
392397
return entry;
393398
});
399+
}
394400

395-
manifest = JSON.stringify({
396-
entrypoint,
397-
available,
398-
});
401+
const mergedManifest = {};
402+
403+
if (customManifest.environment) {
404+
mergedManifest.environment = customManifest.environment;
405+
}
406+
407+
if (customManifest.entrypoint) {
408+
mergedManifest.entrypoint = customManifest.entrypoint;
409+
} else if (implicitContext.entrypoint) {
410+
mergedManifest.entrypoint = implicitContext.entrypoint;
411+
}
412+
413+
if (customManifest.available && implicitContext.available) {
414+
mergedManifest.available = customManifest.available;
415+
for (const entry of implicitContext.available) {
416+
mergedManifest.available = mergedManifest.available.filter((existing) => existing.name === entry.name);
417+
mergedManifest.available.push(entry);
418+
}
419+
} else if (customManifest.available) {
420+
mergedManifest.available = customManifest.available;
421+
} else {
422+
mergedManifest.available = implicitContext.available;
399423
}
400424

401425
return `
@@ -408,7 +432,7 @@ config.plugins.push(
408432
(function() {
409433
"use strict";
410434
411-
const manifest = ${manifest.trim()};
435+
const manifest = ${JSON.stringify(mergedManifest).trim()};
412436
413437
window.addEventListener("DOMContentLoaded", function() {
414438
const react = rocker_so_dependencies("./node_modules/react/index.js");
@@ -425,7 +449,7 @@ config.plugins.push(
425449
})
426450
}())
427451
</script>
428-
<div id="${settings.PROJECT_NAME}" style="width: 100%; height: 100%;" />
452+
<div id="${settings.PROJECT_NAME}" style="width: 100%; min-height: 100%; display: grid;" />
429453
</body>
430454
</html>
431455
`;

0 commit comments

Comments
 (0)