elegance-js 1.11.31 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/build.mjs +17 -6
- package/dist/compile_docs.mjs +17 -6
- package/dist/dynamic_page.mjs +17 -6
- package/dist/page_compiler.mjs +85 -7
- package/dist/server/generateHTMLTemplate.d.ts +2 -1
- package/dist/server/generateHTMLTemplate.mjs +6 -2
- package/dist/server/packModule.d.ts +4 -0
- package/dist/server/packModule.mjs +30 -0
- package/dist/server/server.mjs +17 -6
- package/package.json +1 -1
package/dist/build.mjs
CHANGED
|
@@ -260,14 +260,18 @@ var generateHTMLTemplate = ({
|
|
|
260
260
|
head: head2,
|
|
261
261
|
serverData = null,
|
|
262
262
|
addPageScriptTag = true,
|
|
263
|
-
name
|
|
263
|
+
name,
|
|
264
|
+
requiredClientModules = []
|
|
264
265
|
}) => {
|
|
265
266
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
266
267
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
268
|
+
for (const module of requiredClientModules) {
|
|
269
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
270
|
+
}
|
|
267
271
|
if (addPageScriptTag === true) {
|
|
268
272
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
269
273
|
}
|
|
270
|
-
HTMLTemplate += `<script
|
|
274
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
271
275
|
const builtHead = head2();
|
|
272
276
|
for (const child2 of builtHead.children) {
|
|
273
277
|
HTMLTemplate += renderRecursively(child2);
|
|
@@ -417,7 +421,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
417
421
|
}
|
|
418
422
|
return element;
|
|
419
423
|
};
|
|
420
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
424
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
421
425
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
422
426
|
return [];
|
|
423
427
|
}
|
|
@@ -432,7 +436,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
432
436
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
433
437
|
head: metadata,
|
|
434
438
|
addPageScriptTag: true,
|
|
435
|
-
name: pageName
|
|
439
|
+
name: pageName,
|
|
440
|
+
requiredClientModules
|
|
436
441
|
});
|
|
437
442
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
438
443
|
return {
|
|
@@ -543,6 +548,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
543
548
|
resetLoadHooks();
|
|
544
549
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
545
550
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
551
|
+
let modules = [];
|
|
546
552
|
try {
|
|
547
553
|
const {
|
|
548
554
|
construct
|
|
@@ -551,8 +557,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
551
557
|
page,
|
|
552
558
|
metadata: pageMetadata,
|
|
553
559
|
isDynamicPage,
|
|
554
|
-
requestHook
|
|
560
|
+
requestHook,
|
|
561
|
+
requiredClientModules
|
|
555
562
|
} = construct();
|
|
563
|
+
if (requiredClientModules !== void 0) {
|
|
564
|
+
modules = requiredClientModules;
|
|
565
|
+
}
|
|
556
566
|
if (typeof requestHook === "function") {
|
|
557
567
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
558
568
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -598,7 +608,8 @@ ${e?.stack ?? "No stack."}
|
|
|
598
608
|
pageElements || body(),
|
|
599
609
|
metadata ?? (() => head()),
|
|
600
610
|
DIST_DIR,
|
|
601
|
-
"page"
|
|
611
|
+
"page",
|
|
612
|
+
modules
|
|
602
613
|
);
|
|
603
614
|
await generateClientPageData(
|
|
604
615
|
path.dirname(filePath),
|
package/dist/compile_docs.mjs
CHANGED
|
@@ -263,14 +263,18 @@ var generateHTMLTemplate = ({
|
|
|
263
263
|
head: head2,
|
|
264
264
|
serverData = null,
|
|
265
265
|
addPageScriptTag = true,
|
|
266
|
-
name
|
|
266
|
+
name,
|
|
267
|
+
requiredClientModules = []
|
|
267
268
|
}) => {
|
|
268
269
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
269
270
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
271
|
+
for (const module of requiredClientModules) {
|
|
272
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
273
|
+
}
|
|
270
274
|
if (addPageScriptTag === true) {
|
|
271
275
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
272
276
|
}
|
|
273
|
-
HTMLTemplate += `<script
|
|
277
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
274
278
|
const builtHead = head2();
|
|
275
279
|
for (const child2 of builtHead.children) {
|
|
276
280
|
HTMLTemplate += renderRecursively(child2);
|
|
@@ -420,7 +424,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
420
424
|
}
|
|
421
425
|
return element;
|
|
422
426
|
};
|
|
423
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
427
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
424
428
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
425
429
|
return [];
|
|
426
430
|
}
|
|
@@ -435,7 +439,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
435
439
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
436
440
|
head: metadata,
|
|
437
441
|
addPageScriptTag: true,
|
|
438
|
-
name: pageName
|
|
442
|
+
name: pageName,
|
|
443
|
+
requiredClientModules
|
|
439
444
|
});
|
|
440
445
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
441
446
|
return {
|
|
@@ -546,6 +551,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
546
551
|
resetLoadHooks();
|
|
547
552
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
548
553
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
554
|
+
let modules = [];
|
|
549
555
|
try {
|
|
550
556
|
const {
|
|
551
557
|
construct
|
|
@@ -554,8 +560,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
554
560
|
page,
|
|
555
561
|
metadata: pageMetadata,
|
|
556
562
|
isDynamicPage,
|
|
557
|
-
requestHook
|
|
563
|
+
requestHook,
|
|
564
|
+
requiredClientModules
|
|
558
565
|
} = construct();
|
|
566
|
+
if (requiredClientModules !== void 0) {
|
|
567
|
+
modules = requiredClientModules;
|
|
568
|
+
}
|
|
559
569
|
if (typeof requestHook === "function") {
|
|
560
570
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
561
571
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -601,7 +611,8 @@ ${e?.stack ?? "No stack."}
|
|
|
601
611
|
pageElements || body(),
|
|
602
612
|
metadata ?? (() => head()),
|
|
603
613
|
DIST_DIR,
|
|
604
|
-
"page"
|
|
614
|
+
"page",
|
|
615
|
+
modules
|
|
605
616
|
);
|
|
606
617
|
await generateClientPageData(
|
|
607
618
|
path.dirname(filePath),
|
package/dist/dynamic_page.mjs
CHANGED
|
@@ -217,14 +217,18 @@ var generateHTMLTemplate = ({
|
|
|
217
217
|
head: head2,
|
|
218
218
|
serverData = null,
|
|
219
219
|
addPageScriptTag = true,
|
|
220
|
-
name
|
|
220
|
+
name,
|
|
221
|
+
requiredClientModules = []
|
|
221
222
|
}) => {
|
|
222
223
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
223
224
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
225
|
+
for (const module of requiredClientModules) {
|
|
226
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
227
|
+
}
|
|
224
228
|
if (addPageScriptTag === true) {
|
|
225
229
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
226
230
|
}
|
|
227
|
-
HTMLTemplate += `<script
|
|
231
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
228
232
|
const builtHead = head2();
|
|
229
233
|
for (const child of builtHead.children) {
|
|
230
234
|
HTMLTemplate += renderRecursively(child);
|
|
@@ -374,7 +378,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
374
378
|
}
|
|
375
379
|
return element;
|
|
376
380
|
};
|
|
377
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
381
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
378
382
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
379
383
|
return [];
|
|
380
384
|
}
|
|
@@ -389,7 +393,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
389
393
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
390
394
|
head: metadata,
|
|
391
395
|
addPageScriptTag: true,
|
|
392
|
-
name: pageName
|
|
396
|
+
name: pageName,
|
|
397
|
+
requiredClientModules
|
|
393
398
|
});
|
|
394
399
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
395
400
|
return {
|
|
@@ -500,6 +505,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
500
505
|
resetLoadHooks();
|
|
501
506
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
502
507
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
508
|
+
let modules = [];
|
|
503
509
|
try {
|
|
504
510
|
const {
|
|
505
511
|
construct
|
|
@@ -508,8 +514,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
508
514
|
page,
|
|
509
515
|
metadata: pageMetadata,
|
|
510
516
|
isDynamicPage,
|
|
511
|
-
requestHook
|
|
517
|
+
requestHook,
|
|
518
|
+
requiredClientModules
|
|
512
519
|
} = construct();
|
|
520
|
+
if (requiredClientModules !== void 0) {
|
|
521
|
+
modules = requiredClientModules;
|
|
522
|
+
}
|
|
513
523
|
if (typeof requestHook === "function") {
|
|
514
524
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
515
525
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -555,7 +565,8 @@ ${e?.stack ?? "No stack."}
|
|
|
555
565
|
pageElements || body(),
|
|
556
566
|
metadata ?? (() => head()),
|
|
557
567
|
DIST_DIR,
|
|
558
|
-
"page"
|
|
568
|
+
"page",
|
|
569
|
+
modules
|
|
559
570
|
);
|
|
560
571
|
await generateClientPageData(
|
|
561
572
|
path.dirname(filePath),
|
package/dist/page_compiler.mjs
CHANGED
|
@@ -217,14 +217,18 @@ var generateHTMLTemplate = ({
|
|
|
217
217
|
head: head2,
|
|
218
218
|
serverData = null,
|
|
219
219
|
addPageScriptTag = true,
|
|
220
|
-
name
|
|
220
|
+
name,
|
|
221
|
+
requiredClientModules = []
|
|
221
222
|
}) => {
|
|
222
223
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
223
224
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
225
|
+
for (const module of requiredClientModules) {
|
|
226
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
227
|
+
}
|
|
224
228
|
if (addPageScriptTag === true) {
|
|
225
229
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
226
230
|
}
|
|
227
|
-
HTMLTemplate += `<script
|
|
231
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
228
232
|
const builtHead = head2();
|
|
229
233
|
for (const child of builtHead.children) {
|
|
230
234
|
HTMLTemplate += renderRecursively(child);
|
|
@@ -514,7 +518,7 @@ ${trace}`);
|
|
|
514
518
|
}
|
|
515
519
|
}
|
|
516
520
|
};
|
|
517
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR2, pageName, doWrite = true) => {
|
|
521
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR2, pageName, doWrite = true, requiredClientModules = []) => {
|
|
518
522
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
519
523
|
return [];
|
|
520
524
|
}
|
|
@@ -530,7 +534,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
530
534
|
pageURL: path.relative(DIST_DIR2, pageLocation),
|
|
531
535
|
head: metadata,
|
|
532
536
|
addPageScriptTag: true,
|
|
533
|
-
name: pageName
|
|
537
|
+
name: pageName,
|
|
538
|
+
requiredClientModules
|
|
534
539
|
});
|
|
535
540
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
536
541
|
const htmlLocation = path.join(pageLocation, (pageName === "page" ? "index" : pageName) + ".html");
|
|
@@ -680,12 +685,17 @@ var buildPage = async (DIST_DIR2, directory, filePath, name) => {
|
|
|
680
685
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
681
686
|
let pageElements;
|
|
682
687
|
let metadata;
|
|
688
|
+
let modules = [];
|
|
683
689
|
try {
|
|
684
690
|
const {
|
|
685
691
|
page,
|
|
686
692
|
metadata: pageMetadata,
|
|
687
|
-
isDynamicPage
|
|
693
|
+
isDynamicPage,
|
|
694
|
+
requiredClientModules
|
|
688
695
|
} = await import("file://" + filePath);
|
|
696
|
+
if (requiredClientModules !== void 0) {
|
|
697
|
+
modules = requiredClientModules;
|
|
698
|
+
}
|
|
689
699
|
pageElements = page;
|
|
690
700
|
metadata = pageMetadata;
|
|
691
701
|
if (isDynamicPage === true) {
|
|
@@ -734,7 +744,9 @@ return __exports
|
|
|
734
744
|
pageElements || body(),
|
|
735
745
|
metadata ?? (() => head()),
|
|
736
746
|
DIST_DIR2,
|
|
737
|
-
name
|
|
747
|
+
name,
|
|
748
|
+
true,
|
|
749
|
+
modules
|
|
738
750
|
);
|
|
739
751
|
const {
|
|
740
752
|
sendHardReloadInstruction
|
|
@@ -780,6 +792,56 @@ var externalPackagesPlugin = {
|
|
|
780
792
|
});
|
|
781
793
|
}
|
|
782
794
|
};
|
|
795
|
+
var shippedPlugins = /* @__PURE__ */ new Map();
|
|
796
|
+
var pluginsToShip = [];
|
|
797
|
+
var shipPlugin = {
|
|
798
|
+
name: "ship",
|
|
799
|
+
setup(build2) {
|
|
800
|
+
build2.onLoad({ filter: /\.(js|ts|jsx|tsx)$/ }, async (args) => {
|
|
801
|
+
const contents = await fs.promises.readFile(args.path, "utf8");
|
|
802
|
+
const lines = contents.split(/\r?\n/);
|
|
803
|
+
let prepender = "";
|
|
804
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
805
|
+
if (lines[i].trim() === "//@ship") {
|
|
806
|
+
const nextLine = lines[i + 1].trim();
|
|
807
|
+
const starRegex = /import\s*\*\s*as\s*(\w+)\s*from\s*["']([^"']+)["']\s*;/;
|
|
808
|
+
const defaultRegex = /import\s*(\w+)\s*from\s*["']([^"']+)["']\s*;/;
|
|
809
|
+
let match = nextLine.match(starRegex);
|
|
810
|
+
let importName;
|
|
811
|
+
let pkgPath;
|
|
812
|
+
if (match) {
|
|
813
|
+
importName = match[1];
|
|
814
|
+
pkgPath = match[2];
|
|
815
|
+
} else {
|
|
816
|
+
match = nextLine.match(defaultRegex);
|
|
817
|
+
if (match) {
|
|
818
|
+
importName = match[1];
|
|
819
|
+
pkgPath = match[2];
|
|
820
|
+
} else {
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
if (prepender === "") {
|
|
825
|
+
prepender = "export const requiredClientModules = [\n";
|
|
826
|
+
}
|
|
827
|
+
prepender += `"${importName}",
|
|
828
|
+
`;
|
|
829
|
+
pluginsToShip.push({
|
|
830
|
+
path: pkgPath,
|
|
831
|
+
globalName: importName
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (prepender !== "") {
|
|
836
|
+
prepender += "];";
|
|
837
|
+
}
|
|
838
|
+
return {
|
|
839
|
+
contents: prepender + contents,
|
|
840
|
+
loader: path.extname(args.path).slice(1)
|
|
841
|
+
};
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
};
|
|
783
845
|
var build = async () => {
|
|
784
846
|
if (options.quiet === true) {
|
|
785
847
|
console.log = function() {
|
|
@@ -815,12 +877,13 @@ var build = async () => {
|
|
|
815
877
|
const projectFiles = getProjectFiles(options.pagesDirectory);
|
|
816
878
|
const start = performance.now();
|
|
817
879
|
{
|
|
880
|
+
pluginsToShip = [];
|
|
818
881
|
await esbuild.build({
|
|
819
882
|
entryPoints: projectFiles.map((f) => path.join(f.parentPath, f.name)),
|
|
820
883
|
bundle: true,
|
|
821
884
|
outdir: DIST_DIR,
|
|
822
885
|
outExtension: { ".js": ".mjs" },
|
|
823
|
-
plugins: [externalPackagesPlugin],
|
|
886
|
+
plugins: [externalPackagesPlugin, shipPlugin],
|
|
824
887
|
loader: {
|
|
825
888
|
".ts": "ts"
|
|
826
889
|
},
|
|
@@ -832,6 +895,21 @@ var build = async () => {
|
|
|
832
895
|
"PROD": options.environment === "development" ? "false" : "true"
|
|
833
896
|
}
|
|
834
897
|
});
|
|
898
|
+
for (const plugin of pluginsToShip) {
|
|
899
|
+
{
|
|
900
|
+
if (shippedPlugins.has(plugin.globalName)) continue;
|
|
901
|
+
shippedPlugins.set(plugin.globalName, true);
|
|
902
|
+
}
|
|
903
|
+
await esbuild.build({
|
|
904
|
+
entryPoints: [plugin.path],
|
|
905
|
+
bundle: true,
|
|
906
|
+
outfile: path.join(DIST_DIR, "shipped", plugin.globalName + ".js"),
|
|
907
|
+
format: "iife",
|
|
908
|
+
platform: "browser",
|
|
909
|
+
globalName: plugin.globalName
|
|
910
|
+
});
|
|
911
|
+
log("Built a client module.");
|
|
912
|
+
}
|
|
835
913
|
}
|
|
836
914
|
const pagesTranspiled = performance.now();
|
|
837
915
|
const {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export declare const generateHTMLTemplate: ({ pageURL, head, serverData, addPageScriptTag, name, }: {
|
|
1
|
+
export declare const generateHTMLTemplate: ({ pageURL, head, serverData, addPageScriptTag, name, requiredClientModules, }: {
|
|
2
2
|
addPageScriptTag: boolean;
|
|
3
3
|
pageURL: string;
|
|
4
4
|
head: () => BuiltElement<"head">;
|
|
5
5
|
serverData?: string | null;
|
|
6
6
|
name: string;
|
|
7
|
+
requiredClientModules: string[];
|
|
7
8
|
}) => string;
|
|
@@ -199,14 +199,18 @@ var generateHTMLTemplate = ({
|
|
|
199
199
|
head,
|
|
200
200
|
serverData = null,
|
|
201
201
|
addPageScriptTag = true,
|
|
202
|
-
name
|
|
202
|
+
name,
|
|
203
|
+
requiredClientModules = []
|
|
203
204
|
}) => {
|
|
204
205
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
205
206
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
207
|
+
for (const module of requiredClientModules) {
|
|
208
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
209
|
+
}
|
|
206
210
|
if (addPageScriptTag === true) {
|
|
207
211
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
208
212
|
}
|
|
209
|
-
HTMLTemplate += `<script
|
|
213
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
210
214
|
const builtHead = head();
|
|
211
215
|
for (const child of builtHead.children) {
|
|
212
216
|
HTMLTemplate += renderRecursively(child);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// src/server/packModule.ts
|
|
2
|
+
import esbuild from "esbuild";
|
|
3
|
+
function packModule({
|
|
4
|
+
path,
|
|
5
|
+
globalName
|
|
6
|
+
}) {
|
|
7
|
+
try {
|
|
8
|
+
const result = esbuild.buildSync({
|
|
9
|
+
entryPoints: [path],
|
|
10
|
+
write: false,
|
|
11
|
+
platform: "browser",
|
|
12
|
+
format: "iife",
|
|
13
|
+
bundle: true,
|
|
14
|
+
globalName,
|
|
15
|
+
keepNames: true
|
|
16
|
+
});
|
|
17
|
+
globalThis.__SERVER_PAGE_DATA_BANNER__ += result.outputFiles[0].text;
|
|
18
|
+
const errorProxy = new Proxy({}, {
|
|
19
|
+
get() {
|
|
20
|
+
throw new Error("You cannot use client modules in the server.");
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
return errorProxy;
|
|
24
|
+
} catch (e) {
|
|
25
|
+
throw `Failed to pack a module. Module: ${path}. Error: ${e}`;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
packModule
|
|
30
|
+
};
|
package/dist/server/server.mjs
CHANGED
|
@@ -217,14 +217,18 @@ var generateHTMLTemplate = ({
|
|
|
217
217
|
head: head2,
|
|
218
218
|
serverData = null,
|
|
219
219
|
addPageScriptTag = true,
|
|
220
|
-
name
|
|
220
|
+
name,
|
|
221
|
+
requiredClientModules = []
|
|
221
222
|
}) => {
|
|
222
223
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
223
224
|
HTMLTemplate += '<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><meta charset="UTF-8">';
|
|
225
|
+
for (const module of requiredClientModules) {
|
|
226
|
+
HTMLTemplate += `<script src="/shipped/${module}.js" defer="true"></script>`;
|
|
227
|
+
}
|
|
224
228
|
if (addPageScriptTag === true) {
|
|
225
229
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
226
230
|
}
|
|
227
|
-
HTMLTemplate += `<script
|
|
231
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
228
232
|
const builtHead = head2();
|
|
229
233
|
for (const child of builtHead.children) {
|
|
230
234
|
HTMLTemplate += renderRecursively(child);
|
|
@@ -374,7 +378,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
374
378
|
}
|
|
375
379
|
return element;
|
|
376
380
|
};
|
|
377
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
381
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
378
382
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
379
383
|
return [];
|
|
380
384
|
}
|
|
@@ -389,7 +393,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
389
393
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
390
394
|
head: metadata,
|
|
391
395
|
addPageScriptTag: true,
|
|
392
|
-
name: pageName
|
|
396
|
+
name: pageName,
|
|
397
|
+
requiredClientModules
|
|
393
398
|
});
|
|
394
399
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
395
400
|
return {
|
|
@@ -500,6 +505,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
500
505
|
resetLoadHooks();
|
|
501
506
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
502
507
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
508
|
+
let modules = [];
|
|
503
509
|
try {
|
|
504
510
|
const {
|
|
505
511
|
construct
|
|
@@ -508,8 +514,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
508
514
|
page,
|
|
509
515
|
metadata: pageMetadata,
|
|
510
516
|
isDynamicPage,
|
|
511
|
-
requestHook
|
|
517
|
+
requestHook,
|
|
518
|
+
requiredClientModules
|
|
512
519
|
} = construct();
|
|
520
|
+
if (requiredClientModules !== void 0) {
|
|
521
|
+
modules = requiredClientModules;
|
|
522
|
+
}
|
|
513
523
|
if (typeof requestHook === "function") {
|
|
514
524
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
515
525
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -555,7 +565,8 @@ ${e?.stack ?? "No stack."}
|
|
|
555
565
|
pageElements || body(),
|
|
556
566
|
metadata ?? (() => head()),
|
|
557
567
|
DIST_DIR,
|
|
558
|
-
"page"
|
|
568
|
+
"page",
|
|
569
|
+
modules
|
|
559
570
|
);
|
|
560
571
|
await generateClientPageData(
|
|
561
572
|
path.dirname(filePath),
|