@modern-js/runtime 2.20.0 → 2.21.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.
Files changed (76) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/dist/cjs/document/cli/index.js +5 -2
  3. package/dist/cjs/router/runtime/DeferredDataScripts.node.js +4 -4
  4. package/dist/cjs/router/runtime/PrefetchLink.js +7 -7
  5. package/dist/cjs/router/runtime/index.js +55 -56
  6. package/dist/cjs/router/runtime/plugin.js +7 -7
  7. package/dist/cjs/router/runtime/plugin.node.js +7 -7
  8. package/dist/cjs/router/runtime/server.js +1 -1
  9. package/dist/cjs/router/runtime/utils.js +6 -5
  10. package/dist/cjs/router/runtime/withRouter.js +4 -4
  11. package/dist/cjs/ssr/cli/index.js +1 -1
  12. package/dist/cjs/ssr/serverRender/renderToStream/buildTemplate.after.js +6 -2
  13. package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +2 -2
  14. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +10 -12
  15. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +11 -11
  16. package/dist/cjs/ssr/serverRender/renderToString/entry.js +10 -4
  17. package/dist/cjs/ssr/serverRender/renderToString/loadable.js +7 -4
  18. package/dist/cjs/ssr/serverRender/utils.js +7 -1
  19. package/dist/esm/document/cli/index.js +6 -3
  20. package/dist/esm/router/runtime/DeferredDataScripts.node.js +1 -1
  21. package/dist/esm/router/runtime/PrefetchLink.js +1 -1
  22. package/dist/esm/router/runtime/index.js +6 -4
  23. package/dist/esm/router/runtime/plugin.js +1 -1
  24. package/dist/esm/router/runtime/plugin.node.js +3 -3
  25. package/dist/esm/router/runtime/server.js +1 -1
  26. package/dist/esm/router/runtime/utils.js +3 -2
  27. package/dist/esm/router/runtime/withRouter.js +1 -1
  28. package/dist/esm/ssr/cli/index.js +1 -1
  29. package/dist/esm/ssr/serverRender/renderToStream/buildTemplate.after.js +6 -2
  30. package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +1 -1
  31. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +10 -12
  32. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +12 -12
  33. package/dist/esm/ssr/serverRender/renderToString/entry.js +11 -5
  34. package/dist/esm/ssr/serverRender/renderToString/loadable.js +8 -5
  35. package/dist/esm/ssr/serverRender/utils.js +7 -0
  36. package/dist/esm-node/document/cli/index.js +5 -2
  37. package/dist/esm-node/router/runtime/DeferredDataScripts.node.js +1 -1
  38. package/dist/esm-node/router/runtime/PrefetchLink.js +1 -1
  39. package/dist/esm-node/router/runtime/index.js +6 -4
  40. package/dist/esm-node/router/runtime/plugin.js +1 -1
  41. package/dist/esm-node/router/runtime/plugin.node.js +3 -3
  42. package/dist/esm-node/router/runtime/server.js +1 -1
  43. package/dist/esm-node/router/runtime/utils.js +3 -2
  44. package/dist/esm-node/router/runtime/withRouter.js +1 -1
  45. package/dist/esm-node/ssr/cli/index.js +1 -1
  46. package/dist/esm-node/ssr/serverRender/renderToStream/buildTemplate.after.js +6 -2
  47. package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +1 -1
  48. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +10 -12
  49. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +11 -11
  50. package/dist/esm-node/ssr/serverRender/renderToString/entry.js +11 -5
  51. package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +8 -5
  52. package/dist/esm-node/ssr/serverRender/utils.js +5 -0
  53. package/dist/types/cli/index.d.ts +0 -2
  54. package/dist/types/core/loader/loaderManager.d.ts +0 -1
  55. package/dist/types/core/loader/useLoader.d.ts +0 -5
  56. package/dist/types/core/plugin.d.ts +1 -6
  57. package/dist/types/document/cli/index.d.ts +0 -2
  58. package/dist/types/router/cli/index.d.ts +0 -2
  59. package/dist/types/router/runtime/DeferredDataScripts.d.ts +0 -1
  60. package/dist/types/router/runtime/DeferredDataScripts.node.d.ts +0 -1
  61. package/dist/types/router/runtime/PrefetchLink.d.ts +1 -3
  62. package/dist/types/router/runtime/hooks.d.ts +1 -1
  63. package/dist/types/router/runtime/index.d.ts +3 -4
  64. package/dist/types/router/runtime/server.d.ts +1 -1
  65. package/dist/types/router/runtime/types.d.ts +1 -3
  66. package/dist/types/router/runtime/utils.d.ts +1 -3
  67. package/dist/types/router/runtime/withRouter.d.ts +1 -1
  68. package/dist/types/runtimeContext.d.ts +1 -1
  69. package/dist/types/ssr/cli/index.d.ts +0 -2
  70. package/dist/types/ssr/serverRender/renderToStream/type.d.ts +2 -2
  71. package/dist/types/ssr/serverRender/renderToString/entry.d.ts +1 -0
  72. package/dist/types/ssr/serverRender/renderToString/type.d.ts +1 -0
  73. package/dist/types/ssr/serverRender/utils.d.ts +2 -1
  74. package/dist/types/state/cli/index.d.ts +0 -2
  75. package/dist/types/state/types.d.ts +0 -1
  76. package/package.json +27 -20
@@ -109,7 +109,8 @@ class Entry {
109
109
  host: this.host,
110
110
  result: this.result,
111
111
  entryName: this.entryName,
112
- config: this.pluginConfig
112
+ config: this.pluginConfig,
113
+ nonce: this.nonce
113
114
  };
114
115
  html = (0, _reduce.reduce)(App, renderContext, [
115
116
  _styledComponent.toHtml,
@@ -129,11 +130,14 @@ class Entry {
129
130
  getSSRDataScript(templateData, routerData) {
130
131
  const useInlineScript = this.pluginConfig.inlineScript !== false;
131
132
  const ssrData = (0, _runtimenode.serializeJson)(templateData);
132
- let ssrDataScripts = useInlineScript ? `<script>window._SSR_DATA = ${ssrData}</script>` : `<script type="application/json" id="${_utils1.SSR_DATA_JSON_ID}">${ssrData}</script>`;
133
+ const attrsStr = (0, _utils1.attributesToString)({
134
+ nonce: this.nonce
135
+ });
136
+ let ssrDataScripts = useInlineScript ? `<script${attrsStr}>window._SSR_DATA = ${ssrData}</script>` : `<script type="application/json" id="${_utils1.SSR_DATA_JSON_ID}">${ssrData}</script>`;
133
137
  if (routerData) {
134
138
  const serializedRouterData = (0, _runtimenode.serializeJson)(routerData);
135
139
  ssrDataScripts += useInlineScript ? `
136
- <script>window._ROUTER_DATA = ${serializedRouterData}</script>` : `
140
+ <script${attrsStr}>window._ROUTER_DATA = ${serializedRouterData}</script>` : `
137
141
  <script type="application/json" id="${_utils1.ROUTER_DATA_JSON_ID}">${serializedRouterData}</script>`;
138
142
  }
139
143
  return {
@@ -149,8 +153,9 @@ class Entry {
149
153
  _define_property._(this, "fragments", void 0);
150
154
  _define_property._(this, "pluginConfig", void 0);
151
155
  _define_property._(this, "host", void 0);
156
+ _define_property._(this, "nonce", void 0);
152
157
  const { ctx, config } = options;
153
- const { entryName, template, request: { host } } = ctx;
158
+ const { entryName, template, request: { host }, nonce } = ctx;
154
159
  this.fragments = (0, _template.toFragments)(template, entryName);
155
160
  this.entryName = entryName;
156
161
  this.host = host;
@@ -158,6 +163,7 @@ class Entry {
158
163
  this.pluginConfig = config;
159
164
  this.metrics = ctx.metrics;
160
165
  this.logger = ctx.logger;
166
+ this.nonce = nonce;
161
167
  this.result = {
162
168
  renderLevel: _types.RenderLevel.CLIENT_RENDER,
163
169
  html: "",
@@ -16,7 +16,7 @@ const extname = (uri) => {
16
16
  return `.${uri === null || uri === void 0 ? void 0 : uri.split(".").pop()}` || "";
17
17
  };
18
18
  const toHtml = (jsx, renderer, next) => {
19
- const { stats, result: { chunksMap }, host, config = {} } = renderer;
19
+ const { stats, result: { chunksMap }, host, config = {}, nonce } = renderer;
20
20
  if (!stats || chunksMap.js) {
21
21
  return next(jsx);
22
22
  }
@@ -32,12 +32,15 @@ const toHtml = (jsx, renderer, next) => {
32
32
  for (const v of chunks) {
33
33
  const fileType = extname(v.url).slice(1);
34
34
  if (fileType === "js") {
35
- const props = [];
35
+ const attributes = {
36
+ nonce
37
+ };
36
38
  const { crossorigin } = config;
37
39
  if (crossorigin && (0, _utils.isCrossOrigin)(v.url, host)) {
38
- props.push(`crossorigin="${crossorigin === true ? "anonymous" : crossorigin}"`);
40
+ attributes.crossorigin = crossorigin === true ? "anonymous" : crossorigin;
39
41
  }
40
- chunksMap[fileType] += `<script src="${v.url}" ${props.join(" ")}></script>`;
42
+ const attrsStr = (0, _utils1.attributesToString)(attributes);
43
+ chunksMap[fileType] += `<script${attrsStr} src="${v.url}"></script>`;
41
44
  } else if (fileType === "css") {
42
45
  chunksMap[fileType] += `<link href="${v.url}" rel="stylesheet" />`;
43
46
  }
@@ -13,7 +13,8 @@ _export(exports, {
13
13
  CSS_CHUNKS_PLACEHOLDER: () => CSS_CHUNKS_PLACEHOLDER,
14
14
  SSR_DATA_JSON_ID: () => SSR_DATA_JSON_ID,
15
15
  ROUTER_DATA_JSON_ID: () => ROUTER_DATA_JSON_ID,
16
- getLoadableScripts: () => getLoadableScripts
16
+ getLoadableScripts: () => getLoadableScripts,
17
+ attributesToString: () => attributesToString
17
18
  });
18
19
  const CSS_CHUNKS_PLACEHOLDER = "<!--<?- chunksMap.css ?>-->";
19
20
  const SSR_DATA_JSON_ID = "__MODERN_SSR_DATA__";
@@ -26,3 +27,8 @@ function getLoadableScripts(extractor) {
26
27
  }
27
28
  return scripts.split("</script>").slice(0, 2).map((i) => `${i}</script>`).join("");
28
29
  }
30
+ function attributesToString(attributes) {
31
+ return Object.entries(attributes).reduce((str, [key, value]) => {
32
+ return value === void 0 ? str : `${str} ${key}="${value}"`;
33
+ }, "");
34
+ }
@@ -57,13 +57,14 @@ export default function() {
57
57
  }
58
58
  return function() {
59
59
  var _ref2 = _async_to_generator(function(param) {
60
- var htmlWebpackPlugin, documentParams, tempTsConfigFile, userTsConfigFilePath, tsConfig, err, htmlOutputFile, Document, HTMLElement, html, scripts, links, metas, finalHtml;
60
+ var htmlWebpackPlugin, config, documentParams, tempTsConfigFile, userTsConfigFilePath, tsConfig, err, htmlOutputFile, Document, HTMLElement, html, scripts, links, metas, nonce, nonceAttr, finalHtml;
61
61
  return _ts_generator(this, function(_state2) {
62
62
  switch (_state2.label) {
63
63
  case 0:
64
64
  htmlWebpackPlugin = param.htmlWebpackPlugin;
65
+ config = api.useResolvedConfigContext();
65
66
  documentParams = getDocParams({
66
- config: api.useResolvedConfigContext(),
67
+ config: config,
67
68
  entryName: entryName,
68
69
  templateParameters: templateParameters
69
70
  });
@@ -173,8 +174,10 @@ export default function() {
173
174
  }).join("")
174
175
  ].join("");
175
176
  if (html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_START) && html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_END)) {
177
+ nonce = config.security.nonce;
178
+ nonceAttr = nonce ? "nonce=".concat(nonce) : "";
176
179
  html = html.replace(new RegExp("".concat(DOCUMENT_SCRIPT_PLACEHOLDER_START, "(.*?)").concat(DOCUMENT_SCRIPT_PLACEHOLDER_END), "g"), function(_scriptStr, $1) {
177
- return "<script>".concat(decodeURIComponent($1), "</script>");
180
+ return "<script ".concat(nonceAttr, ">").concat(decodeURIComponent($1), "</script>");
178
181
  });
179
182
  }
180
183
  if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
@@ -1,7 +1,7 @@
1
1
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
2
2
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import { Suspense, useEffect, useRef, useMemo, useContext } from "react";
4
- import { Await, UNSAFE_DataRouterContext as DataRouterContext, useAsyncError } from "react-router-dom";
4
+ import { Await, UNSAFE_DataRouterContext as DataRouterContext, useAsyncError } from "@modern-js/utils/runtime/router";
5
5
  import { serializeJson } from "@modern-js/utils/runtime-node";
6
6
  import { JSX_SHELL_STREAM_END_MARK } from "../../common";
7
7
  import { serializeErrors } from "./utils";
@@ -5,7 +5,7 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
5
5
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
6
6
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
7
7
  import React, { useContext, useMemo } from "react";
8
- import { Link as RouterLink, matchRoutes, useResolvedPath, useHref, useMatches, NavLink as RouterNavLink } from "react-router-dom";
8
+ import { Link as RouterLink, matchRoutes, useResolvedPath, useHref, useMatches, NavLink as RouterNavLink } from "@modern-js/utils/runtime/router";
9
9
  import { RuntimeReactContext } from "../../core";
10
10
  export function composeEventHandlers(theirHandler, ourHandler) {
11
11
  return function(event) {
@@ -1,4 +1,4 @@
1
- import { useRouteLoaderData as useRouteData } from "react-router-dom";
1
+ import { useRouteLoaderData as useRouteData } from "@modern-js/utils/runtime/router";
2
2
  import { routerPlugin } from "./plugin";
3
3
  export default routerPlugin;
4
4
  export { modifyRoutes } from "./plugin";
@@ -61,6 +61,8 @@ export {
61
61
  resolvePath,
62
62
  createPath,
63
63
  unstable_useBlocker,
64
- unstable_usePrompt
65
- } from "react-router-dom";
66
- export { defer, json, redirect } from "@modern-js/utils/runtime/router";
64
+ unstable_usePrompt,
65
+ defer,
66
+ json,
67
+ redirect
68
+ } from "@modern-js/utils/runtime/router";
@@ -1,7 +1,7 @@
1
1
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
2
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
- import { createBrowserRouter, createHashRouter, RouterProvider, createRoutesFromElements, useMatches, useLocation } from "react-router-dom";
4
+ import { createBrowserRouter, createHashRouter, RouterProvider, createRoutesFromElements, useMatches, useLocation } from "@modern-js/utils/runtime/router";
5
5
  import hoistNonReactStatics from "hoist-non-react-statics";
6
6
  import { parsedJSONFromElement } from "@modern-js/utils/runtime-browser";
7
7
  import { modifyRoutes as modifyRoutesHook } from "./hooks";
@@ -6,10 +6,10 @@ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
7
  import { jsx as _jsx } from "react/jsx-runtime";
8
8
  import { useContext } from "react";
9
- import { createStaticHandler } from "@modern-js/utils/runtime/router";
10
- import { createStaticRouter, StaticRouterProvider } from "react-router-dom/server";
9
+ import { createStaticHandler } from "@modern-js/utils/runtime/remix-router";
10
+ import { createStaticRouter, StaticRouterProvider } from "@modern-js/utils/runtime-node/router";
11
11
  import hoistNonReactStatics from "hoist-non-react-statics";
12
- import { createRoutesFromElements } from "react-router-dom";
12
+ import { createRoutesFromElements } from "@modern-js/utils/runtime/router";
13
13
  import { RuntimeReactContext } from "../../core";
14
14
  import { renderRoutes, urlJoin } from "./utils";
15
15
  import { installGlobals } from "./fetch";
@@ -1 +1 @@
1
- export * from "react-router-dom/server";
1
+ export * from "@modern-js/utils/runtime-node/router";
@@ -4,8 +4,9 @@ import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
4
4
  import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
5
5
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
6
  import { jsx as _jsx } from "react/jsx-runtime";
7
- import { Route } from "react-router-dom";
8
- import { ErrorResponse, isRouteErrorResponse, renderNestedRoute } from "@modern-js/utils/runtime/router";
7
+ import { Route, isRouteErrorResponse } from "@modern-js/utils/runtime/router";
8
+ import { ErrorResponse } from "@modern-js/utils/runtime/remix-router";
9
+ import { renderNestedRoute } from "@modern-js/utils/runtime/nested-routes";
9
10
  import { DefaultNotFound } from "./DefaultNotFound";
10
11
  import DeferredDataScripts from "./DeferredDataScripts";
11
12
  export function getRouteComponents(routes, param) {
@@ -1,7 +1,7 @@
1
1
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
2
  import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
3
3
  import { jsx as _jsx } from "react/jsx-runtime";
4
- import { useLocation, useNavigate, useParams } from "react-router-dom";
4
+ import { useLocation, useNavigate, useParams } from "@modern-js/utils/runtime/router";
5
5
  export var withRouter = function(Component) {
6
6
  return function(props) {
7
7
  var location = useLocation();
@@ -77,7 +77,7 @@ export default function() {
77
77
  source: {
78
78
  alias: {
79
79
  // ensure that all packages use the same storage in @modern-js/utils/runtime-node
80
- "@modern-js/utils/runtime-node": require.resolve("@modern-js/utils/runtime-node"),
80
+ "@modern-js/utils/runtime-node$": require.resolve("@modern-js/utils/runtime-node"),
81
81
  "@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
82
82
  },
83
83
  globalVars: function(values, param) {
@@ -1,5 +1,6 @@
1
1
  import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
2
2
  import { serializeJson } from "@modern-js/utils/runtime-node";
3
+ import { attributesToString } from "../utils";
3
4
  import { buildTemplate } from "./buildTemplate.share";
4
5
  export function buildShellAfterTemplate(afterAppTemplate, options) {
5
6
  var injectSSRDataScript = function injectSSRDataScript2(template) {
@@ -7,7 +8,7 @@ export function buildShellAfterTemplate(afterAppTemplate, options) {
7
8
  return template.replace("<!--<?- SSRDataScript ?>-->", ssrDataScript);
8
9
  function buildSSRDataScript() {
9
10
  var _options_context = options.context, ssrContext = _options_context.ssrContext, initialData = _options_context.initialData, __i18nData__ = _options_context.__i18nData__, renderLevel = options.renderLevel;
10
- var request = ssrContext.request, enableUnsafeCtx = ssrContext.enableUnsafeCtx;
11
+ var request = ssrContext.request, enableUnsafeCtx = ssrContext.enableUnsafeCtx, nonce = ssrContext.nonce;
11
12
  var unsafeContext = {
12
13
  headers: request.headers
13
14
  };
@@ -27,7 +28,10 @@ export function buildShellAfterTemplate(afterAppTemplate, options) {
27
28
  },
28
29
  renderLevel: renderLevel
29
30
  };
30
- return "\n <script>window._SSR_DATA = ".concat(serializeJson(SSRData), "</script>\n ");
31
+ var attrsStr = attributesToString({
32
+ nonce: nonce
33
+ });
34
+ return "\n <script".concat(attrsStr, ">window._SSR_DATA = ").concat(serializeJson(SSRData), "</script>\n ");
31
35
  }
32
36
  };
33
37
  var callbacks = [
@@ -1,7 +1,7 @@
1
1
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
2
2
  import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
3
3
  import ReactHelmet from "react-helmet";
4
- import { matchRoutes } from "react-router-dom";
4
+ import { matchRoutes } from "@modern-js/utils/runtime/router";
5
5
  import helmetReplace from "../helmet";
6
6
  import { CSS_CHUNKS_PLACEHOLDER } from "../utils";
7
7
  import { HEAD_REG_EXP, buildTemplate } from "./buildTemplate.share";
@@ -7,13 +7,13 @@ import { ESCAPED_SHELL_STREAM_END_MARK } from "../../../common";
7
7
  import { getTemplates } from "./template";
8
8
  var ShellChunkStatus;
9
9
  (function(ShellChunkStatus2) {
10
- ShellChunkStatus2[ShellChunkStatus2["IDLE"] = 0] = "IDLE";
11
- ShellChunkStatus2[ShellChunkStatus2["START"] = 1] = "START";
12
- ShellChunkStatus2[ShellChunkStatus2["FINIESH"] = 2] = "FINIESH";
10
+ ShellChunkStatus2[ShellChunkStatus2["START"] = 0] = "START";
11
+ ShellChunkStatus2[ShellChunkStatus2["FINIESH"] = 1] = "FINIESH";
13
12
  })(ShellChunkStatus || (ShellChunkStatus = {}));
14
13
  function renderToPipe(rootElement, context, options) {
15
- var shellChunkStatus = ShellChunkStatus.IDLE;
14
+ var shellChunkStatus = ShellChunkStatus.START;
16
15
  var ssrContext = context.ssrContext;
16
+ var chunkVec = [];
17
17
  var forUserPipe = function(stream) {
18
18
  return new Promise(function(resolve) {
19
19
  var renderToPipeableStream;
@@ -22,6 +22,7 @@ function renderToPipe(rootElement, context, options) {
22
22
  } catch (e) {
23
23
  }
24
24
  var pipe = renderToPipeableStream(rootElement, _object_spread_props(_object_spread({}, options), {
25
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
25
26
  onShellReady: function onShellReady() {
26
27
  var _options_onShellReady;
27
28
  var _getTemplates = getTemplates(context, RenderLevel.SERVER_RENDER), shellAfter = _getTemplates.shellAfter, shellBefore = _getTemplates.shellBefore;
@@ -30,16 +31,13 @@ function renderToPipe(rootElement, context, options) {
30
31
  transform: function transform(chunk, _encoding, callback) {
31
32
  try {
32
33
  if (shellChunkStatus !== ShellChunkStatus.FINIESH) {
33
- var concatedChunk = chunk.toString();
34
- if (shellChunkStatus === ShellChunkStatus.IDLE) {
35
- concatedChunk = "".concat(shellBefore).concat(concatedChunk);
36
- shellChunkStatus = ShellChunkStatus.START;
37
- }
38
- if (shellChunkStatus === ShellChunkStatus.START && concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
39
- concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, shellAfter);
34
+ chunkVec.push(chunk.toString());
35
+ var concatedChunk = chunkVec.join("");
36
+ if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
37
+ concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
40
38
  shellChunkStatus = ShellChunkStatus.FINIESH;
39
+ this.push("".concat(shellBefore).concat(concatedChunk).concat(shellAfter));
41
40
  }
42
- this.push(concatedChunk);
43
41
  } else {
44
42
  this.push(chunk);
45
43
  }
@@ -7,12 +7,12 @@ import { ESCAPED_SHELL_STREAM_END_MARK } from "../../../common";
7
7
  import { getTemplates } from "./template";
8
8
  var ShellChunkStatus;
9
9
  (function(ShellChunkStatus2) {
10
- ShellChunkStatus2[ShellChunkStatus2["IDLE"] = 0] = "IDLE";
11
- ShellChunkStatus2[ShellChunkStatus2["START"] = 1] = "START";
12
- ShellChunkStatus2[ShellChunkStatus2["FINIESH"] = 2] = "FINIESH";
10
+ ShellChunkStatus2[ShellChunkStatus2["START"] = 0] = "START";
11
+ ShellChunkStatus2[ShellChunkStatus2["FINIESH"] = 1] = "FINIESH";
13
12
  })(ShellChunkStatus || (ShellChunkStatus = {}));
14
13
  function renderToPipe(rootElement, context, options) {
15
- var shellChunkStatus = ShellChunkStatus.IDLE;
14
+ var shellChunkStatus = ShellChunkStatus.START;
15
+ var chunkVec = [];
16
16
  var ssrContext = context.ssrContext;
17
17
  var forUserPipe = function() {
18
18
  var _ref = _async_to_generator(function() {
@@ -36,6 +36,7 @@ function renderToPipe(rootElement, context, options) {
36
36
  return [
37
37
  4,
38
38
  renderToReadableStream(rootElement, _object_spread_props(_object_spread({}, options), {
39
+ nonce: ssrContext === null || ssrContext === void 0 ? void 0 : ssrContext.nonce,
39
40
  onError: function onError(error) {
40
41
  var _options_onError;
41
42
  ssrContext.logger.error("An error occurs during streaming SSR", error);
@@ -54,7 +55,7 @@ function renderToPipe(rootElement, context, options) {
54
55
  }
55
56
  function _push() {
56
57
  _push = _async_to_generator(function() {
57
- var _ref2, done, value, concatedChunk;
58
+ var _ref2, done, value, chunk, concatedChunk;
58
59
  return _ts_generator(this, function(_state2) {
59
60
  switch (_state2.label) {
60
61
  case 0:
@@ -71,14 +72,13 @@ function renderToPipe(rootElement, context, options) {
71
72
  ];
72
73
  }
73
74
  if (shellChunkStatus !== ShellChunkStatus.FINIESH) {
74
- concatedChunk = new TextDecoder().decode(value);
75
- if (shellChunkStatus === ShellChunkStatus.IDLE) {
76
- concatedChunk = "".concat(shellBefore).concat(concatedChunk);
77
- shellChunkStatus = ShellChunkStatus.START;
78
- }
79
- if (shellChunkStatus === ShellChunkStatus.START && concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
80
- concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, shellAfter);
75
+ chunk = new TextDecoder().decode(value);
76
+ chunkVec.push(chunk);
77
+ concatedChunk = chunkVec.join("");
78
+ if (concatedChunk.endsWith(ESCAPED_SHELL_STREAM_END_MARK)) {
79
+ concatedChunk = concatedChunk.replace(ESCAPED_SHELL_STREAM_END_MARK, "");
81
80
  shellChunkStatus = ShellChunkStatus.FINIESH;
81
+ controller.enqueue(encodeForWebStream("".concat(shellBefore).concat(concatedChunk).concat(shellAfter)));
82
82
  }
83
83
  controller.enqueue(encodeForWebStream(concatedChunk));
84
84
  } else {
@@ -13,7 +13,7 @@ import helmetReplace from "../helmet";
13
13
  import { RenderLevel } from "../types";
14
14
  import { time } from "../time";
15
15
  import prefetch from "../../prefetch";
16
- import { ROUTER_DATA_JSON_ID, SSR_DATA_JSON_ID } from "../utils";
16
+ import { ROUTER_DATA_JSON_ID, SSR_DATA_JSON_ID, attributesToString } from "../utils";
17
17
  import { toFragments } from "./template";
18
18
  import { reduce } from "./reduce";
19
19
  import * as loadableRenderer from "./loadable";
@@ -49,8 +49,9 @@ var Entry = /* @__PURE__ */ function() {
49
49
  _define_property(this, "fragments", void 0);
50
50
  _define_property(this, "pluginConfig", void 0);
51
51
  _define_property(this, "host", void 0);
52
+ _define_property(this, "nonce", void 0);
52
53
  var ctx = options.ctx, config = options.config;
53
- var entryName = ctx.entryName, template = ctx.template, host = ctx.request.host;
54
+ var entryName = ctx.entryName, template = ctx.template, host = ctx.request.host, nonce = ctx.nonce;
54
55
  this.fragments = toFragments(template, entryName);
55
56
  this.entryName = entryName;
56
57
  this.host = host;
@@ -58,6 +59,7 @@ var Entry = /* @__PURE__ */ function() {
58
59
  this.pluginConfig = config;
59
60
  this.metrics = ctx.metrics;
60
61
  this.logger = ctx.logger;
62
+ this.nonce = nonce;
61
63
  this.result = {
62
64
  renderLevel: RenderLevel.CLIENT_RENDER,
63
65
  html: "",
@@ -215,7 +217,8 @@ var Entry = /* @__PURE__ */ function() {
215
217
  host: this.host,
216
218
  result: this.result,
217
219
  entryName: this.entryName,
218
- config: this.pluginConfig
220
+ config: this.pluginConfig,
221
+ nonce: this.nonce
219
222
  };
220
223
  html = reduce(App, renderContext, [
221
224
  styledComponentRenderer.toHtml,
@@ -240,10 +243,13 @@ var Entry = /* @__PURE__ */ function() {
240
243
  value: function getSSRDataScript(templateData, routerData) {
241
244
  var useInlineScript = this.pluginConfig.inlineScript !== false;
242
245
  var ssrData = serializeJson(templateData);
243
- var ssrDataScripts = useInlineScript ? "<script>window._SSR_DATA = ".concat(ssrData, "</script>") : '<script type="application/json" id="'.concat(SSR_DATA_JSON_ID, '">').concat(ssrData, "</script>");
246
+ var attrsStr = attributesToString({
247
+ nonce: this.nonce
248
+ });
249
+ var ssrDataScripts = useInlineScript ? "<script".concat(attrsStr, ">window._SSR_DATA = ").concat(ssrData, "</script>") : '<script type="application/json" id="'.concat(SSR_DATA_JSON_ID, '">').concat(ssrData, "</script>");
244
250
  if (routerData) {
245
251
  var serializedRouterData = serializeJson(routerData);
246
- ssrDataScripts += useInlineScript ? "\n<script>window._ROUTER_DATA = ".concat(serializedRouterData, "</script>") : '\n<script type="application/json" id="'.concat(ROUTER_DATA_JSON_ID, '">').concat(serializedRouterData, "</script>");
252
+ ssrDataScripts += useInlineScript ? "\n<script".concat(attrsStr, ">window._ROUTER_DATA = ").concat(serializedRouterData, "</script>") : '\n<script type="application/json" id="'.concat(ROUTER_DATA_JSON_ID, '">').concat(serializedRouterData, "</script>");
247
253
  }
248
254
  return {
249
255
  SSRDataScript: ssrDataScripts
@@ -1,6 +1,6 @@
1
1
  import { ChunkExtractor } from "@loadable/server";
2
2
  import { isCrossOrigin } from "../../utils";
3
- import { getLoadableScripts } from "../utils";
3
+ import { attributesToString, getLoadableScripts } from "../utils";
4
4
  var extname = function(uri) {
5
5
  if (typeof uri !== "string" || !uri.includes(".")) {
6
6
  return "";
@@ -8,7 +8,7 @@ var extname = function(uri) {
8
8
  return ".".concat(uri === null || uri === void 0 ? void 0 : uri.split(".").pop()) || "";
9
9
  };
10
10
  export var toHtml = function(jsx, renderer, next) {
11
- var stats = renderer.stats, chunksMap = renderer.result.chunksMap, host = renderer.host, _renderer_config = renderer.config, config = _renderer_config === void 0 ? {} : _renderer_config;
11
+ var stats = renderer.stats, chunksMap = renderer.result.chunksMap, host = renderer.host, _renderer_config = renderer.config, config = _renderer_config === void 0 ? {} : _renderer_config, nonce = renderer.nonce;
12
12
  if (!stats || chunksMap.js) {
13
13
  return next(jsx);
14
14
  }
@@ -27,12 +27,15 @@ export var toHtml = function(jsx, renderer, next) {
27
27
  var v = _step.value;
28
28
  var fileType = extname(v.url).slice(1);
29
29
  if (fileType === "js") {
30
- var props = [];
30
+ var attributes = {
31
+ nonce: nonce
32
+ };
31
33
  var crossorigin = config.crossorigin;
32
34
  if (crossorigin && isCrossOrigin(v.url, host)) {
33
- props.push('crossorigin="'.concat(crossorigin === true ? "anonymous" : crossorigin, '"'));
35
+ attributes.crossorigin = crossorigin === true ? "anonymous" : crossorigin;
34
36
  }
35
- chunksMap[fileType] += '<script src="'.concat(v.url, '" ').concat(props.join(" "), "></script>");
37
+ var attrsStr = attributesToString(attributes);
38
+ chunksMap[fileType] += "<script".concat(attrsStr, ' src="').concat(v.url, '"></script>');
36
39
  } else if (fileType === "css") {
37
40
  chunksMap[fileType] += '<link href="'.concat(v.url, '" rel="stylesheet" />');
38
41
  }
@@ -1,3 +1,4 @@
1
+ import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
1
2
  export var CSS_CHUNKS_PLACEHOLDER = "<!--<?- chunksMap.css ?>-->";
2
3
  export var SSR_DATA_JSON_ID = "__MODERN_SSR_DATA__";
3
4
  export var ROUTER_DATA_JSON_ID = "__MODERN_ROUTER_DATA__";
@@ -13,3 +14,9 @@ export function getLoadableScripts(extractor) {
13
14
  return "".concat(i, "</script>");
14
15
  }).join("");
15
16
  }
17
+ export function attributesToString(attributes) {
18
+ return Object.entries(attributes).reduce(function(str, param) {
19
+ var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
20
+ return value === void 0 ? str : "".concat(str, " ").concat(key, '="').concat(value, '"');
21
+ }, "");
22
+ }
@@ -46,8 +46,9 @@ export default () => {
46
46
  return null;
47
47
  }
48
48
  return async ({ htmlWebpackPlugin }) => {
49
+ const config = api.useResolvedConfigContext();
49
50
  const documentParams = getDocParams({
50
- config: api.useResolvedConfigContext(),
51
+ config,
51
52
  entryName,
52
53
  templateParameters
53
54
  });
@@ -121,7 +122,9 @@ export default () => {
121
122
  htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName !== "script" && item.tagName !== "link").join("")
122
123
  ].join("");
123
124
  if (html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_START) && html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_END)) {
124
- html = html.replace(new RegExp(`${DOCUMENT_SCRIPT_PLACEHOLDER_START}(.*?)${DOCUMENT_SCRIPT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `<script>${decodeURIComponent($1)}</script>`);
125
+ const { nonce } = config.security;
126
+ const nonceAttr = nonce ? `nonce=${nonce}` : "";
127
+ html = html.replace(new RegExp(`${DOCUMENT_SCRIPT_PLACEHOLDER_START}(.*?)${DOCUMENT_SCRIPT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `<script ${nonceAttr}>${decodeURIComponent($1)}</script>`);
125
128
  }
126
129
  if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
127
130
  html = html.replace(new RegExp(`${DOCUMENT_COMMENT_PLACEHOLDER_START}(.*?)${DOCUMENT_COMMENT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `${decodeURIComponent($1)}`);
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { Suspense, useEffect, useRef, useMemo, useContext } from "react";
3
- import { Await, UNSAFE_DataRouterContext as DataRouterContext, useAsyncError } from "react-router-dom";
3
+ import { Await, UNSAFE_DataRouterContext as DataRouterContext, useAsyncError } from "@modern-js/utils/runtime/router";
4
4
  import { serializeJson } from "@modern-js/utils/runtime-node";
5
5
  import { JSX_SHELL_STREAM_END_MARK } from "../../common";
6
6
  import { serializeErrors } from "./utils";
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import React, { useContext, useMemo } from "react";
3
- import { Link as RouterLink, matchRoutes, useResolvedPath, useHref, useMatches, NavLink as RouterNavLink } from "react-router-dom";
3
+ import { Link as RouterLink, matchRoutes, useResolvedPath, useHref, useMatches, NavLink as RouterNavLink } from "@modern-js/utils/runtime/router";
4
4
  import { RuntimeReactContext } from "../../core";
5
5
  export function composeEventHandlers(theirHandler, ourHandler) {
6
6
  return (event) => {
@@ -1,4 +1,4 @@
1
- import { useRouteLoaderData as useRouteData } from "react-router-dom";
1
+ import { useRouteLoaderData as useRouteData } from "@modern-js/utils/runtime/router";
2
2
  import { routerPlugin } from "./plugin";
3
3
  export default routerPlugin;
4
4
  export { modifyRoutes } from "./plugin";
@@ -61,6 +61,8 @@ export {
61
61
  resolvePath,
62
62
  createPath,
63
63
  unstable_useBlocker,
64
- unstable_usePrompt
65
- } from "react-router-dom";
66
- export { defer, json, redirect } from "@modern-js/utils/runtime/router";
64
+ unstable_usePrompt,
65
+ defer,
66
+ json,
67
+ redirect
68
+ } from "@modern-js/utils/runtime/router";
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { createBrowserRouter, createHashRouter, RouterProvider, createRoutesFromElements, useMatches, useLocation } from "react-router-dom";
2
+ import { createBrowserRouter, createHashRouter, RouterProvider, createRoutesFromElements, useMatches, useLocation } from "@modern-js/utils/runtime/router";
3
3
  import hoistNonReactStatics from "hoist-non-react-statics";
4
4
  import { parsedJSONFromElement } from "@modern-js/utils/runtime-browser";
5
5
  import { modifyRoutes as modifyRoutesHook } from "./hooks";
@@ -1,9 +1,9 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useContext } from "react";
3
- import { createStaticHandler } from "@modern-js/utils/runtime/router";
4
- import { createStaticRouter, StaticRouterProvider } from "react-router-dom/server";
3
+ import { createStaticHandler } from "@modern-js/utils/runtime/remix-router";
4
+ import { createStaticRouter, StaticRouterProvider } from "@modern-js/utils/runtime-node/router";
5
5
  import hoistNonReactStatics from "hoist-non-react-statics";
6
- import { createRoutesFromElements } from "react-router-dom";
6
+ import { createRoutesFromElements } from "@modern-js/utils/runtime/router";
7
7
  import { RuntimeReactContext } from "../../core";
8
8
  import { renderRoutes, urlJoin } from "./utils";
9
9
  import { installGlobals } from "./fetch";
@@ -1 +1 @@
1
- export * from "react-router-dom/server";
1
+ export * from "@modern-js/utils/runtime-node/router";
@@ -1,6 +1,7 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { Route } from "react-router-dom";
3
- import { ErrorResponse, isRouteErrorResponse, renderNestedRoute } from "@modern-js/utils/runtime/router";
2
+ import { Route, isRouteErrorResponse } from "@modern-js/utils/runtime/router";
3
+ import { ErrorResponse } from "@modern-js/utils/runtime/remix-router";
4
+ import { renderNestedRoute } from "@modern-js/utils/runtime/nested-routes";
4
5
  import { DefaultNotFound } from "./DefaultNotFound";
5
6
  import DeferredDataScripts from "./DeferredDataScripts";
6
7
  export function getRouteComponents(routes, { globalApp, ssrMode, props }) {
@@ -1,5 +1,5 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useLocation, useNavigate, useParams } from "react-router-dom";
2
+ import { useLocation, useNavigate, useParams } from "@modern-js/utils/runtime/router";
3
3
  export const withRouter = (Component) => {
4
4
  return (props) => {
5
5
  const location = useLocation();
@@ -58,7 +58,7 @@ export default () => {
58
58
  source: {
59
59
  alias: {
60
60
  // ensure that all packages use the same storage in @modern-js/utils/runtime-node
61
- "@modern-js/utils/runtime-node": require.resolve("@modern-js/utils/runtime-node"),
61
+ "@modern-js/utils/runtime-node$": require.resolve("@modern-js/utils/runtime-node"),
62
62
  "@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
63
63
  },
64
64
  globalVars: (values, { target }) => {
@@ -1,4 +1,5 @@
1
1
  import { serializeJson } from "@modern-js/utils/runtime-node";
2
+ import { attributesToString } from "../utils";
2
3
  import { buildTemplate } from "./buildTemplate.share";
3
4
  export function buildShellAfterTemplate(afterAppTemplate, options) {
4
5
  const callbacks = [
@@ -10,7 +11,7 @@ export function buildShellAfterTemplate(afterAppTemplate, options) {
10
11
  return template.replace("<!--<?- SSRDataScript ?>-->", ssrDataScript);
11
12
  function buildSSRDataScript() {
12
13
  const { context: { ssrContext, initialData, __i18nData__ }, renderLevel } = options;
13
- const { request, enableUnsafeCtx } = ssrContext;
14
+ const { request, enableUnsafeCtx, nonce } = ssrContext;
14
15
  const unsafeContext = {
15
16
  headers: request.headers
16
17
  };
@@ -31,8 +32,11 @@ export function buildShellAfterTemplate(afterAppTemplate, options) {
31
32
  },
32
33
  renderLevel
33
34
  };
35
+ const attrsStr = attributesToString({
36
+ nonce
37
+ });
34
38
  return `
35
- <script>window._SSR_DATA = ${serializeJson(SSRData)}</script>
39
+ <script${attrsStr}>window._SSR_DATA = ${serializeJson(SSRData)}</script>
36
40
  `;
37
41
  }
38
42
  }