@modern-js/app-tools 2.0.0-beta.2 → 2.0.0-beta.3

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 (60) hide show
  1. package/CHANGELOG.md +251 -0
  2. package/bin/modern.js +10 -1
  3. package/dist/js/modern/analyze/constants.js +2 -0
  4. package/dist/js/modern/analyze/generateCode.js +38 -18
  5. package/dist/js/modern/analyze/getServerRoutes.js +3 -3
  6. package/dist/js/modern/analyze/nestedRoutes.js +17 -6
  7. package/dist/js/modern/analyze/templates.js +59 -27
  8. package/dist/js/modern/analyze/utils.js +30 -4
  9. package/dist/js/modern/builder/builderPlugins/compatModern.js +6 -1
  10. package/dist/js/modern/builder/index.js +3 -5
  11. package/dist/js/modern/builder/loaders/routerLoader.js +20 -0
  12. package/dist/js/modern/builder/loaders/serverModuleLoader.js +4 -0
  13. package/dist/js/modern/commands/dev.js +23 -27
  14. package/dist/js/modern/commands/start.js +0 -1
  15. package/dist/js/modern/config/default.js +0 -1
  16. package/dist/js/modern/config/initial/createOutputConfig.js +3 -0
  17. package/dist/js/modern/utils/createFileWatcher.js +1 -1
  18. package/dist/js/modern/utils/restart.js +1 -1
  19. package/dist/js/node/analyze/constants.js +5 -1
  20. package/dist/js/node/analyze/generateCode.js +36 -16
  21. package/dist/js/node/analyze/getServerRoutes.js +3 -3
  22. package/dist/js/node/analyze/nestedRoutes.js +15 -4
  23. package/dist/js/node/analyze/templates.js +60 -27
  24. package/dist/js/node/analyze/utils.js +31 -3
  25. package/dist/js/node/builder/builderPlugins/compatModern.js +6 -1
  26. package/dist/js/node/builder/index.js +3 -5
  27. package/dist/js/node/builder/loaders/routerLoader.js +27 -0
  28. package/dist/js/node/builder/loaders/serverModuleLoader.js +11 -0
  29. package/dist/js/node/commands/dev.js +23 -27
  30. package/dist/js/node/commands/start.js +0 -1
  31. package/dist/js/node/config/default.js +0 -1
  32. package/dist/js/node/config/initial/createOutputConfig.js +3 -0
  33. package/dist/js/node/utils/createFileWatcher.js +2 -1
  34. package/dist/js/node/utils/restart.js +1 -1
  35. package/dist/js/treeshaking/analyze/constants.js +2 -0
  36. package/dist/js/treeshaking/analyze/generateCode.js +137 -81
  37. package/dist/js/treeshaking/analyze/getServerRoutes.js +4 -3
  38. package/dist/js/treeshaking/analyze/nestedRoutes.js +98 -55
  39. package/dist/js/treeshaking/analyze/templates.js +175 -101
  40. package/dist/js/treeshaking/analyze/utils.js +80 -4
  41. package/dist/js/treeshaking/builder/builderPlugins/compatModern.js +6 -1
  42. package/dist/js/treeshaking/builder/index.js +3 -5
  43. package/dist/js/treeshaking/builder/loaders/routerLoader.js +14 -0
  44. package/dist/js/treeshaking/builder/loaders/serverModuleLoader.js +4 -0
  45. package/dist/js/treeshaking/commands/dev.js +37 -38
  46. package/dist/js/treeshaking/commands/start.js +0 -1
  47. package/dist/js/treeshaking/config/default.js +0 -1
  48. package/dist/js/treeshaking/config/initial/createOutputConfig.js +3 -0
  49. package/dist/js/treeshaking/utils/createFileWatcher.js +1 -1
  50. package/dist/js/treeshaking/utils/restart.js +1 -1
  51. package/dist/types/analyze/constants.d.ts +2 -0
  52. package/dist/types/analyze/templates.d.ts +8 -7
  53. package/dist/types/analyze/utils.d.ts +9 -1
  54. package/dist/types/builder/loaders/routerLoader.d.ts +3 -0
  55. package/dist/types/builder/loaders/serverModuleLoader.d.ts +3 -0
  56. package/dist/types/types/config/tools.d.ts +8 -1
  57. package/dist/types/types/hooks.d.ts +2 -1
  58. package/dist/types/utils/createFileWatcher.d.ts +2 -1
  59. package/lib/types.d.ts +1 -1
  60. package/package.json +25 -22
package/CHANGELOG.md CHANGED
@@ -1,5 +1,256 @@
1
1
  # @modern-js/app-tools
2
2
 
3
+ ## 2.0.0-beta.3
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Minor Changes
10
+
11
+ - edd1cfb1af: feat: modernjs Access builder compiler
12
+ feat: modernjs 接入 builder 构建
13
+ - b710adb: feat: extract the data loader
14
+ feat: 提取 data loader
15
+ - bbe4c4a: feat: add @modern-js/plugin-swc
16
+
17
+ feat: 新增 @modern-js/plugin-swc 插件
18
+
19
+ - e4558a0: feat:
20
+
21
+ 1. add `runBin` function
22
+ 2. config internal plugins constants in the app/module/doc tools
23
+ 3. add app/module/doc tools internal plugins
24
+
25
+ feat:
26
+
27
+ 1. 添加 `runBin` 函数
28
+ 2. 在 app/module/doc tools 里配置内部插件
29
+ 3. 增加 app/module/doc tools 使用的插件常量
30
+
31
+ - 543be9558e: feat: compile server loader and support handle loader request
32
+ feat: 编译 server loader 并支持处理 loader 的请求
33
+
34
+ ### Patch Changes
35
+
36
+ - c9f912ca4d: feat(app-tools): improve build logs of dev and build command
37
+
38
+ feat(app-tools): 优化 dev 和 build 过程中的日志展示
39
+
40
+ - 0078da4: fix: remove webpack oneof rule in new config, save in legacy mode.
41
+ fix: 在新模式下删除 webpack oneof 规则,兼容模式下保留
42
+ - 103973cde9: fix: builder tools.webpackChain config args not match the Modernjs tools.webpackChain
43
+ fix: builder tools.webpackChain 配置传参无法匹配 Modernjs tools.webpackChain
44
+ - d4e8e6f: fix: modernjs dev server can't start normaly
45
+ fix: modernjs dev 服务端不能正常启动
46
+ - 0b2d1ef02b: fix: repeat register `babel-plugin-lodash`
47
+ fix: 重复注册 `babel-plugin-lodash`
48
+ - 82cef85ed7: fix: specify builder compiler framework
49
+ fix: 指明 builder 构建时框架
50
+ - 3e57f2bd58: feat: add document feature with plugin
51
+
52
+ feat: 增加 document 功能插件
53
+
54
+ - 85edee888c: feat(app-tools): support tools.htmlPlugin config
55
+
56
+ feat(app-tools): 支持 tools.htmlPlugin 配置项
57
+
58
+ - 2e60319: fix: some optimizations for router and loader
59
+ fix: 一些 router 和 loader 的优化
60
+ q
61
+ - a55b965: fix: rename "loader routes" file to avoid influence ssr
62
+ fix: 重命名 loader routes 避免影响 ssr
63
+ - 5402fdb0ca: feat(Builder): add output.disableTsChecker config
64
+
65
+ feat(Builder): 新增 output.disableTsChecker 配置项
66
+
67
+ - dc8eeb9cbb: fix: clear distDirectory in prepare hook & inject data loader plugin to server
68
+ fix: 在 prepare hook 中清理 dist 目录,并且向 server 中注入 data loader plugin
69
+ - cc971eabfc: refactor: move server plugin load logic in `@modern-js/core`
70
+ refactor:移除在 `@modern-js/core` 中的 server 插件加载逻辑
71
+ - 5b9049f2e9: feat: inject async js chunk when streaming ssr
72
+ feat: streaming ssr 时, 注入 async 类型的 js chunk
73
+ - d4a456659b: chore: rename plugin-jarvis to plugin-lint
74
+
75
+ chore: 重命名 plugin-jarvis 为 plugin-lint
76
+
77
+ - 6bda14ed71: feat: refactor router with react-router@6.4
78
+
79
+ feat: 使用 react-router@6.4 重构路由模块
80
+
81
+ - d36c6ee126: fix(app-tools): failed to run inspect command
82
+
83
+ fix(app-tools): 修复运行 inspect 命令失败的问题
84
+
85
+ - 92004d1906: feat: support load chunks parallelly
86
+ feat: 支持并行加载 chunks
87
+ - b8bbe036c7: feat: change type logic
88
+ feat: 修改类型相关的逻辑
89
+ - 40ed5874c6: feat: inject css chunk into html for streaming ssr
90
+ feat: streaming ssr 返回的 html 注入 css chunk
91
+ - af4422d67f: feat(builder): complete utils of tools.webpack
92
+
93
+ feat(builder): 补全 tools.webpack 提供的 utils 方法
94
+
95
+ - 87c1ff86b9: feat(app-tools): attach builder instance to appContext
96
+
97
+ feat(app-tools): 将 builder 实例挂载到 appContext 上
98
+
99
+ - c258e34202: fix: add builder hooks `beforeBuild` params
100
+ fix: 新增 builder hooks `beforeBuild` 的参数
101
+ - 3b3d709: fix(app-tools): cli --analyze option not work
102
+
103
+ fix(app-tools): 修复 --analyze 命令行参数不生效的问题
104
+
105
+ - 8b8e1bb571: feat: support nested routes
106
+ feat: 支持嵌套路由
107
+ - 88eb147: fix(app-tools): builder's onBeforeCreateDevServer hook not work
108
+
109
+ fix(app-tools): 修复 builder 的 onBeforeCreateDevServer hook 无法触发的问题
110
+
111
+ - 8c32dc4: fix: builder should not be checked when apiOnly is true
112
+ fix: 当 apiOnly 为 true 时,builder 不应该被校验
113
+ - a2c8cc3: fix: change tools define userconfig type
114
+ fix: 修改工程定义的 UserConfig 类型
115
+ - b7a96c3: fix(app-tools): loose CLI init options after restart
116
+
117
+ fix(app-tools): 修复重启 CLI 后丢失 init options 的问题
118
+
119
+ - 7de97ae24f: fix: `deploy` command has't load `builder` instance
120
+ fix: `deploy` 命令没有加载 builder 实例
121
+ - cce8ece: fix: handle some `TODO` & `FIXME`, change some tests
122
+ fix: 处理一些 `TODO` 和 `FIXME`, 修改了一些 tests
123
+ - c3b7de4bfb: fix(app-tools): dev.assetPrefix not work
124
+
125
+ fix(app-tools): 修复 dev.assetPrefix 配置项不生效的问题
126
+
127
+ - 16a3441: fix(app-tools): remove duplicated port log
128
+
129
+ fix(app-tools): 修复 port 重复的日志输出两遍的问题
130
+
131
+ - 92004d1906: fix: use loadable lazy instead of loadable
132
+ fix: 使用 loadable lazy 组件替代 loadable
133
+ - c677befc22: fix(app-tools): compat legacy resolve behavior
134
+
135
+ fix(app-tools): 兼容旧版本 node_modules 解析逻辑
136
+
137
+ - 3f7cde5caa: fix: builder plugin setup can't get config
138
+ fix: builder 插件在 setup 阶段无法拿到 config
139
+ - 99213e4bae: fix: process does't exit when exec command
140
+ fix: 修复执行命令时进程未退出的问题
141
+ - b16fd964da: fix: `modern-js/app-tools` pass error config to builder.
142
+ fix: `modern-js/app-tools` 传递错误的 config 给 builder.
143
+ - 7eefedd7ca: fix: add html-webpack-plugin `__internal__` options, for bottom template
144
+ fix: 为了 bottom template, 增加 `html-webpack-plugin` `__internal__` 配置项,
145
+ - 14b712da84: fix: use consistent alias type and default value across packages
146
+
147
+ fix: 在各个包中使用一致的 alias 类型定义和默认值
148
+
149
+ - Updated dependencies [c9f912ca4d]
150
+ - Updated dependencies [95be7cc49c]
151
+ - Updated dependencies [e439457a51]
152
+ - Updated dependencies [4d1545f8c0]
153
+ - Updated dependencies [2bc090c089]
154
+ - Updated dependencies [f0abb2e]
155
+ - Updated dependencies [f96a725211]
156
+ - Updated dependencies [828f42f9ce]
157
+ - Updated dependencies [060abd4]
158
+ - Updated dependencies [309cd71]
159
+ - Updated dependencies [c7456864a8]
160
+ - Updated dependencies [c9e800d39a]
161
+ - Updated dependencies [d4e8e6f]
162
+ - Updated dependencies [0ff846fb56]
163
+ - Updated dependencies [3cf9633]
164
+ - Updated dependencies [6604f1b]
165
+ - Updated dependencies [57077b2c64]
166
+ - Updated dependencies [d032d49e09]
167
+ - Updated dependencies [6aca875]
168
+ - Updated dependencies [2ff6167be0]
169
+ - Updated dependencies [287f298990]
170
+ - Updated dependencies [15bf09d9c8]
171
+ - Updated dependencies [423188db70]
172
+ - Updated dependencies [fd2d652c03]
173
+ - Updated dependencies [0c2d8dae31]
174
+ - Updated dependencies [2edad29dd7]
175
+ - Updated dependencies [85edee888c]
176
+ - Updated dependencies [2e60319]
177
+ - Updated dependencies [a2509bfbdb]
178
+ - Updated dependencies [3998875791]
179
+ - Updated dependencies [ab3924a]
180
+ - Updated dependencies [3998875791]
181
+ - Updated dependencies [ba86b8b711]
182
+ - Updated dependencies [61f21d1e77]
183
+ - Updated dependencies [5402fdb0ca]
184
+ - Updated dependencies [2ae58176fe]
185
+ - Updated dependencies [92f0eade39]
186
+ - Updated dependencies [edd1cfb1af]
187
+ - Updated dependencies [5d67c26cdb]
188
+ - Updated dependencies [cc971eabfc]
189
+ - Updated dependencies [5b9049f2e9]
190
+ - Updated dependencies [a3af050]
191
+ - Updated dependencies [d4a456659b]
192
+ - Updated dependencies [18360a38d7]
193
+ - Updated dependencies [6bda14ed71]
194
+ - Updated dependencies [0b314e6946]
195
+ - Updated dependencies [92004d1906]
196
+ - Updated dependencies [b8bbe036c7]
197
+ - Updated dependencies [5911154]
198
+ - Updated dependencies [40ed5874c6]
199
+ - Updated dependencies [af4422d67f]
200
+ - Updated dependencies [705adc1]
201
+ - Updated dependencies [f680410]
202
+ - Updated dependencies [87c1ff86b9]
203
+ - Updated dependencies [d5a31df781]
204
+ - Updated dependencies [dda38c9c3e]
205
+ - Updated dependencies [102d32e4ba]
206
+ - Updated dependencies [c258e34202]
207
+ - Updated dependencies [812913ccdd]
208
+ - Updated dependencies [7248342e4d]
209
+ - Updated dependencies [568eab1e42]
210
+ - Updated dependencies [8b8e1bb571]
211
+ - Updated dependencies [3bbea92b2a]
212
+ - Updated dependencies [ae71096d45]
213
+ - Updated dependencies [73cd29dd9f]
214
+ - Updated dependencies [b710adb]
215
+ - Updated dependencies [b7a96c3]
216
+ - Updated dependencies [a23010138d]
217
+ - Updated dependencies [75d1b2657c]
218
+ - Updated dependencies [cce8ece]
219
+ - Updated dependencies [18aaf42249]
220
+ - Updated dependencies [f179749375]
221
+ - Updated dependencies [3fae2d03b3]
222
+ - Updated dependencies [ea7cf06]
223
+ - Updated dependencies [8a6d45f105]
224
+ - Updated dependencies [bbe4c4a]
225
+ - Updated dependencies [ebbeed1ece]
226
+ - Updated dependencies [90e2879520]
227
+ - Updated dependencies [e4558a0]
228
+ - Updated dependencies [df41d71ade]
229
+ - Updated dependencies [f727e5c6cc]
230
+ - Updated dependencies [5e3cecd523]
231
+ - Updated dependencies [abf3421a75]
232
+ - Updated dependencies [da2d1fc3c2]
233
+ - Updated dependencies [543be9558e]
234
+ - Updated dependencies [fd1d9fd]
235
+ - Updated dependencies [14b712da84]
236
+ - @modern-js/builder-webpack-provider@2.0.0-beta.3
237
+ - @modern-js/builder-shared@2.0.0-beta.3
238
+ - @modern-js/core@2.0.0-beta.3
239
+ - @modern-js/prod-server@2.0.0-beta.3
240
+ - @modern-js/server@2.0.0-beta.3
241
+ - @modern-js/builder-plugin-esbuild@2.0.0-beta.3
242
+ - @modern-js/plugin-data-loader@2.0.0-beta.3
243
+ - @modern-js/builder-plugin-node-polyfill@2.0.0-beta.3
244
+ - @modern-js/utils@2.0.0-beta.3
245
+ - @modern-js/types@2.0.0-beta.3
246
+ - @modern-js/node-bundle-require@2.0.0-beta.3
247
+ - @modern-js/plugin-lint@2.0.0-beta.3
248
+ - @modern-js/plugin@2.0.0-beta.3
249
+ - @modern-js/builder@2.0.0-beta.3
250
+ - @modern-js/plugin-i18n@2.0.0-beta.3
251
+ - @modern-js/new-action@2.0.0-beta.3
252
+ - @modern-js/upgrade@2.0.0-beta.3
253
+
3
254
  ## 2.0.0-beta.2
4
255
 
5
256
  ### Major Changes
package/bin/modern.js CHANGED
@@ -1,3 +1,12 @@
1
1
  #!/usr/bin/env node
2
+ const {
3
+ INTERNAL_APP_TOOLS_PLUGINS,
4
+ INTERNAL_SERVER_PLUGINS,
5
+ } = require('@modern-js/utils');
2
6
 
3
- require('@modern-js/core/bin');
7
+ require('@modern-js/core/runBin').run({
8
+ internalPlugins: {
9
+ cli: INTERNAL_APP_TOOLS_PLUGINS,
10
+ server: INTERNAL_SERVER_PLUGINS,
11
+ },
12
+ });
@@ -4,6 +4,8 @@ export const APP_FILE_NAME = 'App';
4
4
  export const PAGES_DIR_NAME = 'pages';
5
5
  export const NESTED_ROUTES_DIR = 'routes';
6
6
  export const FILE_SYSTEM_ROUTES_FILE_NAME = 'routes.js';
7
+ export const LOADER_EXPORT_NAME = 'loader';
8
+ export const TEMP_LOADERS_DIR = '__loaders__';
7
9
  export const ENTRY_POINT_FILE_NAME = 'index.js';
8
10
  export const ENTRY_BOOTSTRAP_FILE_NAME = 'bootstrap.js';
9
11
  export const FILE_SYSTEM_ROUTES_DYNAMIC_REGEXP = /^\[(\S+)\]([*+?]?)$/;
@@ -1,13 +1,18 @@
1
1
  import path from 'path';
2
- import { fs, logger } from '@modern-js/utils';
2
+ import { fs, LOADER_ROUTES_DIR, logger } from '@modern-js/utils';
3
3
  import { useResolvedConfigContext } from '@modern-js/core';
4
4
  import esbuild from 'esbuild';
5
5
  import { getCommand } from "../utils/commands";
6
6
  import * as templates from "./templates";
7
7
  import { getClientRoutes, getClientRoutesLegacy } from "./getClientRoutes";
8
- import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME } from "./constants";
8
+ import { FILE_SYSTEM_ROUTES_FILE_NAME, ENTRY_POINT_FILE_NAME, ENTRY_BOOTSTRAP_FILE_NAME, TEMP_LOADERS_DIR } from "./constants";
9
9
  import { getDefaultImports } from "./utils";
10
10
  import { walk } from "./nestedRoutes";
11
+ const loader = {
12
+ '.js': 'jsx',
13
+ '.ts': 'tsx'
14
+ };
15
+ const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
11
16
  const createImportSpecifier = specifiers => {
12
17
  let defaults = '';
13
18
  const named = [];
@@ -63,15 +68,10 @@ export const createImportStatements = statements => {
63
68
  }) => `import ${createImportSpecifier(specifiers)} from '${value}';\n${initialize || ''}`).join('\n');
64
69
  };
65
70
  const buildLoader = async (entry, outfile) => {
66
- const loader = {
67
- '.js': 'jsx',
68
- '.ts': 'tsx'
69
- };
70
- const EXTERNAL_REGEXP = /^[^./]|^\.[^./]|^\.\.[^/]/;
71
71
  const command = getCommand();
72
72
  await esbuild.build({
73
- format: 'cjs',
74
- platform: 'node',
73
+ format: 'esm',
74
+ platform: 'browser',
75
75
  target: 'esnext',
76
76
  loader,
77
77
  watch: command === 'dev' && {},
@@ -100,6 +100,20 @@ const buildLoader = async (entry, outfile) => {
100
100
  }]
101
101
  });
102
102
  };
103
+ const buildServerLoader = async (entry, outfile) => {
104
+ const command = getCommand();
105
+ await esbuild.build({
106
+ format: 'cjs',
107
+ platform: 'node',
108
+ target: 'esnext',
109
+ loader,
110
+ watch: command === 'dev' && {},
111
+ bundle: true,
112
+ logLevel: 'error',
113
+ entryPoints: [entry],
114
+ outfile
115
+ });
116
+ };
103
117
  export const generateCode = async (appContext, config, entrypoints, api) => {
104
118
  var _config$runtime, _config$runtime$route;
105
119
  const {
@@ -178,28 +192,34 @@ export const generateCode = async (appContext, config, entrypoints, api) => {
178
192
  code
179
193
  } = await hookRunners.beforeGenerateRoutes({
180
194
  entrypoint,
181
- code: templates.fileSystemRoutes({
195
+ code: await templates.fileSystemRoutes({
182
196
  routes,
183
197
  ssrMode: mode,
184
198
  nestedRoutesEntry: entrypoint.nestedRoutesEntry,
185
- entryName: entrypoint.entryName
199
+ entryName: entrypoint.entryName,
200
+ internalDirectory
186
201
  })
187
202
  });
188
203
 
189
204
  // extract nested router loaders
190
205
  if (entrypoint.nestedRoutesEntry) {
191
- const routesServerFile = path.join(internalDirectory, entryName, 'routes.server.js');
192
- const outputRoutesServerFile = path.join(distDirectory, 'loader-routes', entryName, 'index.js');
206
+ const routesServerFile = path.join(internalDirectory, entryName, 'route-server-loaders.js');
207
+ const outputRoutesServerFile = path.join(distDirectory, LOADER_ROUTES_DIR, entryName, 'index.js');
193
208
  const code = templates.routesForServer({
194
209
  routes: routes,
195
- alias: {
196
- name: internalSrcAlias,
197
- basename: srcDirectory
198
- }
210
+ internalDirectory,
211
+ entryName
199
212
  });
200
213
  await fs.ensureFile(routesServerFile);
201
214
  await fs.writeFile(routesServerFile, code);
202
- await buildLoader(routesServerFile, outputRoutesServerFile);
215
+ const loaderEntryFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'entry.js');
216
+ const loaderIndexFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'index.js');
217
+ if (await fs.pathExists(loaderEntryFile)) {
218
+ await buildLoader(loaderEntryFile, loaderIndexFile);
219
+ }
220
+ if (await fs.pathExists(routesServerFile)) {
221
+ await buildServerLoader(routesServerFile, outputRoutesServerFile);
222
+ }
203
223
  }
204
224
  fs.outputFileSync(path.resolve(internalDirectory, `./${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`), code, 'utf8');
205
225
  }
@@ -100,7 +100,8 @@ const collectHtmlRoutes = (entrypoints, appContext, config) => {
100
100
  output: {
101
101
  distPath: {
102
102
  html: htmlPath
103
- } = {}
103
+ } = {},
104
+ enableModernMode
104
105
  },
105
106
  server: {
106
107
  baseUrl,
@@ -127,8 +128,7 @@ const collectHtmlRoutes = (entrypoints, appContext, config) => {
127
128
  isSPA: true,
128
129
  isSSR,
129
130
  responseHeaders: resHeaders,
130
- // FIXME: remove the config.enableModernMode
131
- // enableModernMode: Boolean(enableModernMode),
131
+ enableModernMode: Boolean(enableModernMode),
132
132
  bundle: isSSR ? `${SERVER_BUNDLE_DIRECTORY}/${entryName}.js` : undefined
133
133
  };
134
134
  if (routes !== null && routes !== void 0 && routes.hasOwnProperty(entryName)) {
@@ -2,15 +2,21 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
2
2
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
3
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
4
  import * as path from 'path';
5
- import { fs, getRouteId } from '@modern-js/utils';
5
+ import { fs, getRouteId, normalizeToPosixPath } from '@modern-js/utils';
6
6
  import { JS_EXTENSIONS } from "./constants";
7
- import { replaceWithAlias } from "./utils";
7
+ import { hasLoader, replaceWithAlias } from "./utils";
8
8
  const LAYOUT_FILE = 'layout';
9
9
  const PAGE_FILE = 'page';
10
10
  const LOADING_FILE = 'loading';
11
11
  const ERROR_FILE = 'error';
12
12
  const LOADER_FILE = 'loader';
13
13
  const conventionNames = [LAYOUT_FILE, PAGE_FILE, LOADING_FILE, ERROR_FILE, LOADER_FILE];
14
+ const getLoaderPath = async filename => {
15
+ if (await hasLoader(filename)) {
16
+ return normalizeToPosixPath(filename);
17
+ }
18
+ return undefined;
19
+ };
14
20
  const replaceDynamicPath = routePath => {
15
21
  return routePath.replace(/\[(.*?)\]/g, ':$1');
16
22
  };
@@ -48,7 +54,8 @@ export const walk = async (dirname, rootDir, alias, entryName) => {
48
54
  routePath = replaceDynamicPath(routePath);
49
55
  const route = {
50
56
  path: routePath,
51
- children: []
57
+ children: [],
58
+ isRoot
52
59
  };
53
60
  const items = await fs.readdir(dirname);
54
61
  for (const item of items) {
@@ -68,17 +75,21 @@ export const walk = async (dirname, rootDir, alias, entryName) => {
68
75
  }
69
76
  if (itemWithoutExt === LAYOUT_FILE) {
70
77
  route._component = replaceWithAlias(alias.basename, itemPath, alias.name);
78
+ route.loader = await getLoaderPath(itemPath);
71
79
  }
72
80
  if (itemWithoutExt === PAGE_FILE) {
73
81
  var _route$children2;
74
82
  const childRoute = createIndexRoute({
75
83
  _component: replaceWithAlias(alias.basename, itemPath, alias.name)
76
84
  }, rootDir, itemPath, entryName);
85
+ childRoute.loader = await getLoaderPath(itemPath);
77
86
  (_route$children2 = route.children) === null || _route$children2 === void 0 ? void 0 : _route$children2.push(childRoute);
78
87
  }
79
- if (itemWithoutExt === LOADER_FILE) {
80
- route.loader = replaceWithAlias(alias.basename, itemPath, alias.name);
81
- }
88
+
89
+ // if (itemWithoutExt === LOADER_FILE) {
90
+ // route.loader = replaceWithAlias(alias.basename, itemPath, alias.name);
91
+ // }
92
+
82
93
  if (itemWithoutExt === LOADING_FILE) {
83
94
  route.loading = replaceWithAlias(alias.basename, itemPath, alias.name);
84
95
  }
@@ -1,6 +1,9 @@
1
1
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
2
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
3
3
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
+ import path from 'path';
5
+ import { fs } from '@modern-js/utils';
6
+ import { TEMP_LOADERS_DIR } from "./constants";
4
7
  export const index = ({
5
8
  mountId,
6
9
  imports,
@@ -78,13 +81,11 @@ export const html = partials => `
78
81
  `;
79
82
  export const routesForServer = ({
80
83
  routes,
81
- alias
84
+ internalDirectory,
85
+ entryName
82
86
  }) => {
83
- const {
84
- name,
85
- basename
86
- } = alias;
87
87
  const loaders = [];
88
+ const loaderIndexFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'index.js');
88
89
  const traverseRouteTree = route => {
89
90
  let children;
90
91
  if ('children' in route && route.children) {
@@ -116,38 +117,43 @@ export const routesForServer = ({
116
117
  }
117
118
  }
118
119
  routesCode += `\n];`;
119
- const importLoadersCode = loaders.map((loader, index) => {
120
- const realLoaderPath = loader.replace(name, basename);
121
- return `import loader_${index} from '${realLoaderPath}';\n`;
122
- }).join('');
120
+ let importLoadersCode = '';
121
+ if (loaders.length > 0) {
122
+ importLoadersCode = `
123
+ import { ${loaders.map((loader, index) => `loader_${index}`)} } from "${loaderIndexFile}"`;
124
+ }
123
125
  return `
124
126
  ${importLoadersCode}
125
127
  ${routesCode}
126
128
  `;
127
129
  };
128
- export const fileSystemRoutes = ({
130
+ export const fileSystemRoutes = async ({
129
131
  routes,
130
132
  ssrMode,
131
133
  nestedRoutesEntry,
132
- entryName
134
+ entryName,
135
+ internalDirectory
133
136
  }) => {
134
- // The legacy mode and pages dir routes should use loadable
135
- // nested routes + renderTostring should use loadable.lazy
136
- // nested routes + renderToStream should use react.lazy
137
+ const loadings = [];
138
+ const errors = [];
139
+ const loaders = [];
140
+ const loadersMap = {};
141
+ const loadersIndexFile = path.join('@_modern_js_internal', entryName, TEMP_LOADERS_DIR, 'index.js');
142
+ const loadersMapFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'map.json');
137
143
  const importLazyCode = `
138
144
  import { lazy } from "react";
139
145
  import loadable, { lazy as loadableLazy } from "@modern-js/runtime/loadable"
140
146
  `;
147
+ let rootLayoutCode = ``;
141
148
  let dataLoaderPath = '';
149
+ let componentLoaderPath = '';
142
150
  if (ssrMode) {
143
151
  dataLoaderPath = require.resolve(`@modern-js/plugin-data-loader/loader`);
144
152
  if (nestedRoutesEntry) {
145
- dataLoaderPath = `${dataLoaderPath}?routesDir=${nestedRoutesEntry}&entryName=${entryName}!`;
153
+ dataLoaderPath = `${dataLoaderPath}?routesDir=${nestedRoutesEntry}&mapFile=${loadersMapFile}!`;
146
154
  }
155
+ componentLoaderPath = `${path.join(__dirname, '../builder/loaders/routerLoader')}!`;
147
156
  }
148
- const loadings = [];
149
- const errors = [];
150
- const loaders = [];
151
157
  const traverseRouteTree = route => {
152
158
  let children;
153
159
  if ('children' in route && route.children) {
@@ -169,13 +175,19 @@ export const fileSystemRoutes = ({
169
175
  }
170
176
  if (route.loader) {
171
177
  loaders.push(route.loader);
172
- loader = `loader_${loaders.length - 1}`;
178
+ const loaderId = loaders.length - 1;
179
+ loader = `loader_${loaderId}`;
180
+ loadersMap[loader] = route.id;
173
181
  }
174
182
  if (route._component) {
175
- if (ssrMode === 'stream') {
176
- component = `lazy(() => import(/* webpackChunkName: "${route.id}" */ /* webpackMode: "lazy-once" */ '${route._component}'))`;
183
+ if (route.isRoot) {
184
+ rootLayoutCode = `import RootLayout from '${route._component}'`;
185
+ component = `RootLayout`;
186
+ } else if (ssrMode === 'string') {
187
+ component = `loadable(() => import(/* webpackChunkName: "${route.id}" */ '${componentLoaderPath}${route._component}'))`;
177
188
  } else {
178
- component = `loadable(() => import(/* webpackChunkName: "${route.id}" */ /* webpackMode: "lazy-once" */ '${route._component}'))`;
189
+ // csr and streaming
190
+ component = `lazy(() => import(/* webpackChunkName: "${route.id}" */ '${componentLoaderPath}${route._component}'))`;
179
191
  }
180
192
  }
181
193
  } else if (route._component) {
@@ -198,7 +210,7 @@ export const fileSystemRoutes = ({
198
210
  for (const route of routes) {
199
211
  if ('type' in route) {
200
212
  const newRoute = traverseRouteTree(route);
201
- routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, '$1').replace(/"(loadableLazy.*\))"/g, '$1').replace(/"(lazy.*\))"/g, '$1').replace(/"(loading_[^"])"/g, '$1').replace(/"(loader_[^"])"/g, '$1').replace(/"(error_[^"])"/g, '$1').replace(/\\"/g, '"')},`;
213
+ routeComponentsCode += `${JSON.stringify(newRoute, null, 2).replace(/"(loadable.*\))"/g, '$1').replace(/"(loadableLazy.*\))"/g, '$1').replace(/"(lazy.*\))"/g, '$1').replace(/"(loading_[^"])"/g, '$1').replace(/"(loader_[^"])"/g, '$1').replace(/"(RootLayout)"/g, '$1').replace(/"(error_[^"])"/g, '$1').replace(/\\"/g, '"')},`;
202
214
  } else {
203
215
  const component = `loadable(() => import('${route._component}'))`;
204
216
  const finalRoute = _objectSpread(_objectSpread({}, route), {}, {
@@ -214,14 +226,34 @@ export const fileSystemRoutes = ({
214
226
  const importErrorComponentsCode = errors.map((error, index) => {
215
227
  return `import error_${index} from '${error}';\n`;
216
228
  }).join('');
217
- const importLoaderComponentsCode = loaders.map((loader, index) => {
218
- return `import loader_${index} from '${dataLoaderPath}${loader}';\n`;
219
- }).join('');
229
+ let importLoadersCode = '';
230
+ if (loaders.length > 0) {
231
+ importLoadersCode = `
232
+ import { ${loaders.map((loader, index) => `loader_${index}`)} } from "${dataLoaderPath}${loadersIndexFile}"
233
+ `;
234
+ const loaderEntryCode = loaders.map((loader, index) => {
235
+ return `export * from './loader_${index}.js';`;
236
+ }).join('\n');
237
+ const loaderEntryFile = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, 'entry.js');
238
+ await fs.ensureFile(loaderEntryFile);
239
+ await fs.writeFile(loaderEntryFile, loaderEntryCode);
240
+ await fs.writeJSON(loadersMapFile, loadersMap);
241
+ await Promise.all(loaders.map(async (loader, index) => {
242
+ const name = `loader_${index}`;
243
+ const filename = path.join(internalDirectory, entryName, TEMP_LOADERS_DIR, `${name}.js`);
244
+ const code = `
245
+ export { loader as ${name} } from '${loader}'
246
+ `;
247
+ await fs.ensureFile(filename);
248
+ await fs.writeFile(filename, code);
249
+ }));
250
+ }
220
251
  return `
221
252
  ${importLazyCode}
253
+ ${rootLayoutCode}
222
254
  ${importLoadingCode}
223
255
  ${importErrorComponentsCode}
224
- ${importLoaderComponentsCode}
256
+ ${importLoadersCode}
225
257
  ${routeComponentsCode}
226
258
  `;
227
259
  };
@@ -1,7 +1,9 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
- import { isReact18, normalizeToPosixPath } from '@modern-js/utils';
4
- import { FILE_SYSTEM_ROUTES_FILE_NAME } from "./constants";
3
+ import { isReact18, normalizeToPosixPath, fs as fse } from '@modern-js/utils';
4
+ import { transform } from 'esbuild';
5
+ import { parse } from 'es-module-lexer';
6
+ import { FILE_SYSTEM_ROUTES_FILE_NAME, LOADER_EXPORT_NAME } from "./constants";
5
7
  export const walkDirectory = dir => fs.readdirSync(dir).reduce((previous, filename) => {
6
8
  const filePath = path.join(dir, filename);
7
9
  if (fs.statSync(filePath).isDirectory()) {
@@ -51,7 +53,7 @@ export const getDefaultImports = ({
51
53
  specifiers: [{
52
54
  imported: 'routes'
53
55
  }],
54
- value: normalizeToPosixPath(`${internalDirAlias}/${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME}`)
56
+ value: normalizeToPosixPath(`${internalDirAlias}/${entryName}/${FILE_SYSTEM_ROUTES_FILE_NAME.replace('.js', '')}`)
55
57
  };
56
58
  if (fileSystemRoutes.globalApp) {
57
59
  imports.push({
@@ -83,4 +85,28 @@ export const isRouteComponentFile = filePath => {
83
85
  }
84
86
  return false;
85
87
  };
86
- export const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
88
+ export const replaceWithAlias = (base, filePath, alias) => normalizeToPosixPath(path.join(alias, path.relative(base, filePath)));
89
+ export const parseModule = async ({
90
+ source,
91
+ filename
92
+ }) => {
93
+ let content = source;
94
+ if (filename.endsWith('.tsx') || filename.endsWith('.jsx')) {
95
+ const result = await transform(content, {
96
+ loader: path.extname(filename).slice(1),
97
+ format: 'esm'
98
+ });
99
+ content = result.code;
100
+ }
101
+
102
+ // eslint-disable-next-line @typescript-eslint/await-thenable
103
+ return await parse(content);
104
+ };
105
+ export const hasLoader = async filename => {
106
+ const source = await fse.readFile(filename);
107
+ const [, moduleExports] = await parseModule({
108
+ source: source.toString(),
109
+ filename
110
+ });
111
+ return moduleExports.some(e => e.n === LOADER_EXPORT_NAME);
112
+ };