@modern-js/app-tools 3.1.0 → 3.1.2

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 (57) hide show
  1. package/dist/cjs/commands/build.js +3 -9
  2. package/dist/cjs/commands/dev.js +4 -9
  3. package/dist/cjs/esm/register-esm.js +16 -12
  4. package/dist/cjs/esm/register-esm.mjs +11 -10
  5. package/dist/cjs/esm/ts-paths-loader.js +95 -0
  6. package/dist/cjs/esm/ts-paths-loader.mjs +57 -0
  7. package/dist/cjs/plugins/deploy/platforms/templates/netlify-entry.cjs +1 -1
  8. package/dist/cjs/plugins/deploy/utils/generator.js +4 -3
  9. package/dist/cjs/utils/register.js +48 -17
  10. package/dist/esm/builder/generator/createCopyPattern.mjs +1 -1
  11. package/dist/esm/builder/index.mjs +3 -3
  12. package/dist/esm/builder/shared/builderPlugins/adapterSSR.mjs +2 -2
  13. package/dist/esm/builder/shared/loaders/serverModuleLoader.mjs +1 -1
  14. package/dist/esm/commands/build.mjs +3 -9
  15. package/dist/esm/commands/dev.mjs +4 -9
  16. package/dist/esm/esm/register-esm.mjs +12 -11
  17. package/dist/esm/esm/ts-paths-loader.mjs +58 -0
  18. package/dist/esm/index.mjs +7 -6
  19. package/dist/esm/plugins/analyze/getHtmlTemplate.mjs +2 -2
  20. package/dist/esm/plugins/analyze/index.mjs +5 -5
  21. package/dist/esm/plugins/analyze/isDefaultExportFunction.mjs +2 -2
  22. package/dist/esm/plugins/deploy/index.mjs +1 -1
  23. package/dist/esm/plugins/deploy/platforms/templates/netlify-entry.cjs +1 -1
  24. package/dist/esm/plugins/deploy/utils/generator.mjs +4 -3
  25. package/dist/esm/plugins/initialize/index.mjs +1 -1
  26. package/dist/esm/plugins/serverBuild.mjs +1 -1
  27. package/dist/esm/plugins/serverRuntime.mjs +1 -1
  28. package/dist/esm/utils/config.mjs +2 -2
  29. package/dist/esm/utils/register.mjs +45 -17
  30. package/dist/esm-node/builder/generator/createCopyPattern.mjs +1 -1
  31. package/dist/esm-node/builder/index.mjs +3 -3
  32. package/dist/esm-node/builder/shared/builderPlugins/adapterBasic.mjs +2 -2
  33. package/dist/esm-node/builder/shared/builderPlugins/adapterSSR.mjs +2 -2
  34. package/dist/esm-node/builder/shared/loaders/serverModuleLoader.mjs +1 -1
  35. package/dist/esm-node/commands/build.mjs +3 -9
  36. package/dist/esm-node/commands/dev.mjs +4 -9
  37. package/dist/esm-node/esm/register-esm.mjs +12 -11
  38. package/dist/esm-node/esm/ts-paths-loader.mjs +59 -0
  39. package/dist/esm-node/index.mjs +7 -6
  40. package/dist/esm-node/plugins/analyze/getHtmlTemplate.mjs +2 -2
  41. package/dist/esm-node/plugins/analyze/index.mjs +4 -4
  42. package/dist/esm-node/plugins/analyze/isDefaultExportFunction.mjs +2 -2
  43. package/dist/esm-node/plugins/deploy/index.mjs +1 -1
  44. package/dist/esm-node/plugins/deploy/platforms/templates/netlify-entry.cjs +1 -1
  45. package/dist/esm-node/plugins/deploy/utils/generator.mjs +4 -3
  46. package/dist/esm-node/plugins/deploy/utils/index.mjs +2 -2
  47. package/dist/esm-node/plugins/initialize/index.mjs +1 -1
  48. package/dist/esm-node/plugins/serverBuild.mjs +1 -1
  49. package/dist/esm-node/plugins/serverRuntime.mjs +1 -1
  50. package/dist/esm-node/utils/config.mjs +2 -2
  51. package/dist/esm-node/utils/register.mjs +45 -17
  52. package/dist/types/esm/register-esm.d.mts +5 -0
  53. package/dist/types/esm/ts-paths-loader.d.mts +6 -0
  54. package/dist/types/plugins/deploy/platforms/templates/netlify-entry.d.cts +2 -1
  55. package/dist/types/utils/register.d.ts +8 -1
  56. package/package.json +15 -15
  57. package/{rstest.config.ts → rstest.config.mts} +0 -4
@@ -70,15 +70,9 @@ const build = async (api, options)=>{
70
70
  const hooks = api.getHooks();
71
71
  const combinedAlias = [].concat(resolvedConfig?.resolve?.alias ?? []).concat(resolvedConfig?.source?.alias ?? []);
72
72
  await (0, loadPlugins_js_namespaceObject.loadServerPlugins)(api, appContext.appDirectory, appContext.metaName);
73
- if (appContext.moduleType && 'module' === appContext.moduleType) {
74
- const { registerModuleHooks } = await import("../esm/register-esm.js");
75
- await registerModuleHooks({
76
- appDir: appContext.appDirectory,
77
- distDir: appContext.distDirectory,
78
- alias: {}
79
- });
80
- }
81
- await (0, register_js_namespaceObject.setupTsRuntime)(appContext.appDirectory, appContext.distDirectory, combinedAlias);
73
+ await (0, register_js_namespaceObject.setupTsRuntime)(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
74
+ moduleType: appContext.moduleType
75
+ });
82
76
  const { apiOnly } = appContext;
83
77
  if (apiOnly) {
84
78
  await hooks.onBeforeBuild.call({
@@ -51,15 +51,10 @@ const dev = async (api, options, devServerOptions)=>{
51
51
  const appContext = api.getAppContext();
52
52
  const hooks = api.getHooks();
53
53
  const combinedAlias = [].concat(normalizedConfig?.resolve?.alias ?? []).concat(normalizedConfig?.source?.alias ?? []);
54
- if (appContext.moduleType && 'module' === appContext.moduleType) {
55
- const { registerModuleHooks } = await import("../esm/register-esm.js");
56
- await registerModuleHooks({
57
- appDir: appContext.appDirectory,
58
- distDir: appContext.distDirectory,
59
- alias: {}
60
- });
61
- }
62
- await (0, register_js_namespaceObject.setupTsRuntime)(appContext.appDirectory, appContext.distDirectory, combinedAlias);
54
+ await (0, register_js_namespaceObject.setupTsRuntime)(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
55
+ moduleType: appContext.moduleType,
56
+ preferTsNodeForServerRuntime: true
57
+ });
63
58
  const { appDirectory, port, apiOnly, metaName, serverRoutes } = appContext;
64
59
  const meta = (0, utils_namespaceObject.getMeta)(metaName);
65
60
  const serverConfigPath = external_node_path_default().resolve(appDirectory, utils_namespaceObject.SERVER_DIR, `${meta}.server`);
@@ -27,24 +27,16 @@ var __webpack_require__ = {};
27
27
  var __webpack_exports__ = {};
28
28
  __webpack_require__.r(__webpack_exports__);
29
29
  __webpack_require__.d(__webpack_exports__, {
30
- registerModuleHooks: ()=>registerModuleHooks
30
+ registerModuleHooks: ()=>registerModuleHooks,
31
+ registerPathsLoader: ()=>registerPathsLoader
31
32
  });
32
33
  const external_node_path_namespaceObject = require("node:path");
33
34
  const utils_namespaceObject = require("@modern-js/utils");
34
- const checkDepExist = async (dep)=>{
35
- try {
36
- await import(dep);
37
- return true;
38
- } catch {
39
- return false;
40
- }
41
- };
42
35
  const registerModuleHooks = async ({ appDir, distDir, alias })=>{
43
36
  const TS_CONFIG_FILENAME = "tsconfig.json";
44
37
  const tsconfigPath = external_node_path_namespaceObject.resolve(appDir, TS_CONFIG_FILENAME);
45
38
  const hasTsconfig = await utils_namespaceObject.fs.pathExists(tsconfigPath);
46
- const hasTsNode = await checkDepExist('ts-node');
47
- if (!hasTsconfig || !hasTsNode) return;
39
+ if (!hasTsconfig) return;
48
40
  const { register } = await import("node:module");
49
41
  process.env.TS_NODE_TRANSPILE_ONLY = true;
50
42
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -60,9 +52,21 @@ const registerModuleHooks = async ({ appDir, distDir, alias })=>{
60
52
  }
61
53
  });
62
54
  };
55
+ const registerPathsLoader = async ({ appDir, baseUrl, paths })=>{
56
+ const { register } = await import("node:module");
57
+ register('./ts-paths-loader.mjs', __rslib_import_meta_url__, {
58
+ data: {
59
+ appDir,
60
+ baseUrl,
61
+ paths
62
+ }
63
+ });
64
+ };
63
65
  exports.registerModuleHooks = __webpack_exports__.registerModuleHooks;
66
+ exports.registerPathsLoader = __webpack_exports__.registerPathsLoader;
64
67
  for(var __rspack_i in __webpack_exports__)if (-1 === [
65
- "registerModuleHooks"
68
+ "registerModuleHooks",
69
+ "registerPathsLoader"
66
70
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
67
71
  Object.defineProperty(exports, '__esModule', {
68
72
  value: true
@@ -1,19 +1,10 @@
1
1
  import path from 'node:path';
2
2
  import { fs } from '@modern-js/utils';
3
- const checkDepExist = async (dep)=>{
4
- try {
5
- await import(dep);
6
- return true;
7
- } catch {
8
- return false;
9
- }
10
- };
11
3
  export const registerModuleHooks = async ({ appDir, distDir, alias })=>{
12
4
  const TS_CONFIG_FILENAME = "tsconfig.json";
13
5
  const tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
14
6
  const hasTsconfig = await fs.pathExists(tsconfigPath);
15
- const hasTsNode = await checkDepExist('ts-node');
16
- if (!hasTsconfig || !hasTsNode) return;
7
+ if (!hasTsconfig) return;
17
8
  const { register } = await import('node:module');
18
9
  process.env.TS_NODE_TRANSPILE_ONLY = true;
19
10
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -29,3 +20,13 @@ export const registerModuleHooks = async ({ appDir, distDir, alias })=>{
29
20
  }
30
21
  });
31
22
  };
23
+ export const registerPathsLoader = async ({ appDir, baseUrl, paths })=>{
24
+ const { register } = await import('node:module');
25
+ register('./ts-paths-loader.mjs', import.meta.url, {
26
+ data: {
27
+ appDir,
28
+ baseUrl,
29
+ paths
30
+ }
31
+ });
32
+ };
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ initialize: ()=>initialize,
28
+ resolve: ()=>resolve
29
+ });
30
+ const external_node_fs_namespaceObject = require("node:fs");
31
+ const external_node_path_namespaceObject = require("node:path");
32
+ const external_url_namespaceObject = require("url");
33
+ const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
34
+ let matchPath;
35
+ let appDir;
36
+ const resolvePathWithExtensions = (matchedPath)=>{
37
+ if (external_node_path_namespaceObject.extname(matchedPath)) return matchedPath;
38
+ const fileCandidates = [
39
+ matchedPath,
40
+ `${matchedPath}.ts`,
41
+ `${matchedPath}.tsx`,
42
+ `${matchedPath}.mts`,
43
+ `${matchedPath}.cts`,
44
+ `${matchedPath}.js`,
45
+ `${matchedPath}.mjs`,
46
+ `${matchedPath}.cjs`
47
+ ];
48
+ for (const candidate of fileCandidates)if (external_node_fs_namespaceObject.existsSync(candidate) && external_node_fs_namespaceObject.statSync(candidate).isFile()) return candidate;
49
+ const indexCandidates = [
50
+ external_node_path_namespaceObject.join(matchedPath, 'index.ts'),
51
+ external_node_path_namespaceObject.join(matchedPath, 'index.tsx'),
52
+ external_node_path_namespaceObject.join(matchedPath, 'index.mts'),
53
+ external_node_path_namespaceObject.join(matchedPath, 'index.cts'),
54
+ external_node_path_namespaceObject.join(matchedPath, 'index.js'),
55
+ external_node_path_namespaceObject.join(matchedPath, 'index.mjs'),
56
+ external_node_path_namespaceObject.join(matchedPath, 'index.cjs')
57
+ ];
58
+ for (const candidate of indexCandidates)if (external_node_fs_namespaceObject.existsSync(candidate) && external_node_fs_namespaceObject.statSync(candidate).isFile()) return candidate;
59
+ return matchedPath;
60
+ };
61
+ async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
62
+ appDir = external_node_path_namespaceObject.resolve(currentAppDir);
63
+ matchPath = (0, tsconfig_paths_namespaceObject.createMatchPath)(baseUrl || './', paths || {});
64
+ }
65
+ function resolve(specifier, context, defaultResolve) {
66
+ const parentPath = context.parentURL ? external_node_path_namespaceObject.dirname((0, external_url_namespaceObject.fileURLToPath)(context.parentURL)) : process.cwd();
67
+ const relativeFromApp = appDir ? external_node_path_namespaceObject.relative(appDir, parentPath) : '';
68
+ const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !external_node_path_namespaceObject.isAbsolute(relativeFromApp));
69
+ if ((specifier.startsWith('./') || specifier.startsWith('../')) && !external_node_path_namespaceObject.extname(specifier) && isAppFile) {
70
+ const resolvedPath = resolvePathWithExtensions(external_node_path_namespaceObject.resolve(parentPath, specifier));
71
+ if (resolvedPath && external_node_fs_namespaceObject.existsSync(resolvedPath)) return defaultResolve((0, external_url_namespaceObject.pathToFileURL)(resolvedPath).href, context, defaultResolve);
72
+ }
73
+ if (!matchPath) return defaultResolve(specifier, context, defaultResolve);
74
+ const match = matchPath(specifier, void 0, void 0, [
75
+ '.ts',
76
+ '.tsx',
77
+ '.mts',
78
+ '.cts',
79
+ '.js',
80
+ '.mjs',
81
+ '.cjs'
82
+ ]);
83
+ if (!match) return defaultResolve(specifier, context, defaultResolve);
84
+ const resolvedPath = resolvePathWithExtensions(match);
85
+ return defaultResolve((0, external_url_namespaceObject.pathToFileURL)(resolvedPath).href, context, defaultResolve);
86
+ }
87
+ exports.initialize = __webpack_exports__.initialize;
88
+ exports.resolve = __webpack_exports__.resolve;
89
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
90
+ "initialize",
91
+ "resolve"
92
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
93
+ Object.defineProperty(exports, '__esModule', {
94
+ value: true
95
+ });
@@ -0,0 +1,57 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath, pathToFileURL } from 'url';
4
+ import { createMatchPath as oCreateMatchPath } from '@modern-js/utils/tsconfig-paths';
5
+ let matchPath;
6
+ let appDir;
7
+ const resolvePathWithExtensions = (matchedPath)=>{
8
+ if (path.extname(matchedPath)) return matchedPath;
9
+ const fileCandidates = [
10
+ matchedPath,
11
+ `${matchedPath}.ts`,
12
+ `${matchedPath}.tsx`,
13
+ `${matchedPath}.mts`,
14
+ `${matchedPath}.cts`,
15
+ `${matchedPath}.js`,
16
+ `${matchedPath}.mjs`,
17
+ `${matchedPath}.cjs`
18
+ ];
19
+ for (const candidate of fileCandidates)if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate;
20
+ const indexCandidates = [
21
+ path.join(matchedPath, 'index.ts'),
22
+ path.join(matchedPath, 'index.tsx'),
23
+ path.join(matchedPath, 'index.mts'),
24
+ path.join(matchedPath, 'index.cts'),
25
+ path.join(matchedPath, 'index.js'),
26
+ path.join(matchedPath, 'index.mjs'),
27
+ path.join(matchedPath, 'index.cjs')
28
+ ];
29
+ for (const candidate of indexCandidates)if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) return candidate;
30
+ return matchedPath;
31
+ };
32
+ export async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
33
+ appDir = path.resolve(currentAppDir);
34
+ matchPath = oCreateMatchPath(baseUrl || './', paths || {});
35
+ }
36
+ export function resolve(specifier, context, defaultResolve) {
37
+ const parentPath = context.parentURL ? path.dirname(fileURLToPath(context.parentURL)) : process.cwd();
38
+ const relativeFromApp = appDir ? path.relative(appDir, parentPath) : '';
39
+ const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !path.isAbsolute(relativeFromApp));
40
+ if ((specifier.startsWith('./') || specifier.startsWith('../')) && !path.extname(specifier) && isAppFile) {
41
+ const resolvedPath = resolvePathWithExtensions(path.resolve(parentPath, specifier));
42
+ if (resolvedPath && fs.existsSync(resolvedPath)) return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
43
+ }
44
+ if (!matchPath) return defaultResolve(specifier, context, defaultResolve);
45
+ const match = matchPath(specifier, void 0, void 0, [
46
+ '.ts',
47
+ '.tsx',
48
+ '.mts',
49
+ '.cts',
50
+ '.js',
51
+ '.mjs',
52
+ '.cjs'
53
+ ]);
54
+ if (!match) return defaultResolve(specifier, context, defaultResolve);
55
+ const resolvedPath = resolvePathWithExtensions(match);
56
+ return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
57
+ }
@@ -54,4 +54,4 @@ const handler = async (request, context)=>{
54
54
  if (!requestHandler) await createHandler();
55
55
  return requestHandler(request, context);
56
56
  };
57
- exports.handler = handler;
57
+ module.exports.default = handler;
@@ -46,10 +46,11 @@ const lodash_namespaceObject = require("@modern-js/utils/lodash");
46
46
  const external_index_js_namespaceObject = require("./index.js");
47
47
  const serverAppContextTemplate = (appContext)=>{
48
48
  const { appDirectory, sharedDirectory, apiDirectory, lambdaDirectory, metaName, bffRuntimeFramework } = appContext;
49
+ const getRelativePathTemplate = (targetDirectory)=>`path.join(__dirname, ${JSON.stringify((0, external_index_js_namespaceObject.normalizePath)(external_node_path_default().relative(appDirectory, targetDirectory)))})`;
49
50
  return {
50
- sharedDirectory: `path.join(__dirname, "${(0, external_index_js_namespaceObject.normalizePath)(external_node_path_default().relative(appDirectory, sharedDirectory))}")`,
51
- apiDirectory: `path.join(__dirname, "${(0, external_index_js_namespaceObject.normalizePath)(external_node_path_default().relative(appDirectory, apiDirectory))}")`,
52
- lambdaDirectory: `path.join(__dirname, "${(0, external_index_js_namespaceObject.normalizePath)(external_node_path_default().relative(appDirectory, lambdaDirectory))}")`,
51
+ sharedDirectory: getRelativePathTemplate(sharedDirectory),
52
+ apiDirectory: getRelativePathTemplate(apiDirectory),
53
+ lambdaDirectory: getRelativePathTemplate(lambdaDirectory),
53
54
  metaName,
54
55
  bffRuntimeFramework: bffRuntimeFramework || 'hono'
55
56
  };
@@ -33,17 +33,28 @@ var __webpack_require__ = {};
33
33
  var __webpack_exports__ = {};
34
34
  __webpack_require__.r(__webpack_exports__);
35
35
  __webpack_require__.d(__webpack_exports__, {
36
- setupTsRuntime: ()=>setupTsRuntime
36
+ setupTsRuntime: ()=>setupTsRuntime,
37
+ resolveTsRuntimeRegisterMode: ()=>resolveTsRuntimeRegisterMode
37
38
  });
38
39
  const external_node_path_namespaceObject = require("node:path");
39
40
  var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
40
41
  const utils_namespaceObject = require("@modern-js/utils");
41
- const setupTsRuntime = async (appDir, distDir, alias)=>{
42
+ const resolveTsRuntimeRegisterMode = (hasTsNode)=>{
43
+ const hasNativeTypeScriptSupport = process.features?.typescript;
44
+ const nodeMajorVersion = Number(process.versions.node.split('.')[0]);
45
+ const supportsNativeTypeScript = void 0 === hasNativeTypeScriptSupport ? nodeMajorVersion >= 22 : false !== hasNativeTypeScriptSupport;
46
+ if (supportsNativeTypeScript) return 'node-loader';
47
+ if (hasTsNode) return 'ts-node';
48
+ return 'unsupported';
49
+ };
50
+ const setupTsRuntime = async (appDir, distDir, alias, options = {})=>{
42
51
  const TS_CONFIG_FILENAME = "tsconfig.json";
43
52
  const tsconfigPath = external_node_path_default().resolve(appDir, TS_CONFIG_FILENAME);
44
53
  const isTsProject = await utils_namespaceObject.fs.pathExists(tsconfigPath);
45
54
  const hasTsNode = (0, utils_namespaceObject.isDepExists)(appDir, 'ts-node');
46
- if (!isTsProject || !hasTsNode) return;
55
+ if (!isTsProject) return;
56
+ const preferredRegisterMode = resolveTsRuntimeRegisterMode(hasTsNode);
57
+ const registerMode = options.preferTsNodeForServerRuntime && hasTsNode ? 'ts-node' : preferredRegisterMode;
47
58
  const aliasConfig = (0, utils_namespaceObject.getAliasConfig)(alias, {
48
59
  appDirectory: appDir,
49
60
  tsconfigPath
@@ -68,28 +79,48 @@ const setupTsRuntime = async (appDir, distDir, alias)=>{
68
79
  [`${key}`]: tsPath
69
80
  };
70
81
  }, {});
71
- const tsConfig = (0, utils_namespaceObject.readTsConfigByFile)(tsconfigPath);
72
- const tsNode = await (0, utils_namespaceObject.loadFromProject)('ts-node', appDir);
73
- const tsNodeOptions = tsConfig['ts-node'];
74
- tsNode.register({
75
- project: tsconfigPath,
76
- scope: true,
77
- files: true,
78
- transpileOnly: true,
79
- ignore: [
80
- '(?:^|/)node_modules/',
81
- `(?:^|/)${external_node_path_default().relative(appDir, distDir)}/`
82
- ],
83
- ...tsNodeOptions
84
- });
82
+ if ('unsupported' === registerMode) return;
83
+ if ('ts-node' === registerMode) {
84
+ if ('module' === options.moduleType) {
85
+ const { registerModuleHooks } = await import("../esm/register-esm.js");
86
+ await registerModuleHooks({
87
+ appDir,
88
+ distDir,
89
+ alias: alias || {}
90
+ });
91
+ }
92
+ const tsConfig = (0, utils_namespaceObject.readTsConfigByFile)(tsconfigPath);
93
+ const tsNode = await (0, utils_namespaceObject.loadFromProject)('ts-node', appDir);
94
+ const tsNodeOptions = tsConfig['ts-node'];
95
+ tsNode.register({
96
+ project: tsconfigPath,
97
+ scope: true,
98
+ files: true,
99
+ transpileOnly: true,
100
+ ignore: [
101
+ '(?:^|/)node_modules/',
102
+ `(?:^|/)${external_node_path_default().relative(appDir, distDir)}/`
103
+ ],
104
+ ...tsNodeOptions
105
+ });
106
+ } else if ('node-loader' === registerMode) {
107
+ const { registerPathsLoader } = await import("../esm/register-esm.js");
108
+ await registerPathsLoader({
109
+ appDir,
110
+ baseUrl: absoluteBaseUrl || './',
111
+ paths: tsPaths
112
+ });
113
+ }
85
114
  const { register } = await import("@modern-js/utils/tsconfig-paths");
86
115
  register({
87
116
  baseUrl: absoluteBaseUrl || './',
88
117
  paths: tsPaths
89
118
  });
90
119
  };
120
+ exports.resolveTsRuntimeRegisterMode = __webpack_exports__.resolveTsRuntimeRegisterMode;
91
121
  exports.setupTsRuntime = __webpack_exports__.setupTsRuntime;
92
122
  for(var __rspack_i in __webpack_exports__)if (-1 === [
123
+ "resolveTsRuntimeRegisterMode",
93
124
  "setupTsRuntime"
94
125
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
95
126
  Object.defineProperty(exports, '__esModule', {
@@ -7,7 +7,7 @@ const info = (file)=>({
7
7
  function createPublicPattern(appContext, config, chain) {
8
8
  const { publicDir } = createCopyInfo(appContext, config);
9
9
  return {
10
- info,
10
+ info: info,
11
11
  from: '**/*',
12
12
  to: 'public',
13
13
  context: publicDir,
@@ -1,7 +1,7 @@
1
- import { parseRspackConfig } from "@modern-js/builder";
2
- import { builderPluginAdapterBasic, builderPluginAdapterHooks } from "./shared/builderPlugins/index.mjs";
3
1
  async function createBuilderGenerator() {
4
2
  const { createRspackBuilderForModern } = await import("./builder-rspack/index.mjs");
5
3
  return createRspackBuilderForModern;
6
4
  }
7
- export { builderPluginAdapterBasic, builderPluginAdapterHooks, createBuilderGenerator, parseRspackConfig };
5
+ export { parseRspackConfig } from "@modern-js/builder";
6
+ export { builderPluginAdapterBasic, builderPluginAdapterHooks } from "./shared/builderPlugins/index.mjs";
7
+ export { createBuilderGenerator };
@@ -1,9 +1,9 @@
1
- import { resolve, sep } from "path";
2
1
  import { SERVICE_WORKER_ENVIRONMENT_NAME, isHtmlDisabled } from "@modern-js/builder";
3
2
  import { fs, isUseRsc, isUseSSRBundle } from "@modern-js/utils";
4
3
  import { mergeRsbuildConfig } from "@rsbuild/core";
5
4
  import { getServerCombinedModuleFile } from "../../../plugins/analyze/utils.mjs";
6
5
  import { HtmlAsyncChunkPlugin, RouterPlugin } from "../bundlerPlugins/index.mjs";
6
+ import * as __rspack_external_path from "path";
7
7
  const builderPluginAdapterSSR = (options)=>({
8
8
  name: 'builder-plugin-adapter-modern-ssr',
9
9
  setup (api) {
@@ -110,7 +110,7 @@ function applySSRDataLoader(chain, options) {
110
110
  const { normalizedConfig, appContext } = options;
111
111
  const { appDirectory } = appContext;
112
112
  const { entriesDir = './src' } = normalizedConfig.source;
113
- const absolutePath = resolve(appDirectory, entriesDir).split(sep).join('(\\\\|/)');
113
+ const absolutePath = __rspack_external_path.resolve(appDirectory, entriesDir).split(__rspack_external_path.sep).join('(\\\\|/)');
114
114
  const reg = new RegExp(`${absolutePath}.*\\.(loader|data|data.client)\\.[t|j]sx?$`);
115
115
  chain.module.rule('ssr-data-loader').test(reg).use('data-loader').loader(require.resolve('@modern-js/plugin-data-loader/loader')).end();
116
116
  }
@@ -2,4 +2,4 @@ function loader() {
2
2
  return "module.exports = {}";
3
3
  }
4
4
  const serverModuleLoader = loader;
5
- export { serverModuleLoader as default };
5
+ export default serverModuleLoader;
@@ -32,15 +32,9 @@ const build = async (api, options)=>{
32
32
  const hooks = api.getHooks();
33
33
  const combinedAlias = [].concat(resolvedConfig?.resolve?.alias ?? []).concat(resolvedConfig?.source?.alias ?? []);
34
34
  await loadServerPlugins(api, appContext.appDirectory, appContext.metaName);
35
- if (appContext.moduleType && 'module' === appContext.moduleType) {
36
- const { registerModuleHooks } = await import("../esm/register-esm.mjs");
37
- await registerModuleHooks({
38
- appDir: appContext.appDirectory,
39
- distDir: appContext.distDirectory,
40
- alias: {}
41
- });
42
- }
43
- await setupTsRuntime(appContext.appDirectory, appContext.distDirectory, combinedAlias);
35
+ await setupTsRuntime(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
36
+ moduleType: appContext.moduleType
37
+ });
44
38
  const { apiOnly } = appContext;
45
39
  if (apiOnly) {
46
40
  await hooks.onBeforeBuild.call({
@@ -13,15 +13,10 @@ const dev = async (api, options, devServerOptions)=>{
13
13
  const appContext = api.getAppContext();
14
14
  const hooks = api.getHooks();
15
15
  const combinedAlias = [].concat(normalizedConfig?.resolve?.alias ?? []).concat(normalizedConfig?.source?.alias ?? []);
16
- if (appContext.moduleType && 'module' === appContext.moduleType) {
17
- const { registerModuleHooks } = await import("../esm/register-esm.mjs");
18
- await registerModuleHooks({
19
- appDir: appContext.appDirectory,
20
- distDir: appContext.distDirectory,
21
- alias: {}
22
- });
23
- }
24
- await setupTsRuntime(appContext.appDirectory, appContext.distDirectory, combinedAlias);
16
+ await setupTsRuntime(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
17
+ moduleType: appContext.moduleType,
18
+ preferTsNodeForServerRuntime: true
19
+ });
25
20
  const { appDirectory, port, apiOnly, metaName, serverRoutes } = appContext;
26
21
  const meta = getMeta(metaName);
27
22
  const serverConfigPath = node_path.resolve(appDirectory, SERVER_DIR, `${meta}.server`);
@@ -1,19 +1,10 @@
1
1
  import node_path from "node:path";
2
2
  import { fs } from "@modern-js/utils";
3
- const checkDepExist = async (dep)=>{
4
- try {
5
- await import(dep);
6
- return true;
7
- } catch {
8
- return false;
9
- }
10
- };
11
3
  const registerModuleHooks = async ({ appDir, distDir, alias })=>{
12
4
  const TS_CONFIG_FILENAME = "tsconfig.json";
13
5
  const tsconfigPath = node_path.resolve(appDir, TS_CONFIG_FILENAME);
14
6
  const hasTsconfig = await fs.pathExists(tsconfigPath);
15
- const hasTsNode = await checkDepExist('ts-node');
16
- if (!hasTsconfig || !hasTsNode) return;
7
+ if (!hasTsconfig) return;
17
8
  const { register } = await import("node:module");
18
9
  process.env.TS_NODE_TRANSPILE_ONLY = true;
19
10
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -29,4 +20,14 @@ const registerModuleHooks = async ({ appDir, distDir, alias })=>{
29
20
  }
30
21
  });
31
22
  };
32
- export { registerModuleHooks };
23
+ const registerPathsLoader = async ({ appDir, baseUrl, paths })=>{
24
+ const { register } = await import("node:module");
25
+ register('./ts-paths-loader.mjs', import.meta.url, {
26
+ data: {
27
+ appDir,
28
+ baseUrl,
29
+ paths
30
+ }
31
+ });
32
+ };
33
+ export { registerModuleHooks, registerPathsLoader };
@@ -0,0 +1,58 @@
1
+ import node_fs from "node:fs";
2
+ import node_path from "node:path";
3
+ import { fileURLToPath, pathToFileURL } from "url";
4
+ import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
5
+ let matchPath;
6
+ let appDir;
7
+ const resolvePathWithExtensions = (matchedPath)=>{
8
+ if (node_path.extname(matchedPath)) return matchedPath;
9
+ const fileCandidates = [
10
+ matchedPath,
11
+ `${matchedPath}.ts`,
12
+ `${matchedPath}.tsx`,
13
+ `${matchedPath}.mts`,
14
+ `${matchedPath}.cts`,
15
+ `${matchedPath}.js`,
16
+ `${matchedPath}.mjs`,
17
+ `${matchedPath}.cjs`
18
+ ];
19
+ for (const candidate of fileCandidates)if (node_fs.existsSync(candidate) && node_fs.statSync(candidate).isFile()) return candidate;
20
+ const indexCandidates = [
21
+ node_path.join(matchedPath, 'index.ts'),
22
+ node_path.join(matchedPath, 'index.tsx'),
23
+ node_path.join(matchedPath, 'index.mts'),
24
+ node_path.join(matchedPath, 'index.cts'),
25
+ node_path.join(matchedPath, 'index.js'),
26
+ node_path.join(matchedPath, 'index.mjs'),
27
+ node_path.join(matchedPath, 'index.cjs')
28
+ ];
29
+ for (const candidate of indexCandidates)if (node_fs.existsSync(candidate) && node_fs.statSync(candidate).isFile()) return candidate;
30
+ return matchedPath;
31
+ };
32
+ async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
33
+ appDir = node_path.resolve(currentAppDir);
34
+ matchPath = createMatchPath(baseUrl || './', paths || {});
35
+ }
36
+ function resolve(specifier, context, defaultResolve) {
37
+ const parentPath = context.parentURL ? node_path.dirname(fileURLToPath(context.parentURL)) : process.cwd();
38
+ const relativeFromApp = appDir ? node_path.relative(appDir, parentPath) : '';
39
+ const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !node_path.isAbsolute(relativeFromApp));
40
+ if ((specifier.startsWith('./') || specifier.startsWith('../')) && !node_path.extname(specifier) && isAppFile) {
41
+ const resolvedPath = resolvePathWithExtensions(node_path.resolve(parentPath, specifier));
42
+ if (resolvedPath && node_fs.existsSync(resolvedPath)) return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
43
+ }
44
+ if (!matchPath) return defaultResolve(specifier, context, defaultResolve);
45
+ const match = matchPath(specifier, void 0, void 0, [
46
+ '.ts',
47
+ '.tsx',
48
+ '.mts',
49
+ '.cts',
50
+ '.js',
51
+ '.mjs',
52
+ '.cjs'
53
+ ]);
54
+ if (!match) return defaultResolve(specifier, context, defaultResolve);
55
+ const resolvedPath = resolvePathWithExtensions(match);
56
+ return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
57
+ }
58
+ export { initialize, resolve };
@@ -15,11 +15,6 @@ import serverRuntime from "./plugins/serverRuntime.mjs";
15
15
  import { generateWatchFiles } from "./utils/generateWatchFiles.mjs";
16
16
  import { initAppContext } from "./utils/initAppContext.mjs";
17
17
  import { restart } from "./utils/restart.mjs";
18
- import { defineConfig } from "./defineConfig.mjs";
19
- import { dev } from "./commands/dev.mjs";
20
- import { serve } from "./commands/serve.mjs";
21
- import { resolveModernRsbuildConfig } from "./rsbuild.mjs";
22
- import { mergeConfig } from "@modern-js/plugin/cli";
23
18
  export * from "./defineConfig.mjs";
24
19
  export * from "./types/index.mjs";
25
20
  const appTools = ()=>({
@@ -120,4 +115,10 @@ const appTools = ()=>({
120
115
  }
121
116
  });
122
117
  const src = appTools;
123
- export { appTools, src as default, defineConfig, dev, generateWatchFiles, initAppContext, mergeConfig, resolveModernRsbuildConfig, serve };
118
+ export { defineConfig } from "./defineConfig.mjs";
119
+ export { dev } from "./commands/dev.mjs";
120
+ export { serve } from "./commands/serve.mjs";
121
+ export { resolveModernRsbuildConfig } from "./rsbuild.mjs";
122
+ export { mergeConfig } from "@modern-js/plugin/cli";
123
+ export default src;
124
+ export { appTools, generateWatchFiles, initAppContext };
@@ -1,7 +1,7 @@
1
1
  import path from "path";
2
2
  import { findExists, fs } from "@modern-js/utils";
3
3
  import { HTML_PARTIALS_EXTENSIONS, HTML_PARTIALS_FOLDER } from "./constants.mjs";
4
- import { html } from "./templates.mjs";
4
+ import * as __rspack_external__templates_mjs_4da4c6c8 from "./templates.mjs";
5
5
  const findPartials = (dir, entryName, position)=>{
6
6
  if (fs.existsSync(dir)) {
7
7
  const base = findExists(HTML_PARTIALS_EXTENSIONS.map((ext)=>path.resolve(dir, `${position}${ext}`)));
@@ -70,7 +70,7 @@ const getHtmlTemplate = async (entrypoints, hooks, { appContext, config })=>{
70
70
  partials: getModifyHtmlPartials(partials)
71
71
  });
72
72
  const templatePath = path.resolve(internalDirectory, entryName, 'index.html');
73
- fs.outputFileSync(templatePath, html(partials), 'utf8');
73
+ fs.outputFileSync(templatePath, __rspack_external__templates_mjs_4da4c6c8.html(partials), 'utf8');
74
74
  htmlTemplates[entryName] = templatePath.replace(/\\/g, '/');
75
75
  partialsByEntrypoint[entryName] = partials;
76
76
  const bottomTemplate = findPartials(htmlDir, name, "bottom");