elegance-js 1.11.30 → 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 +20 -10
- package/dist/compile_docs.mjs +20 -10
- package/dist/dynamic_page.mjs +17 -6
- package/dist/log.mjs +3 -4
- 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 +20 -10
- package/package.json +1 -1
package/dist/build.mjs
CHANGED
|
@@ -26,15 +26,14 @@ function color(text, code) {
|
|
|
26
26
|
}
|
|
27
27
|
function logInfo(...args) {
|
|
28
28
|
if (quiet) return;
|
|
29
|
-
console.info(
|
|
29
|
+
console.info(`Elegance.JS: ${getTimestamp()} ${color("[INFO]:", 34)}`, ...args);
|
|
30
30
|
}
|
|
31
31
|
function logWarn(...args) {
|
|
32
32
|
if (quiet) return;
|
|
33
|
-
console.warn(
|
|
33
|
+
console.warn(`Elegance.JS: ${getTimestamp()} ${color("[WARN]:", 33)}`, ...args);
|
|
34
34
|
}
|
|
35
35
|
function logError(...args) {
|
|
36
|
-
|
|
37
|
-
console.error(`${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
36
|
+
console.error(`Elegance.JS: ${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
38
37
|
}
|
|
39
38
|
var log = {
|
|
40
39
|
info: logInfo,
|
|
@@ -261,14 +260,18 @@ var generateHTMLTemplate = ({
|
|
|
261
260
|
head: head2,
|
|
262
261
|
serverData = null,
|
|
263
262
|
addPageScriptTag = true,
|
|
264
|
-
name
|
|
263
|
+
name,
|
|
264
|
+
requiredClientModules = []
|
|
265
265
|
}) => {
|
|
266
266
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
267
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
|
+
}
|
|
268
271
|
if (addPageScriptTag === true) {
|
|
269
272
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
270
273
|
}
|
|
271
|
-
HTMLTemplate += `<script
|
|
274
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
272
275
|
const builtHead = head2();
|
|
273
276
|
for (const child2 of builtHead.children) {
|
|
274
277
|
HTMLTemplate += renderRecursively(child2);
|
|
@@ -418,7 +421,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
418
421
|
}
|
|
419
422
|
return element;
|
|
420
423
|
};
|
|
421
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
424
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
422
425
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
423
426
|
return [];
|
|
424
427
|
}
|
|
@@ -433,7 +436,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
433
436
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
434
437
|
head: metadata,
|
|
435
438
|
addPageScriptTag: true,
|
|
436
|
-
name: pageName
|
|
439
|
+
name: pageName,
|
|
440
|
+
requiredClientModules
|
|
437
441
|
});
|
|
438
442
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
439
443
|
return {
|
|
@@ -544,6 +548,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
544
548
|
resetLoadHooks();
|
|
545
549
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
546
550
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
551
|
+
let modules = [];
|
|
547
552
|
try {
|
|
548
553
|
const {
|
|
549
554
|
construct
|
|
@@ -552,8 +557,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
552
557
|
page,
|
|
553
558
|
metadata: pageMetadata,
|
|
554
559
|
isDynamicPage,
|
|
555
|
-
requestHook
|
|
560
|
+
requestHook,
|
|
561
|
+
requiredClientModules
|
|
556
562
|
} = construct();
|
|
563
|
+
if (requiredClientModules !== void 0) {
|
|
564
|
+
modules = requiredClientModules;
|
|
565
|
+
}
|
|
557
566
|
if (typeof requestHook === "function") {
|
|
558
567
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
559
568
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -599,7 +608,8 @@ ${e?.stack ?? "No stack."}
|
|
|
599
608
|
pageElements || body(),
|
|
600
609
|
metadata ?? (() => head()),
|
|
601
610
|
DIST_DIR,
|
|
602
|
-
"page"
|
|
611
|
+
"page",
|
|
612
|
+
modules
|
|
603
613
|
);
|
|
604
614
|
await generateClientPageData(
|
|
605
615
|
path.dirname(filePath),
|
package/dist/compile_docs.mjs
CHANGED
|
@@ -29,15 +29,14 @@ function color(text, code) {
|
|
|
29
29
|
}
|
|
30
30
|
function logInfo(...args) {
|
|
31
31
|
if (quiet) return;
|
|
32
|
-
console.info(
|
|
32
|
+
console.info(`Elegance.JS: ${getTimestamp()} ${color("[INFO]:", 34)}`, ...args);
|
|
33
33
|
}
|
|
34
34
|
function logWarn(...args) {
|
|
35
35
|
if (quiet) return;
|
|
36
|
-
console.warn(
|
|
36
|
+
console.warn(`Elegance.JS: ${getTimestamp()} ${color("[WARN]:", 33)}`, ...args);
|
|
37
37
|
}
|
|
38
38
|
function logError(...args) {
|
|
39
|
-
|
|
40
|
-
console.error(`${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
39
|
+
console.error(`Elegance.JS: ${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
41
40
|
}
|
|
42
41
|
var log = {
|
|
43
42
|
info: logInfo,
|
|
@@ -264,14 +263,18 @@ var generateHTMLTemplate = ({
|
|
|
264
263
|
head: head2,
|
|
265
264
|
serverData = null,
|
|
266
265
|
addPageScriptTag = true,
|
|
267
|
-
name
|
|
266
|
+
name,
|
|
267
|
+
requiredClientModules = []
|
|
268
268
|
}) => {
|
|
269
269
|
let HTMLTemplate = `<head><meta name="viewport" content="width=device-width, initial-scale=1.0">`;
|
|
270
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
|
+
}
|
|
271
274
|
if (addPageScriptTag === true) {
|
|
272
275
|
HTMLTemplate += `<script data-tag="true" type="module" src="${pageURL === "" ? "" : "/"}${pageURL}/${name}_data.js" defer="true"></script>`;
|
|
273
276
|
}
|
|
274
|
-
HTMLTemplate += `<script
|
|
277
|
+
HTMLTemplate += `<script type="module" src="/client.js" defer="true"></script>`;
|
|
275
278
|
const builtHead = head2();
|
|
276
279
|
for (const child2 of builtHead.children) {
|
|
277
280
|
HTMLTemplate += renderRecursively(child2);
|
|
@@ -421,7 +424,7 @@ var processPageElements = async (element, objectAttributes, parent) => {
|
|
|
421
424
|
}
|
|
422
425
|
return element;
|
|
423
426
|
};
|
|
424
|
-
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName) => {
|
|
427
|
+
var generateSuitablePageElements = async (pageLocation, pageElements, metadata, DIST_DIR, pageName, requiredClientModules) => {
|
|
425
428
|
if (typeof pageElements === "string" || typeof pageElements === "boolean" || typeof pageElements === "number" || Array.isArray(pageElements)) {
|
|
426
429
|
return [];
|
|
427
430
|
}
|
|
@@ -436,7 +439,8 @@ var generateSuitablePageElements = async (pageLocation, pageElements, metadata,
|
|
|
436
439
|
pageURL: path.relative(DIST_DIR, pageLocation),
|
|
437
440
|
head: metadata,
|
|
438
441
|
addPageScriptTag: true,
|
|
439
|
-
name: pageName
|
|
442
|
+
name: pageName,
|
|
443
|
+
requiredClientModules
|
|
440
444
|
});
|
|
441
445
|
const resultHTML = `<!DOCTYPE html><html>${template}${renderedPage.bodyHTML}</html>`;
|
|
442
446
|
return {
|
|
@@ -547,6 +551,7 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
547
551
|
resetLoadHooks();
|
|
548
552
|
globalThis.__SERVER_PAGE_DATA_BANNER__ = "";
|
|
549
553
|
globalThis.__SERVER_CURRENT_STATE_ID__ = 1;
|
|
554
|
+
let modules = [];
|
|
550
555
|
try {
|
|
551
556
|
const {
|
|
552
557
|
construct
|
|
@@ -555,8 +560,12 @@ var buildDynamicPage = async (filePath, DIST_DIR, req, res) => {
|
|
|
555
560
|
page,
|
|
556
561
|
metadata: pageMetadata,
|
|
557
562
|
isDynamicPage,
|
|
558
|
-
requestHook
|
|
563
|
+
requestHook,
|
|
564
|
+
requiredClientModules
|
|
559
565
|
} = construct();
|
|
566
|
+
if (requiredClientModules !== void 0) {
|
|
567
|
+
modules = requiredClientModules;
|
|
568
|
+
}
|
|
560
569
|
if (typeof requestHook === "function") {
|
|
561
570
|
if (requestHook.constructor.name === "AsyncFunction") {
|
|
562
571
|
const doProcessRequest = await requestHook(req, res);
|
|
@@ -602,7 +611,8 @@ ${e?.stack ?? "No stack."}
|
|
|
602
611
|
pageElements || body(),
|
|
603
612
|
metadata ?? (() => head()),
|
|
604
613
|
DIST_DIR,
|
|
605
|
-
"page"
|
|
614
|
+
"page",
|
|
615
|
+
modules
|
|
606
616
|
);
|
|
607
617
|
await generateClientPageData(
|
|
608
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/log.mjs
CHANGED
|
@@ -19,15 +19,14 @@ function color(text, code) {
|
|
|
19
19
|
}
|
|
20
20
|
function logInfo(...args) {
|
|
21
21
|
if (quiet) return;
|
|
22
|
-
console.info(
|
|
22
|
+
console.info(`Elegance.JS: ${getTimestamp()} ${color("[INFO]:", 34)}`, ...args);
|
|
23
23
|
}
|
|
24
24
|
function logWarn(...args) {
|
|
25
25
|
if (quiet) return;
|
|
26
|
-
console.warn(
|
|
26
|
+
console.warn(`Elegance.JS: ${getTimestamp()} ${color("[WARN]:", 33)}`, ...args);
|
|
27
27
|
}
|
|
28
28
|
function logError(...args) {
|
|
29
|
-
|
|
30
|
-
console.error(`${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
29
|
+
console.error(`Elegance.JS: ${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
31
30
|
}
|
|
32
31
|
var log = {
|
|
33
32
|
info: logInfo,
|
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),
|
|
@@ -592,15 +603,14 @@ function color(text, code) {
|
|
|
592
603
|
}
|
|
593
604
|
function logInfo(...args) {
|
|
594
605
|
if (quiet) return;
|
|
595
|
-
console.info(
|
|
606
|
+
console.info(`Elegance.JS: ${getTimestamp()} ${color("[INFO]:", 34)}`, ...args);
|
|
596
607
|
}
|
|
597
608
|
function logWarn(...args) {
|
|
598
609
|
if (quiet) return;
|
|
599
|
-
console.warn(
|
|
610
|
+
console.warn(`Elegance.JS: ${getTimestamp()} ${color("[WARN]:", 33)}`, ...args);
|
|
600
611
|
}
|
|
601
612
|
function logError(...args) {
|
|
602
|
-
|
|
603
|
-
console.error(`${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
613
|
+
console.error(`Elegance.JS: ${getTimestamp()} ${color("[ERROR]:", 31)}`, ...args);
|
|
604
614
|
}
|
|
605
615
|
var log = {
|
|
606
616
|
info: logInfo,
|