@modern-js/plugin-server 0.0.0-nightly-20240630170643 → 0.0.0-nightly-20240701170657

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.
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var assets_exports = {};
20
+ __export(assets_exports, {
21
+ getAssets: () => getAssets,
22
+ getAssetsTags: () => getAssetsTags,
23
+ injectAssetsTags: () => injectAssetsTags
24
+ });
25
+ module.exports = __toCommonJS(assets_exports);
26
+ var import_remix_router = require("@modern-js/runtime-utils/remix-router");
27
+ var import_node = require("@modern-js/runtime-utils/node");
28
+ const createScript = (href, nonce) => `<script defer src="${href}"${nonce ? ` nonce="${nonce}"` : ""}></script>`;
29
+ const createCss = (href) => `<link rel="stylesheet" type="text/css" href="${href}">`;
30
+ const getAssets = (html, nestedRoutes, routesJson, routesManifest, pathname) => {
31
+ const serverRoutes = routesJson.routes;
32
+ const entry = (0, import_node.matchEntry)(pathname, serverRoutes);
33
+ if (!entry) {
34
+ return {};
35
+ }
36
+ const { entryName } = entry;
37
+ if (!entryName) {
38
+ return {};
39
+ }
40
+ const entryRoutes = nestedRoutes[entryName];
41
+ if (!entryRoutes) {
42
+ return {};
43
+ }
44
+ const { routeAssets } = routesManifest;
45
+ const matches = (0, import_remix_router.matchRoutes)(entryRoutes, pathname, entry.urlPath);
46
+ const assets = matches === null || matches === void 0 ? void 0 : matches.reduce((acc, match) => {
47
+ const routeId = match.route.id;
48
+ if (routeId) {
49
+ const matchedManifest = routeAssets[routeId];
50
+ const assets2 = matchedManifest === null || matchedManifest === void 0 ? void 0 : matchedManifest.assets;
51
+ if (Array.isArray(assets2)) {
52
+ acc.push(...assets2);
53
+ }
54
+ }
55
+ return acc;
56
+ }, []);
57
+ const cssAssets = assets === null || assets === void 0 ? void 0 : assets.filter((asset) => asset.endsWith(".css")).filter((asset) => !html.includes(asset));
58
+ const jsAssets = assets === null || assets === void 0 ? void 0 : assets.filter((asset) => asset.endsWith(".js")).filter((asset) => !asset.includes("hot-update")).filter((asset) => !html.includes(asset));
59
+ return {
60
+ cssAssets,
61
+ jsAssets
62
+ };
63
+ };
64
+ const getAssetsTags = (html, nestedRoutes, routesJson, routesManifest, pathname, nonce) => {
65
+ const { cssAssets, jsAssets } = getAssets(html, nestedRoutes, routesJson, routesManifest, pathname);
66
+ const cssLinks = cssAssets === null || cssAssets === void 0 ? void 0 : cssAssets.map(createCss);
67
+ const scripts = jsAssets === null || jsAssets === void 0 ? void 0 : jsAssets.map((asset) => createScript(asset, nonce));
68
+ return {
69
+ cssLinks,
70
+ scripts
71
+ };
72
+ };
73
+ const injectAssetsTags = (html, cssLinks, scripts) => {
74
+ let injectedHtml = html;
75
+ if (cssLinks && cssLinks.length > 0) {
76
+ injectedHtml = injectedHtml.replace("</head>", `${cssLinks.join("\n")}</head>`);
77
+ }
78
+ if (scripts && scripts.length > 0) {
79
+ injectedHtml = injectedHtml.replace(`</body>`, `${scripts.join("\n")}</body>`);
80
+ }
81
+ return injectedHtml;
82
+ };
83
+ // Annotate the CommonJS export names for ESM import in node:
84
+ 0 && (module.exports = {
85
+ getAssets,
86
+ getAssetsTags,
87
+ injectAssetsTags
88
+ });
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var node_exports = {};
30
+ __export(node_exports, {
31
+ getAssets: () => import_assets2.getAssets,
32
+ getAssetsTags: () => import_assets2.getAssetsTags,
33
+ injectAssets: () => injectAssets
34
+ });
35
+ module.exports = __toCommonJS(node_exports);
36
+ var import_node_path = __toESM(require("node:path"));
37
+ var import_fs = __toESM(require("fs"));
38
+ var import_constants = require("@modern-js/utils/universal/constants");
39
+ var import_utils = require("@modern-js/utils");
40
+ var import_assets = require("./assets");
41
+ var import_assets2 = require("./assets");
42
+ const injectAssets = async ({ html, pathname, distDir, nonce }) => {
43
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
44
+ return html;
45
+ }
46
+ try {
47
+ const nestedRoutesSpec = import_node_path.default.join(distDir, import_constants.NESTED_ROUTE_SPEC_FILE);
48
+ if (!import_fs.default.existsSync(nestedRoutesSpec)) {
49
+ return html;
50
+ }
51
+ let injectedHtml = html;
52
+ const routesJson = await Promise.resolve().then(() => __toESM(require(import_node_path.default.join(distDir, import_constants.ROUTE_SPEC_FILE))));
53
+ const nestedRouteSpec = await Promise.resolve().then(() => __toESM(require(nestedRoutesSpec)));
54
+ const routesManifest = await Promise.resolve().then(() => __toESM(require(import_node_path.default.join(distDir, import_utils.ROUTE_MANIFEST_FILE))));
55
+ const { cssLinks, scripts } = (0, import_assets.getAssetsTags)(html, nestedRouteSpec, routesJson, routesManifest, pathname, nonce);
56
+ injectedHtml = (0, import_assets.injectAssetsTags)(injectedHtml, cssLinks, scripts);
57
+ return injectedHtml;
58
+ } catch (error) {
59
+ if (process.env.NODE_ENV === "development") {
60
+ console.error(error);
61
+ }
62
+ return html;
63
+ }
64
+ };
65
+ // Annotate the CommonJS export names for ESM import in node:
66
+ 0 && (module.exports = {
67
+ getAssets,
68
+ getAssetsTags,
69
+ injectAssets
70
+ });
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var worker_exports = {};
20
+ __export(worker_exports, {
21
+ getAssets: () => import_assets2.getAssets,
22
+ getAssetsTags: () => import_assets2.getAssetsTags,
23
+ injectAssets: () => injectAssets
24
+ });
25
+ module.exports = __toCommonJS(worker_exports);
26
+ var import_assets = require("./assets");
27
+ var import_assets2 = require("./assets");
28
+ const injectAssets = async ({ html, pathname, nonce, nestedRoutes, routesManifest, routesJson }) => {
29
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
30
+ return html;
31
+ }
32
+ try {
33
+ if (!nestedRoutes || !routesManifest || !routesJson) {
34
+ return html;
35
+ }
36
+ let injectedHtml = html;
37
+ const { cssLinks, scripts } = (0, import_assets.getAssetsTags)(html, nestedRoutes, routesJson, routesManifest, pathname, nonce);
38
+ injectedHtml = (0, import_assets.injectAssetsTags)(injectedHtml, cssLinks, scripts);
39
+ return injectedHtml;
40
+ } catch (error) {
41
+ if (process.env.NODE_ENV === "development") {
42
+ console.error(error);
43
+ }
44
+ return html;
45
+ }
46
+ };
47
+ // Annotate the CommonJS export names for ESM import in node:
48
+ 0 && (module.exports = {
49
+ getAssets,
50
+ getAssetsTags,
51
+ injectAssets
52
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var node_exports = {};
20
+ __export(node_exports, {
21
+ getAssetsTags: () => import_node.getAssetsTags,
22
+ injectAssets: () => import_node.injectAssets
23
+ });
24
+ module.exports = __toCommonJS(node_exports);
25
+ var import_node = require("./assets/node");
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
28
+ getAssetsTags,
29
+ injectAssets
30
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var worker_exports = {};
20
+ __export(worker_exports, {
21
+ getAssetsTags: () => import_worker.getAssetsTags,
22
+ injectAssets: () => import_worker.injectAssets
23
+ });
24
+ module.exports = __toCommonJS(worker_exports);
25
+ var import_worker = require("./assets/worker");
26
+ // Annotate the CommonJS export names for ESM import in node:
27
+ 0 && (module.exports = {
28
+ getAssetsTags,
29
+ injectAssets
30
+ });
@@ -0,0 +1,80 @@
1
+ import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
2
+ import { matchRoutes } from "@modern-js/runtime-utils/remix-router";
3
+ import { matchEntry } from "@modern-js/runtime-utils/node";
4
+ var createScript = function(href, nonce) {
5
+ return '<script defer src="'.concat(href, '"').concat(nonce ? ' nonce="'.concat(nonce, '"') : "", "></script>");
6
+ };
7
+ var createCss = function(href) {
8
+ return '<link rel="stylesheet" type="text/css" href="'.concat(href, '">');
9
+ };
10
+ var getAssets = function(html, nestedRoutes, routesJson, routesManifest, pathname) {
11
+ var serverRoutes = routesJson.routes;
12
+ var entry = matchEntry(pathname, serverRoutes);
13
+ if (!entry) {
14
+ return {};
15
+ }
16
+ var entryName = entry.entryName;
17
+ if (!entryName) {
18
+ return {};
19
+ }
20
+ var entryRoutes = nestedRoutes[entryName];
21
+ if (!entryRoutes) {
22
+ return {};
23
+ }
24
+ var routeAssets = routesManifest.routeAssets;
25
+ var matches = matchRoutes(entryRoutes, pathname, entry.urlPath);
26
+ var assets = matches === null || matches === void 0 ? void 0 : matches.reduce(function(acc, match) {
27
+ var routeId = match.route.id;
28
+ if (routeId) {
29
+ var matchedManifest = routeAssets[routeId];
30
+ var assets2 = matchedManifest === null || matchedManifest === void 0 ? void 0 : matchedManifest.assets;
31
+ if (Array.isArray(assets2)) {
32
+ var _acc;
33
+ (_acc = acc).push.apply(_acc, _to_consumable_array(assets2));
34
+ }
35
+ }
36
+ return acc;
37
+ }, []);
38
+ var cssAssets = assets === null || assets === void 0 ? void 0 : assets.filter(function(asset) {
39
+ return asset.endsWith(".css");
40
+ }).filter(function(asset) {
41
+ return !html.includes(asset);
42
+ });
43
+ var jsAssets = assets === null || assets === void 0 ? void 0 : assets.filter(function(asset) {
44
+ return asset.endsWith(".js");
45
+ }).filter(function(asset) {
46
+ return !asset.includes("hot-update");
47
+ }).filter(function(asset) {
48
+ return !html.includes(asset);
49
+ });
50
+ return {
51
+ cssAssets,
52
+ jsAssets
53
+ };
54
+ };
55
+ var getAssetsTags = function(html, nestedRoutes, routesJson, routesManifest, pathname, nonce) {
56
+ var _getAssets = getAssets(html, nestedRoutes, routesJson, routesManifest, pathname), cssAssets = _getAssets.cssAssets, jsAssets = _getAssets.jsAssets;
57
+ var cssLinks = cssAssets === null || cssAssets === void 0 ? void 0 : cssAssets.map(createCss);
58
+ var scripts = jsAssets === null || jsAssets === void 0 ? void 0 : jsAssets.map(function(asset) {
59
+ return createScript(asset, nonce);
60
+ });
61
+ return {
62
+ cssLinks,
63
+ scripts
64
+ };
65
+ };
66
+ var injectAssetsTags = function(html, cssLinks, scripts) {
67
+ var injectedHtml = html;
68
+ if (cssLinks && cssLinks.length > 0) {
69
+ injectedHtml = injectedHtml.replace("</head>", "".concat(cssLinks.join("\n"), "</head>"));
70
+ }
71
+ if (scripts && scripts.length > 0) {
72
+ injectedHtml = injectedHtml.replace("</body>", "".concat(scripts.join("\n"), "</body>"));
73
+ }
74
+ return injectedHtml;
75
+ };
76
+ export {
77
+ getAssets,
78
+ getAssetsTags,
79
+ injectAssetsTags
80
+ };
@@ -0,0 +1,86 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import path from "node:path";
4
+ import fs from "fs";
5
+ import { NESTED_ROUTE_SPEC_FILE, ROUTE_SPEC_FILE } from "@modern-js/utils/universal/constants";
6
+ import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
7
+ import { getAssetsTags, injectAssetsTags } from "./assets";
8
+ import { getAssetsTags as getAssetsTags2, getAssets } from "./assets";
9
+ var injectAssets = function() {
10
+ var _ref = _async_to_generator(function(param) {
11
+ var html, pathname, distDir, nonce, nestedRoutesSpec, injectedHtml, routesJson, nestedRouteSpec, routesManifest, _getAssetsTags, cssLinks, scripts, error;
12
+ return _ts_generator(this, function(_state) {
13
+ switch (_state.label) {
14
+ case 0:
15
+ html = param.html, pathname = param.pathname, distDir = param.distDir, nonce = param.nonce;
16
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
17
+ return [
18
+ 2,
19
+ html
20
+ ];
21
+ }
22
+ _state.label = 1;
23
+ case 1:
24
+ _state.trys.push([
25
+ 1,
26
+ 5,
27
+ ,
28
+ 6
29
+ ]);
30
+ nestedRoutesSpec = path.join(distDir, NESTED_ROUTE_SPEC_FILE);
31
+ if (!fs.existsSync(nestedRoutesSpec)) {
32
+ return [
33
+ 2,
34
+ html
35
+ ];
36
+ }
37
+ injectedHtml = html;
38
+ return [
39
+ 4,
40
+ import(path.join(distDir, ROUTE_SPEC_FILE))
41
+ ];
42
+ case 2:
43
+ routesJson = _state.sent();
44
+ return [
45
+ 4,
46
+ import(nestedRoutesSpec)
47
+ ];
48
+ case 3:
49
+ nestedRouteSpec = _state.sent();
50
+ return [
51
+ 4,
52
+ import(path.join(distDir, ROUTE_MANIFEST_FILE))
53
+ ];
54
+ case 4:
55
+ routesManifest = _state.sent();
56
+ _getAssetsTags = getAssetsTags(html, nestedRouteSpec, routesJson, routesManifest, pathname, nonce), cssLinks = _getAssetsTags.cssLinks, scripts = _getAssetsTags.scripts;
57
+ injectedHtml = injectAssetsTags(injectedHtml, cssLinks, scripts);
58
+ return [
59
+ 2,
60
+ injectedHtml
61
+ ];
62
+ case 5:
63
+ error = _state.sent();
64
+ if (process.env.NODE_ENV === "development") {
65
+ console.error(error);
66
+ }
67
+ return [
68
+ 2,
69
+ html
70
+ ];
71
+ case 6:
72
+ return [
73
+ 2
74
+ ];
75
+ }
76
+ });
77
+ });
78
+ return function injectAssets2(_) {
79
+ return _ref.apply(this, arguments);
80
+ };
81
+ }();
82
+ export {
83
+ getAssets,
84
+ getAssetsTags2 as getAssetsTags,
85
+ injectAssets
86
+ };
@@ -0,0 +1,52 @@
1
+ import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
+ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
+ import { getAssetsTags, injectAssetsTags } from "./assets";
4
+ import { getAssetsTags as getAssetsTags2, getAssets } from "./assets";
5
+ var injectAssets = function() {
6
+ var _ref = _async_to_generator(function(param) {
7
+ var html, pathname, nonce, nestedRoutes, routesManifest, routesJson, injectedHtml, _getAssetsTags, cssLinks, scripts;
8
+ return _ts_generator(this, function(_state) {
9
+ html = param.html, pathname = param.pathname, nonce = param.nonce, nestedRoutes = param.nestedRoutes, routesManifest = param.routesManifest, routesJson = param.routesJson;
10
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
11
+ return [
12
+ 2,
13
+ html
14
+ ];
15
+ }
16
+ try {
17
+ if (!nestedRoutes || !routesManifest || !routesJson) {
18
+ return [
19
+ 2,
20
+ html
21
+ ];
22
+ }
23
+ injectedHtml = html;
24
+ _getAssetsTags = getAssetsTags(html, nestedRoutes, routesJson, routesManifest, pathname, nonce), cssLinks = _getAssetsTags.cssLinks, scripts = _getAssetsTags.scripts;
25
+ injectedHtml = injectAssetsTags(injectedHtml, cssLinks, scripts);
26
+ return [
27
+ 2,
28
+ injectedHtml
29
+ ];
30
+ } catch (error) {
31
+ if (process.env.NODE_ENV === "development") {
32
+ console.error(error);
33
+ }
34
+ return [
35
+ 2,
36
+ html
37
+ ];
38
+ }
39
+ return [
40
+ 2
41
+ ];
42
+ });
43
+ });
44
+ return function injectAssets2(_) {
45
+ return _ref.apply(this, arguments);
46
+ };
47
+ }();
48
+ export {
49
+ getAssets,
50
+ getAssetsTags2 as getAssetsTags,
51
+ injectAssets
52
+ };
@@ -0,0 +1,5 @@
1
+ import { injectAssets, getAssetsTags } from "./assets/node";
2
+ export {
3
+ getAssetsTags,
4
+ injectAssets
5
+ };
@@ -0,0 +1,5 @@
1
+ import { injectAssets, getAssetsTags } from "./assets/worker";
2
+ export {
3
+ getAssetsTags,
4
+ injectAssets
5
+ };
@@ -0,0 +1,62 @@
1
+ import { matchRoutes } from "@modern-js/runtime-utils/remix-router";
2
+ import { matchEntry } from "@modern-js/runtime-utils/node";
3
+ const createScript = (href, nonce) => `<script defer src="${href}"${nonce ? ` nonce="${nonce}"` : ""}></script>`;
4
+ const createCss = (href) => `<link rel="stylesheet" type="text/css" href="${href}">`;
5
+ const getAssets = (html, nestedRoutes, routesJson, routesManifest, pathname) => {
6
+ const serverRoutes = routesJson.routes;
7
+ const entry = matchEntry(pathname, serverRoutes);
8
+ if (!entry) {
9
+ return {};
10
+ }
11
+ const { entryName } = entry;
12
+ if (!entryName) {
13
+ return {};
14
+ }
15
+ const entryRoutes = nestedRoutes[entryName];
16
+ if (!entryRoutes) {
17
+ return {};
18
+ }
19
+ const { routeAssets } = routesManifest;
20
+ const matches = matchRoutes(entryRoutes, pathname, entry.urlPath);
21
+ const assets = matches === null || matches === void 0 ? void 0 : matches.reduce((acc, match) => {
22
+ const routeId = match.route.id;
23
+ if (routeId) {
24
+ const matchedManifest = routeAssets[routeId];
25
+ const assets2 = matchedManifest === null || matchedManifest === void 0 ? void 0 : matchedManifest.assets;
26
+ if (Array.isArray(assets2)) {
27
+ acc.push(...assets2);
28
+ }
29
+ }
30
+ return acc;
31
+ }, []);
32
+ const cssAssets = assets === null || assets === void 0 ? void 0 : assets.filter((asset) => asset.endsWith(".css")).filter((asset) => !html.includes(asset));
33
+ const jsAssets = assets === null || assets === void 0 ? void 0 : assets.filter((asset) => asset.endsWith(".js")).filter((asset) => !asset.includes("hot-update")).filter((asset) => !html.includes(asset));
34
+ return {
35
+ cssAssets,
36
+ jsAssets
37
+ };
38
+ };
39
+ const getAssetsTags = (html, nestedRoutes, routesJson, routesManifest, pathname, nonce) => {
40
+ const { cssAssets, jsAssets } = getAssets(html, nestedRoutes, routesJson, routesManifest, pathname);
41
+ const cssLinks = cssAssets === null || cssAssets === void 0 ? void 0 : cssAssets.map(createCss);
42
+ const scripts = jsAssets === null || jsAssets === void 0 ? void 0 : jsAssets.map((asset) => createScript(asset, nonce));
43
+ return {
44
+ cssLinks,
45
+ scripts
46
+ };
47
+ };
48
+ const injectAssetsTags = (html, cssLinks, scripts) => {
49
+ let injectedHtml = html;
50
+ if (cssLinks && cssLinks.length > 0) {
51
+ injectedHtml = injectedHtml.replace("</head>", `${cssLinks.join("\n")}</head>`);
52
+ }
53
+ if (scripts && scripts.length > 0) {
54
+ injectedHtml = injectedHtml.replace(`</body>`, `${scripts.join("\n")}</body>`);
55
+ }
56
+ return injectedHtml;
57
+ };
58
+ export {
59
+ getAssets,
60
+ getAssetsTags,
61
+ injectAssetsTags
62
+ };
@@ -0,0 +1,34 @@
1
+ import path from "node:path";
2
+ import fs from "fs";
3
+ import { NESTED_ROUTE_SPEC_FILE, ROUTE_SPEC_FILE } from "@modern-js/utils/universal/constants";
4
+ import { ROUTE_MANIFEST_FILE } from "@modern-js/utils";
5
+ import { getAssetsTags, injectAssetsTags } from "./assets";
6
+ import { getAssetsTags as getAssetsTags2, getAssets } from "./assets";
7
+ const injectAssets = async ({ html, pathname, distDir, nonce }) => {
8
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
9
+ return html;
10
+ }
11
+ try {
12
+ const nestedRoutesSpec = path.join(distDir, NESTED_ROUTE_SPEC_FILE);
13
+ if (!fs.existsSync(nestedRoutesSpec)) {
14
+ return html;
15
+ }
16
+ let injectedHtml = html;
17
+ const routesJson = await import(path.join(distDir, ROUTE_SPEC_FILE));
18
+ const nestedRouteSpec = await import(nestedRoutesSpec);
19
+ const routesManifest = await import(path.join(distDir, ROUTE_MANIFEST_FILE));
20
+ const { cssLinks, scripts } = getAssetsTags(html, nestedRouteSpec, routesJson, routesManifest, pathname, nonce);
21
+ injectedHtml = injectAssetsTags(injectedHtml, cssLinks, scripts);
22
+ return injectedHtml;
23
+ } catch (error) {
24
+ if (process.env.NODE_ENV === "development") {
25
+ console.error(error);
26
+ }
27
+ return html;
28
+ }
29
+ };
30
+ export {
31
+ getAssets,
32
+ getAssetsTags2 as getAssetsTags,
33
+ injectAssets
34
+ };
@@ -0,0 +1,26 @@
1
+ import { getAssetsTags, injectAssetsTags } from "./assets";
2
+ import { getAssetsTags as getAssetsTags2, getAssets } from "./assets";
3
+ const injectAssets = async ({ html, pathname, nonce, nestedRoutes, routesManifest, routesJson }) => {
4
+ if (typeof html !== "string" || (html === null || html === void 0 ? void 0 : html.includes("window._SSR_DATA")) || !pathname) {
5
+ return html;
6
+ }
7
+ try {
8
+ if (!nestedRoutes || !routesManifest || !routesJson) {
9
+ return html;
10
+ }
11
+ let injectedHtml = html;
12
+ const { cssLinks, scripts } = getAssetsTags(html, nestedRoutes, routesJson, routesManifest, pathname, nonce);
13
+ injectedHtml = injectAssetsTags(injectedHtml, cssLinks, scripts);
14
+ return injectedHtml;
15
+ } catch (error) {
16
+ if (process.env.NODE_ENV === "development") {
17
+ console.error(error);
18
+ }
19
+ return html;
20
+ }
21
+ };
22
+ export {
23
+ getAssets,
24
+ getAssetsTags2 as getAssetsTags,
25
+ injectAssets
26
+ };
@@ -0,0 +1,5 @@
1
+ import { injectAssets, getAssetsTags } from "./assets/node";
2
+ export {
3
+ getAssetsTags,
4
+ injectAssets
5
+ };
@@ -0,0 +1,5 @@
1
+ import { injectAssets, getAssetsTags } from "./assets/worker";
2
+ export {
3
+ getAssetsTags,
4
+ injectAssets
5
+ };
@@ -0,0 +1,12 @@
1
+ export declare const getAssets: (html: string, nestedRoutes: Record<string, any>, routesJson: Record<string, any>, routesManifest: Record<string, any>, pathname: string) => {
2
+ cssAssets?: undefined;
3
+ jsAssets?: undefined;
4
+ } | {
5
+ cssAssets: string[] | undefined;
6
+ jsAssets: string[] | undefined;
7
+ };
8
+ export declare const getAssetsTags: (html: string, nestedRoutes: Record<string, any>, routesJson: Record<string, any>, routesManifest: Record<string, any>, pathname: string, nonce?: string) => {
9
+ cssLinks: string[] | undefined;
10
+ scripts: string[] | undefined;
11
+ };
12
+ export declare const injectAssetsTags: (html: string, cssLinks?: string[], scripts?: string[]) => string;
@@ -0,0 +1,7 @@
1
+ export { getAssetsTags, getAssets } from './assets';
2
+ export declare const injectAssets: ({ html, pathname, distDir, nonce, }: {
3
+ html: string;
4
+ pathname?: string | undefined;
5
+ distDir: string;
6
+ nonce?: string | undefined;
7
+ }) => Promise<string>;
@@ -0,0 +1,9 @@
1
+ export { getAssetsTags, getAssets } from './assets';
2
+ export declare const injectAssets: ({ html, pathname, nonce, nestedRoutes, routesManifest, routesJson, }: {
3
+ html: string;
4
+ pathname?: string | undefined;
5
+ nonce?: string | undefined;
6
+ nestedRoutes: Record<string, any>;
7
+ routesManifest: Record<string, any>;
8
+ routesJson: Record<string, any>;
9
+ }) => Promise<string>;
@@ -0,0 +1 @@
1
+ export { injectAssets, getAssetsTags } from './assets/node';
@@ -0,0 +1 @@
1
+ export { injectAssets, getAssetsTags } from './assets/worker';
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "0.0.0-nightly-20240630170643",
18
+ "version": "0.0.0-nightly-20240701170657",
19
19
  "types": "./dist/types/cli.d.ts",
20
20
  "jsnext:source": "./src/cli",
21
21
  "main": "./dist/cjs/cli.js",
@@ -34,6 +34,14 @@
34
34
  "types": "./dist/types/server.d.ts",
35
35
  "jsnext:source": "./src/server.ts",
36
36
  "default": "./dist/cjs/server.js"
37
+ },
38
+ "./runtime": {
39
+ "types": "./dist/types/runtime/node.d.ts",
40
+ "default": "./dist/cjs/runtime/node.js"
41
+ },
42
+ "./runtime/worker": {
43
+ "types": "./dist/types/runtime/worker.d.ts",
44
+ "default": "./dist/esm-node/runtime/worker.js"
37
45
  }
38
46
  },
39
47
  "typesVersions": {
@@ -46,25 +54,32 @@
46
54
  ],
47
55
  "server": [
48
56
  "./dist/types/server.d.ts"
57
+ ],
58
+ "runtime": [
59
+ "./dist/types/runtime/node.d.ts"
60
+ ],
61
+ "runtime/worker": [
62
+ "./dist/types/runtime/worker.d.ts"
49
63
  ]
50
64
  }
51
65
  },
52
66
  "dependencies": {
53
67
  "@swc/helpers": "0.5.3",
54
- "@modern-js/server-utils": "0.0.0-nightly-20240630170643",
55
- "@modern-js/utils": "0.0.0-nightly-20240630170643"
68
+ "@modern-js/runtime-utils": "0.0.0-nightly-20240701170657",
69
+ "@modern-js/server-utils": "0.0.0-nightly-20240701170657",
70
+ "@modern-js/utils": "0.0.0-nightly-20240701170657"
56
71
  },
57
72
  "devDependencies": {
58
- "typescript": "^5",
59
73
  "@types/jest": "^29",
60
74
  "@types/node": "^14",
61
75
  "jest": "^29",
62
- "@modern-js/server-core": "0.0.0-nightly-20240630170643",
63
- "@modern-js/app-tools": "0.0.0-nightly-20240630170643",
64
- "@modern-js/core": "0.0.0-nightly-20240630170643",
65
- "@modern-js/types": "0.0.0-nightly-20240630170643",
66
- "@scripts/build": "0.0.0-nightly-20240630170643",
67
- "@scripts/jest-config": "0.0.0-nightly-20240630170643"
76
+ "typescript": "^5",
77
+ "@modern-js/core": "0.0.0-nightly-20240701170657",
78
+ "@modern-js/server-core": "0.0.0-nightly-20240701170657",
79
+ "@modern-js/app-tools": "0.0.0-nightly-20240701170657",
80
+ "@modern-js/types": "0.0.0-nightly-20240701170657",
81
+ "@scripts/build": "0.0.0-nightly-20240701170657",
82
+ "@scripts/jest-config": "0.0.0-nightly-20240701170657"
68
83
  },
69
84
  "sideEffects": [
70
85
  "*.css",