@modern-js/runtime 2.26.0 → 2.28.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 (68) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/cjs/core/compatible.js +16 -17
  3. package/dist/cjs/core/loader/useLoader.js +10 -12
  4. package/dist/cjs/core/plugin.js +3 -2
  5. package/dist/cjs/document/Html.js +4 -3
  6. package/dist/cjs/document/cli/index.js +138 -138
  7. package/dist/cjs/router/cli/index.js +91 -92
  8. package/dist/cjs/router/runtime/DeferredDataScripts.node.js +4 -2
  9. package/dist/cjs/router/runtime/PrefetchLink.js +8 -4
  10. package/dist/cjs/router/runtime/plugin.js +4 -3
  11. package/dist/cjs/router/runtime/plugin.node.js +11 -4
  12. package/dist/cjs/router/runtime/utils.js +3 -3
  13. package/dist/cjs/ssr/cli/index.js +122 -123
  14. package/dist/cjs/ssr/index.js +105 -107
  15. package/dist/cjs/ssr/index.node.js +37 -38
  16. package/dist/cjs/ssr/prefetch.js +37 -39
  17. package/dist/cjs/ssr/react/prerender/util.js +4 -2
  18. package/dist/cjs/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
  19. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
  20. package/dist/cjs/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
  21. package/dist/cjs/ssr/serverRender/renderToString/loadable.js +2 -1
  22. package/dist/cjs/state/cli/index.js +70 -72
  23. package/dist/cjs/state/runtime/plugin.js +40 -42
  24. package/dist/esm/core/compatible.js +10 -9
  25. package/dist/esm/core/loader/useLoader.js +9 -9
  26. package/dist/esm/core/plugin.js +3 -2
  27. package/dist/esm/document/Html.js +4 -3
  28. package/dist/esm/document/cli/index.js +4 -3
  29. package/dist/esm/router/cli/index.js +2 -1
  30. package/dist/esm/router/runtime/DeferredDataScripts.node.js +4 -2
  31. package/dist/esm/router/runtime/PrefetchLink.js +8 -4
  32. package/dist/esm/router/runtime/plugin.js +4 -3
  33. package/dist/esm/router/runtime/plugin.node.js +11 -4
  34. package/dist/esm/router/runtime/utils.js +3 -3
  35. package/dist/esm/ssr/cli/index.js +7 -6
  36. package/dist/esm/ssr/index.js +6 -6
  37. package/dist/esm/ssr/index.node.js +2 -1
  38. package/dist/esm/ssr/prefetch.js +2 -2
  39. package/dist/esm/ssr/react/prerender/util.js +4 -2
  40. package/dist/esm/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
  41. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
  42. package/dist/esm/ssr/serverRender/renderToStream/renderToPipe.worker.js +7 -7
  43. package/dist/esm/ssr/serverRender/renderToString/loadable.js +2 -1
  44. package/dist/esm/state/runtime/plugin.js +2 -2
  45. package/dist/esm-node/core/compatible.js +16 -17
  46. package/dist/esm-node/core/loader/useLoader.js +10 -12
  47. package/dist/esm-node/core/plugin.js +3 -2
  48. package/dist/esm-node/document/Html.js +4 -3
  49. package/dist/esm-node/document/cli/index.js +138 -138
  50. package/dist/esm-node/router/cli/index.js +91 -92
  51. package/dist/esm-node/router/runtime/DeferredDataScripts.node.js +4 -2
  52. package/dist/esm-node/router/runtime/PrefetchLink.js +8 -4
  53. package/dist/esm-node/router/runtime/plugin.js +4 -3
  54. package/dist/esm-node/router/runtime/plugin.node.js +11 -4
  55. package/dist/esm-node/router/runtime/utils.js +3 -3
  56. package/dist/esm-node/ssr/cli/index.js +122 -123
  57. package/dist/esm-node/ssr/cli/loadable-bundler-plugin.js +2 -2
  58. package/dist/esm-node/ssr/index.js +105 -107
  59. package/dist/esm-node/ssr/index.node.js +37 -38
  60. package/dist/esm-node/ssr/prefetch.js +37 -39
  61. package/dist/esm-node/ssr/react/prerender/util.js +4 -2
  62. package/dist/esm-node/ssr/serverRender/renderToStream/bulidTemplate.before.js +4 -2
  63. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.js +8 -7
  64. package/dist/esm-node/ssr/serverRender/renderToStream/renderToPipe.worker.js +8 -6
  65. package/dist/esm-node/ssr/serverRender/renderToString/loadable.js +2 -1
  66. package/dist/esm-node/state/cli/index.js +70 -72
  67. package/dist/esm-node/state/runtime/plugin.js +40 -42
  68. package/package.json +13 -10
@@ -21,148 +21,148 @@ export const getDocumenByEntryName = function(entrypoints, entryName, fallbackDi
21
21
  ]);
22
22
  return docFile || void 0;
23
23
  };
24
- export const documentPlugin = () => {
25
- return {
26
- name: "@modern-js/plugin-document",
27
- pre: [
28
- "@modern-js/plugin-analyze"
29
- ],
30
- setup: async (api) => {
31
- function getDocParams(params) {
32
- const { config, templateParameters, entryName } = params;
33
- return {
34
- processEnv: process.env,
35
- config: {
36
- output: config.output
37
- },
38
- entryName,
39
- templateParams: templateParameters
40
- };
24
+ export const documentPlugin = () => ({
25
+ name: "@modern-js/plugin-document",
26
+ pre: [
27
+ "@modern-js/plugin-analyze"
28
+ ],
29
+ setup: async (api) => {
30
+ function getDocParams(params) {
31
+ const { config, templateParameters, entryName } = params;
32
+ return {
33
+ processEnv: process.env,
34
+ config: {
35
+ output: config.output
36
+ },
37
+ entryName,
38
+ templateParams: templateParameters
39
+ };
40
+ }
41
+ const documentEntry = (entryName, templateParameters) => {
42
+ const { entrypoints, internalDirectory, appDirectory } = api.useAppContext();
43
+ const documentFilePath = getDocumenByEntryName(entrypoints, entryName, appDirectory);
44
+ if (!documentFilePath) {
45
+ return null;
41
46
  }
42
- const documentEntry = (entryName, templateParameters) => {
43
- const { entrypoints, internalDirectory, appDirectory } = api.useAppContext();
44
- const documentFilePath = getDocumenByEntryName(entrypoints, entryName, appDirectory);
45
- if (!documentFilePath) {
46
- return null;
47
+ return async ({ htmlWebpackPlugin }) => {
48
+ var _tsConfig;
49
+ const config = api.useResolvedConfigContext();
50
+ const documentParams = getDocParams({
51
+ config,
52
+ entryName,
53
+ templateParameters
54
+ });
55
+ const tempTsConfigFile = path.join(internalDirectory, `./document/_tempTsconfig.json`);
56
+ const userTsConfigFilePath = path.join(appDirectory, "tsconfig.json");
57
+ let tsConfig;
58
+ try {
59
+ tsConfig = await require(userTsConfigFilePath);
60
+ } catch (err) {
61
+ tsConfig = {};
47
62
  }
48
- return async ({ htmlWebpackPlugin }) => {
49
- const config = api.useResolvedConfigContext();
50
- const documentParams = getDocParams({
51
- config,
52
- entryName,
53
- templateParameters
54
- });
55
- const tempTsConfigFile = path.join(internalDirectory, `./document/_tempTsconfig.json`);
56
- const userTsConfigFilePath = path.join(appDirectory, "tsconfig.json");
57
- let tsConfig;
58
- try {
59
- tsConfig = await require(userTsConfigFilePath);
60
- } catch (err) {
61
- tsConfig = {};
62
- }
63
- if (tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.compilerOptions) {
64
- tsConfig.compilerOptions.jsx = "react-jsx";
65
- } else {
66
- tsConfig.compilerOptions = {
67
- jsx: "react-jsx"
68
- };
69
- }
70
- fs.outputFileSync(tempTsConfigFile, JSON.stringify(tsConfig));
71
- const htmlOutputFile = path.join(internalDirectory, `./document/_${entryName}.html.js`);
72
- await build({
73
- entryPoints: [
74
- documentFilePath
75
- ],
76
- outfile: htmlOutputFile,
77
- platform: "node",
78
- // change esbuild use the rootDir tsconfig.json as default to tempTsConfigFile
79
- tsconfig: tempTsConfigFile,
80
- target: "es6",
81
- loader: {
82
- ".ts": "ts",
83
- ".tsx": "tsx"
84
- },
85
- bundle: true,
86
- plugins: [
87
- {
88
- name: "make-all-packages-external",
89
- setup(build2) {
90
- build2.onResolve({
91
- filter: /^[^./]|^\.[^./]|^\.\.[^/]/
92
- }, (args) => {
93
- let external = true;
94
- if (args.kind === "entry-point") {
95
- external = false;
96
- }
97
- return {
98
- path: args.path,
99
- external
100
- };
101
- });
102
- }
103
- }
104
- ]
105
- });
106
- delete require.cache[require.resolve(htmlOutputFile)];
107
- const Document = (await require(htmlOutputFile)).default;
108
- const HTMLElement = React.createElement(DocumentContext.Provider, {
109
- value: documentParams
110
- }, React.createElement(Document, null));
111
- let html = ReactDomServer.renderToStaticMarkup(HTMLElement);
112
- debug("entry %s's document jsx rendered html: %o", entryName, html);
113
- const scripts = [
114
- htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName === "script").join(""),
115
- htmlWebpackPlugin.tags.bodyTags.toString()
116
- ].join("");
117
- const links = [
118
- htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName === "link").join("")
119
- ].join("");
120
- const metas = [
121
- templateParameters.meta,
122
- htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName !== "script" && item.tagName !== "link").join("")
123
- ].join("");
124
- if (html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_START) && html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_END)) {
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>`);
128
- }
129
- if (html.includes(DOCUMENT_STYLE_PLACEHOLDER_START) && html.includes(DOCUMENT_STYLE_PLACEHOLDER_END)) {
130
- html = html.replace(new RegExp(`${DOCUMENT_STYLE_PLACEHOLDER_START}(.*?)${DOCUMENT_STYLE_PLACEHOLDER_END}`, "g"), (_styleStr, $1) => `<style>${decodeURIComponent($1)}</style>`);
131
- }
132
- if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
133
- html = html.replace(new RegExp(`${DOCUMENT_COMMENT_PLACEHOLDER_START}(.*?)${DOCUMENT_COMMENT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `${decodeURIComponent($1)}`);
134
- }
135
- const finalHtml = `<!DOCTYPE html>${html}`.replace(DOCUMENT_META_PLACEHOLDER, metas).replace(DOCUMENT_SSR_PLACEHOLDER, HTML_SEPARATOR).replace(DOCUMENT_SCRIPTS_PLACEHOLDER, scripts).replace(DOCUMENT_LINKS_PLACEHOLDER, links).replace(DOCUMENT_CHUNKSMAP_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP[DOCUMENT_CHUNKSMAP_PLACEHOLDER]).replace(DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP[DOCUMENT_SSRDATASCRIPT_PLACEHOLDER]);
136
- return finalHtml;
137
- };
138
- };
139
- return {
140
- config: () => {
141
- return {
142
- tools: {
143
- htmlPlugin: (options, entry) => {
144
- const hackParameters = typeof (options === null || options === void 0 ? void 0 : options.templateParameters) === "function" ? options === null || options === void 0 ? void 0 : options.templateParameters({}, {}, {}, {}) : {
145
- ...options === null || options === void 0 ? void 0 : options.templateParameters
146
- };
147
- const templateContent = documentEntry(
148
- entry.entryName,
149
- // options,
150
- hackParameters
151
- );
152
- const documentHtmlOptions = templateContent ? {
153
- templateContent,
154
- inject: false
155
- } : {};
156
- return {
157
- ...options,
158
- ...documentHtmlOptions
159
- };
63
+ if ((_tsConfig = tsConfig) === null || _tsConfig === void 0 ? void 0 : _tsConfig.compilerOptions) {
64
+ tsConfig.compilerOptions.jsx = "react-jsx";
65
+ } else {
66
+ tsConfig.compilerOptions = {
67
+ jsx: "react-jsx"
68
+ };
69
+ }
70
+ fs.outputFileSync(tempTsConfigFile, JSON.stringify(tsConfig));
71
+ const htmlOutputFile = path.join(internalDirectory, `./document/_${entryName}.html.js`);
72
+ await build({
73
+ entryPoints: [
74
+ documentFilePath
75
+ ],
76
+ outfile: htmlOutputFile,
77
+ platform: "node",
78
+ // change esbuild use the rootDir tsconfig.json as default to tempTsConfigFile
79
+ tsconfig: tempTsConfigFile,
80
+ target: "es6",
81
+ loader: {
82
+ ".ts": "ts",
83
+ ".tsx": "tsx"
84
+ },
85
+ bundle: true,
86
+ plugins: [
87
+ {
88
+ name: "make-all-packages-external",
89
+ setup(build2) {
90
+ build2.onResolve({
91
+ filter: /^[^./]|^\.[^./]|^\.\.[^/]/
92
+ }, (args) => {
93
+ let external = true;
94
+ if (args.kind === "entry-point") {
95
+ external = false;
96
+ }
97
+ return {
98
+ path: args.path,
99
+ external
100
+ };
101
+ });
160
102
  }
161
103
  }
162
- };
104
+ ]
105
+ });
106
+ delete require.cache[require.resolve(htmlOutputFile)];
107
+ const Document = (await require(htmlOutputFile)).default;
108
+ const HTMLElement = React.createElement(DocumentContext.Provider, {
109
+ value: documentParams
110
+ }, React.createElement(Document, null));
111
+ let html = ReactDomServer.renderToStaticMarkup(HTMLElement);
112
+ debug("entry %s's document jsx rendered html: %o", entryName, html);
113
+ const scripts = [
114
+ htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName === "script").join(""),
115
+ htmlWebpackPlugin.tags.bodyTags.toString()
116
+ ].join("");
117
+ const links = [
118
+ htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName === "link").join("")
119
+ ].join("");
120
+ const metas = [
121
+ templateParameters.meta,
122
+ htmlWebpackPlugin.tags.headTags.filter((item) => item.tagName !== "script" && item.tagName !== "link").join("")
123
+ ].join("");
124
+ if (html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_START) && html.includes(DOCUMENT_SCRIPT_PLACEHOLDER_END)) {
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>`);
128
+ }
129
+ if (html.includes(DOCUMENT_STYLE_PLACEHOLDER_START) && html.includes(DOCUMENT_STYLE_PLACEHOLDER_END)) {
130
+ html = html.replace(new RegExp(`${DOCUMENT_STYLE_PLACEHOLDER_START}(.*?)${DOCUMENT_STYLE_PLACEHOLDER_END}`, "g"), (_styleStr, $1) => `<style>${decodeURIComponent($1)}</style>`);
131
+ }
132
+ if (html.includes(DOCUMENT_COMMENT_PLACEHOLDER_START) && html.includes(DOCUMENT_COMMENT_PLACEHOLDER_END)) {
133
+ html = html.replace(new RegExp(`${DOCUMENT_COMMENT_PLACEHOLDER_START}(.*?)${DOCUMENT_COMMENT_PLACEHOLDER_END}`, "g"), (_scriptStr, $1) => `${decodeURIComponent($1)}`);
163
134
  }
135
+ const finalHtml = `<!DOCTYPE html>${html}`.replace(DOCUMENT_META_PLACEHOLDER, metas).replace(DOCUMENT_SSR_PLACEHOLDER, HTML_SEPARATOR).replace(DOCUMENT_SCRIPTS_PLACEHOLDER, scripts).replace(DOCUMENT_LINKS_PLACEHOLDER, links).replace(DOCUMENT_CHUNKSMAP_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP[DOCUMENT_CHUNKSMAP_PLACEHOLDER]).replace(DOCUMENT_SSRDATASCRIPT_PLACEHOLDER, PLACEHOLDER_REPLACER_MAP[DOCUMENT_SSRDATASCRIPT_PLACEHOLDER]);
136
+ return finalHtml;
164
137
  };
165
- }
166
- };
167
- };
138
+ };
139
+ return {
140
+ config: () => {
141
+ return {
142
+ tools: {
143
+ htmlPlugin: (options, entry) => {
144
+ var _options, _options1, _options2;
145
+ const hackParameters = typeof ((_options = options) === null || _options === void 0 ? void 0 : _options.templateParameters) === "function" ? (_options1 = options) === null || _options1 === void 0 ? void 0 : _options1.templateParameters({}, {}, {}, {}) : {
146
+ ...(_options2 = options) === null || _options2 === void 0 ? void 0 : _options2.templateParameters
147
+ };
148
+ const templateContent = documentEntry(
149
+ entry.entryName,
150
+ // options,
151
+ hackParameters
152
+ );
153
+ const documentHtmlOptions = templateContent ? {
154
+ templateContent,
155
+ inject: false
156
+ } : {};
157
+ return {
158
+ ...options,
159
+ ...documentHtmlOptions
160
+ };
161
+ }
162
+ }
163
+ };
164
+ }
165
+ };
166
+ }
167
+ });
168
168
  export default documentPlugin;
@@ -1,102 +1,101 @@
1
1
  import { getEntryOptions, createRuntimeExportsUtils, isRouterV5 as isV5 } from "@modern-js/utils";
2
2
  const PLUGIN_IDENTIFIER = "router";
3
3
  const ROUTES_IDENTIFIER = "routes";
4
- export const routerPlugin = () => {
5
- return {
6
- name: "@modern-js/plugin-router",
7
- required: [
8
- "@modern-js/runtime"
9
- ],
10
- setup: (api) => {
11
- const runtimeConfigMap = /* @__PURE__ */ new Map();
12
- let pluginsExportsUtils;
13
- return {
14
- config() {
15
- const appContext = api.useAppContext();
16
- pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
17
- return {
18
- source: {
19
- include: [
20
- // react-router v6 is no longer support ie 11
21
- // so we need to compile these packages to ensure the compatibility
22
- // https://github.com/remix-run/react-router/commit/f6df0697e1b2064a2b3a12e8b39577326fdd945b
23
- /node_modules\/react-router/,
24
- /node_modules\/react-router-dom/,
25
- /node_modules\/@remix-run\/router/
26
- ],
27
- alias: {
28
- "@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
29
- }
4
+ export const routerPlugin = () => ({
5
+ name: "@modern-js/plugin-router",
6
+ required: [
7
+ "@modern-js/runtime"
8
+ ],
9
+ setup: (api) => {
10
+ const runtimeConfigMap = /* @__PURE__ */ new Map();
11
+ let pluginsExportsUtils;
12
+ return {
13
+ config() {
14
+ const appContext = api.useAppContext();
15
+ pluginsExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "plugins");
16
+ return {
17
+ source: {
18
+ include: [
19
+ // react-router v6 is no longer support ie 11
20
+ // so we need to compile these packages to ensure the compatibility
21
+ // https://github.com/remix-run/react-router/commit/f6df0697e1b2064a2b3a12e8b39577326fdd945b
22
+ /node_modules\/react-router/,
23
+ /node_modules\/react-router-dom/,
24
+ /node_modules\/@remix-run\/router/
25
+ ],
26
+ alias: {
27
+ "@modern-js/runtime/plugins": pluginsExportsUtils.getPath()
30
28
  }
31
- };
32
- },
33
- validateSchema() {
34
- return [
35
- {
36
- target: "runtime.router",
37
- schema: {
38
- type: [
39
- "boolean",
40
- "object"
41
- ]
42
- }
43
- }
44
- ];
45
- },
46
- modifyEntryImports({ entrypoint, imports }) {
47
- const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
48
- const userConfig = api.useResolvedConfigContext();
49
- const { packageName } = api.useAppContext();
50
- const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
51
- runtimeConfigMap.set(entryName, runtimeConfig);
52
- if (runtimeConfig === null || runtimeConfig === void 0 ? void 0 : runtimeConfig.router) {
53
- if (!isV5(userConfig)) {
54
- imports.push({
55
- value: "@modern-js/runtime/plugins",
56
- specifiers: [
57
- {
58
- imported: PLUGIN_IDENTIFIER
59
- }
60
- ]
61
- });
62
- }
63
- } else if (fileSystemRoutes) {
64
- throw new Error(`should enable runtime.router for entry ${entryName}`);
65
29
  }
66
- return {
67
- entrypoint,
68
- imports
69
- };
70
- },
71
- modifyEntryRuntimePlugins({ entrypoint, plugins }) {
72
- const { entryName, fileSystemRoutes } = entrypoint;
73
- const { serverRoutes } = api.useAppContext();
74
- const userConfig = api.useResolvedConfigContext();
75
- const runtimeConfig = runtimeConfigMap.get(entryName);
76
- if (runtimeConfig.router && !isV5(userConfig)) {
77
- const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
78
- plugins.push({
79
- name: PLUGIN_IDENTIFIER,
80
- options: JSON.stringify({
81
- serverBase,
82
- ...runtimeConfig.router,
83
- routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
84
- }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
85
- });
30
+ };
31
+ },
32
+ validateSchema() {
33
+ return [
34
+ {
35
+ target: "runtime.router",
36
+ schema: {
37
+ type: [
38
+ "boolean",
39
+ "object"
40
+ ]
41
+ }
86
42
  }
87
- return {
88
- entrypoint,
89
- plugins
90
- };
91
- },
92
- addRuntimeExports() {
93
- const userConfig = api.useResolvedConfigContext();
43
+ ];
44
+ },
45
+ modifyEntryImports({ entrypoint, imports }) {
46
+ var _runtimeConfig;
47
+ const { entryName, isMainEntry, fileSystemRoutes } = entrypoint;
48
+ const userConfig = api.useResolvedConfigContext();
49
+ const { packageName } = api.useAppContext();
50
+ const runtimeConfig = getEntryOptions(entryName, isMainEntry, userConfig.runtime, userConfig.runtimeByEntries, packageName);
51
+ runtimeConfigMap.set(entryName, runtimeConfig);
52
+ if ((_runtimeConfig = runtimeConfig) === null || _runtimeConfig === void 0 ? void 0 : _runtimeConfig.router) {
94
53
  if (!isV5(userConfig)) {
95
- pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/runtime/router'`);
54
+ imports.push({
55
+ value: "@modern-js/runtime/plugins",
56
+ specifiers: [
57
+ {
58
+ imported: PLUGIN_IDENTIFIER
59
+ }
60
+ ]
61
+ });
96
62
  }
63
+ } else if (fileSystemRoutes) {
64
+ throw new Error(`should enable runtime.router for entry ${entryName}`);
65
+ }
66
+ return {
67
+ entrypoint,
68
+ imports
69
+ };
70
+ },
71
+ modifyEntryRuntimePlugins({ entrypoint, plugins }) {
72
+ const { entryName, fileSystemRoutes } = entrypoint;
73
+ const { serverRoutes } = api.useAppContext();
74
+ const userConfig = api.useResolvedConfigContext();
75
+ const runtimeConfig = runtimeConfigMap.get(entryName);
76
+ if (runtimeConfig.router && !isV5(userConfig)) {
77
+ const serverBase = serverRoutes.filter((route) => route.entryName === entryName).map((route) => route.urlPath).sort((a, b) => a.length - b.length > 0 ? -1 : 1);
78
+ plugins.push({
79
+ name: PLUGIN_IDENTIFIER,
80
+ options: JSON.stringify({
81
+ serverBase,
82
+ ...runtimeConfig.router,
83
+ routesConfig: fileSystemRoutes ? `{ ${ROUTES_IDENTIFIER}, globalApp: App }` : void 0
84
+ }).replace(/"routesConfig"\s*:\s*"((\S|\s)+)"/g, '"routesConfig": $1,')
85
+ });
86
+ }
87
+ return {
88
+ entrypoint,
89
+ plugins
90
+ };
91
+ },
92
+ addRuntimeExports() {
93
+ const userConfig = api.useResolvedConfigContext();
94
+ if (!isV5(userConfig)) {
95
+ pluginsExportsUtils.addExport(`export { default as router } from '@modern-js/runtime/router'`);
97
96
  }
98
- };
99
- }
100
- };
101
- };
97
+ }
98
+ };
99
+ }
100
+ });
102
101
  export default routerPlugin;
@@ -8,6 +8,7 @@ const setupFnStr = `function s(r,e){_ROUTER_DATA.r=_ROUTER_DATA.r||{},_ROUTER_DA
8
8
  const resolveFnStr = `function r(e,r,o,A){A?_ROUTER_DATA.r[e][r].reject(A):_ROUTER_DATA.r[e][r].resolve(o)};`;
9
9
  const preResolvedFnStr = `function p(e,r){return void 0!==r?Promise.reject(new Error(r.message)):Promise.resolve(e)};`;
10
10
  const DeferredDataScripts = (props) => {
11
+ var _props;
11
12
  const context = useContext(DataRouterContext);
12
13
  const { staticContext } = context || {};
13
14
  const hydratedRef = useRef(false);
@@ -35,8 +36,9 @@ const DeferredDataScripts = (props) => {
35
36
  const { deferredKeys } = deferredData;
36
37
  const deferredKeyPromiseStr = deferredKeys.map((key) => {
37
38
  if (pendingKeys.has(key)) {
39
+ var _props2;
38
40
  deferredDataScripts.push(/* @__PURE__ */ _jsx(DeferredDataScript, {
39
- nonce: props === null || props === void 0 ? void 0 : props.nonce,
41
+ nonce: (_props2 = props) === null || _props2 === void 0 ? void 0 : _props2.nonce,
40
42
  data: deferredData.data[key],
41
43
  dataKey: key,
42
44
  routeId
@@ -72,7 +74,7 @@ const DeferredDataScripts = (props) => {
72
74
  children: [
73
75
  !hydratedRef.current && /* @__PURE__ */ _jsx("script", {
74
76
  async: true,
75
- nonce: props === null || props === void 0 ? void 0 : props.nonce,
77
+ nonce: (_props = props) === null || _props === void 0 ? void 0 : _props.nonce,
76
78
  suppressHydrationWarning: true,
77
79
  dangerouslySetInnerHTML: {
78
80
  __html: deferredScripts[0]
@@ -4,7 +4,8 @@ import { Link as RouterLink, matchRoutes, useResolvedPath, useHref, useMatches,
4
4
  import { RuntimeReactContext } from "../../core";
5
5
  export function composeEventHandlers(theirHandler, ourHandler) {
6
6
  return (event) => {
7
- theirHandler === null || theirHandler === void 0 ? void 0 : theirHandler(event);
7
+ var _theirHandler;
8
+ (_theirHandler = theirHandler) === null || _theirHandler === void 0 ? void 0 : _theirHandler(event);
8
9
  if (!event.defaultPrevented) {
9
10
  ourHandler(event);
10
11
  }
@@ -70,7 +71,8 @@ async function loadRouteModule(route, routeAssets) {
70
71
  }
71
72
  try {
72
73
  await Promise.all(chunkIds.map((chunkId) => {
73
- return __webpack_chunk_load__ === null || __webpack_chunk_load__ === void 0 ? void 0 : __webpack_chunk_load__(String(chunkId));
74
+ var __webpack_chunk_load__1;
75
+ return (__webpack_chunk_load__1 = __webpack_chunk_load__) === null || __webpack_chunk_load__1 === void 0 ? void 0 : __webpack_chunk_load__1(String(chunkId));
74
76
  }));
75
77
  } catch (error) {
76
78
  console.error(error);
@@ -104,7 +106,8 @@ const PrefetchPageLinks = ({ pathname }) => {
104
106
  const { routeAssets } = routeManifest || {};
105
107
  const matches = Array.isArray(routes) ? matchRoutes(routes, pathname) : [];
106
108
  if (Array.isArray(matches) && routeAssets) {
107
- matches === null || matches === void 0 ? void 0 : matches.forEach((match) => loadRouteModule(match.route, routeAssets));
109
+ var _matches;
110
+ (_matches = matches) === null || _matches === void 0 ? void 0 : _matches.forEach((match) => loadRouteModule(match.route, routeAssets));
108
111
  }
109
112
  if (!window._SSR_DATA) {
110
113
  return null;
@@ -119,7 +122,8 @@ const PrefetchDataLinks = ({ matches, pathname, routeManifest }) => {
119
122
  const currentMatches = useMatches();
120
123
  const basename = useHref("/");
121
124
  const dataHrefs = useMemo(() => {
122
- return matches === null || matches === void 0 ? void 0 : matches.filter((match, index) => {
125
+ var _matches;
126
+ return (_matches = matches) === null || _matches === void 0 ? void 0 : _matches.filter((match, index) => {
123
127
  if (!match.route.loader || typeof match.route.loader !== "function" || match.route.loader.length === 0) {
124
128
  return false;
125
129
  }
@@ -46,6 +46,7 @@ export const routerPlugin = ({ serverBase = [], supportHtml5History = true, base
46
46
  });
47
47
  },
48
48
  hoc: ({ App }, next) => {
49
+ var _routesConfig;
49
50
  if (!finalRouteConfig && !createRoutes) {
50
51
  return next({
51
52
  App
@@ -53,7 +54,7 @@ export const routerPlugin = ({ serverBase = [], supportHtml5History = true, base
53
54
  }
54
55
  const getRouteApp = () => {
55
56
  return (props) => {
56
- var _window__SERVER_DATA;
57
+ var _window__SERVER_DATA, _hydrationData;
57
58
  beforeCreateRouter = false;
58
59
  routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
59
60
  routesConfig: finalRouteConfig,
@@ -64,7 +65,7 @@ export const routerPlugin = ({ serverBase = [], supportHtml5History = true, base
64
65
  const baseUrl = ((_window__SERVER_DATA = window._SERVER_DATA) === null || _window__SERVER_DATA === void 0 ? void 0 : _window__SERVER_DATA.router.baseUrl) || select(location.pathname);
65
66
  const _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
66
67
  let hydrationData = window._ROUTER_DATA;
67
- if (hydrationData === null || hydrationData === void 0 ? void 0 : hydrationData.errors) {
68
+ if ((_hydrationData = hydrationData) === null || _hydrationData === void 0 ? void 0 : _hydrationData.errors) {
68
69
  hydrationData = {
69
70
  ...hydrationData,
70
71
  errors: deserializeErrors(hydrationData.errors)
@@ -109,7 +110,7 @@ export const routerPlugin = ({ serverBase = [], supportHtml5History = true, base
109
110
  if (App) {
110
111
  RouteApp = hoistNonReactStatics(RouteApp, App);
111
112
  }
112
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
113
+ if ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp) {
113
114
  return next({
114
115
  App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
115
116
  });
@@ -52,13 +52,15 @@ export const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
52
52
  const { request, mode: ssrMode, nonce } = context.ssrContext;
53
53
  const baseUrl = request.baseUrl;
54
54
  const _basename = baseUrl === "/" ? urlJoin(baseUrl, basename) : baseUrl;
55
- const routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
55
+ let routes = createRoutes ? createRoutes() : createRoutesFromElements(renderRoutes({
56
56
  routesConfig,
57
57
  ssrMode,
58
58
  props: {
59
59
  nonce
60
60
  }
61
61
  }));
62
+ const runner = api.useHookRunners();
63
+ routes = runner.modifyRoutes(routes);
62
64
  const { query } = createStaticHandler(routes, {
63
65
  basename: _basename
64
66
  });
@@ -72,13 +74,12 @@ export const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
72
74
  context.routerContext = routerContext;
73
75
  context.routes = routes;
74
76
  context.routeManifest = context.ssrContext.routeManifest;
75
- const runner = api.useHookRunners();
76
- runner.modifyRoutes(routes);
77
77
  return next({
78
78
  context
79
79
  });
80
80
  },
81
81
  hoc: ({ App }, next) => {
82
+ var _routesConfig;
82
83
  if (!routesConfig) {
83
84
  return next({
84
85
  App
@@ -98,7 +99,7 @@ export const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
98
99
  };
99
100
  };
100
101
  const RouteApp = getRouteApp();
101
- if (routesConfig === null || routesConfig === void 0 ? void 0 : routesConfig.globalApp) {
102
+ if ((_routesConfig = routesConfig) === null || _routesConfig === void 0 ? void 0 : _routesConfig.globalApp) {
102
103
  return next({
103
104
  App: hoistNonReactStatics(RouteApp, routesConfig.globalApp)
104
105
  });
@@ -109,6 +110,12 @@ export const routerPlugin = ({ basename = "", routesConfig, createRoutes }) => {
109
110
  },
110
111
  pickContext: ({ context, pickedContext }, next) => {
111
112
  const { remixRouter } = context;
113
+ if (!remixRouter) {
114
+ return next({
115
+ context,
116
+ pickedContext
117
+ });
118
+ }
112
119
  const router = {
113
120
  navigate: remixRouter.navigate,
114
121
  get location() {
@@ -57,9 +57,9 @@ export function renderRoutes({ routesConfig, props, ssrMode }) {
57
57
  return routeElements;
58
58
  }
59
59
  export function getLocation(serverContext) {
60
- var _url_replace;
61
- const { pathname, url } = (serverContext === null || serverContext === void 0 ? void 0 : serverContext.request) || {};
62
- const cleanUrl = (_url_replace = url === null || url === void 0 ? void 0 : url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
60
+ var _serverContext, _url_replace, _url;
61
+ const { pathname, url } = ((_serverContext = serverContext) === null || _serverContext === void 0 ? void 0 : _serverContext.request) || {};
62
+ const cleanUrl = (_url = url) === null || _url === void 0 ? void 0 : (_url_replace = _url.replace("http://", "")) === null || _url_replace === void 0 ? void 0 : _url_replace.replace("https://", "");
63
63
  const index = (cleanUrl || "").indexOf(pathname);
64
64
  if (index === -1) {
65
65
  return pathname;