@modern-js/app-tools 3.1.3 → 3.1.4

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 (33) hide show
  1. package/dist/cjs/commands/dev.js +1 -2
  2. package/dist/cjs/esm/register-esm.js +3 -8
  3. package/dist/cjs/esm/register-esm.mjs +3 -8
  4. package/dist/cjs/esm/ts-node-loader.js +6 -9
  5. package/dist/cjs/esm/ts-node-loader.mjs +6 -9
  6. package/dist/cjs/esm/ts-paths-loader.js +14 -36
  7. package/dist/cjs/esm/ts-paths-loader.mjs +14 -36
  8. package/dist/cjs/plugins/analyze/utils.js +25 -8
  9. package/dist/cjs/plugins/deploy/utils/generator.js +1 -1
  10. package/dist/cjs/utils/register.js +62 -29
  11. package/dist/esm/commands/dev.mjs +1 -2
  12. package/dist/esm/esm/register-esm.mjs +3 -8
  13. package/dist/esm/esm/ts-node-loader.mjs +6 -9
  14. package/dist/esm/esm/ts-paths-loader.mjs +14 -36
  15. package/dist/esm/plugins/analyze/utils.mjs +24 -7
  16. package/dist/esm/plugins/deploy/utils/generator.mjs +1 -1
  17. package/dist/esm/utils/register.mjs +63 -30
  18. package/dist/esm-node/commands/dev.mjs +1 -2
  19. package/dist/esm-node/esm/register-esm.mjs +3 -8
  20. package/dist/esm-node/esm/ts-node-loader.mjs +6 -9
  21. package/dist/esm-node/esm/ts-paths-loader.mjs +14 -36
  22. package/dist/esm-node/plugins/analyze/utils.mjs +24 -7
  23. package/dist/esm-node/plugins/deploy/utils/generator.mjs +1 -1
  24. package/dist/esm-node/utils/register.mjs +63 -30
  25. package/dist/types/esm/register-esm.d.mts +3 -2
  26. package/dist/types/esm/ts-node-loader.d.mts +3 -4
  27. package/dist/types/utils/register.d.ts +0 -1
  28. package/package.json +13 -14
  29. package/dist/cjs/esm/utils.js +0 -69
  30. package/dist/cjs/esm/utils.mjs +0 -31
  31. package/dist/esm/esm/utils.mjs +0 -32
  32. package/dist/esm-node/esm/utils.mjs +0 -33
  33. package/dist/types/esm/utils.d.mts +0 -5
@@ -52,8 +52,7 @@ const dev = async (api, options, devServerOptions)=>{
52
52
  const hooks = api.getHooks();
53
53
  const combinedAlias = [].concat(normalizedConfig?.resolve?.alias ?? []).concat(normalizedConfig?.source?.alias ?? []);
54
54
  await (0, register_js_namespaceObject.setupTsRuntime)(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
55
- moduleType: appContext.moduleType,
56
- preferTsNodeForServerRuntime: true
55
+ moduleType: appContext.moduleType
57
56
  });
58
57
  const { appDirectory, port, apiOnly, metaName, serverRoutes } = appContext;
59
58
  const meta = (0, utils_namespaceObject.getMeta)(metaName);
@@ -31,12 +31,9 @@ __webpack_require__.d(__webpack_exports__, {
31
31
  registerPathsLoader: ()=>registerPathsLoader
32
32
  });
33
33
  const external_node_path_namespaceObject = require("node:path");
34
- const utils_namespaceObject = require("@modern-js/utils");
35
- const registerModuleHooks = async ({ appDir, distDir, alias })=>{
34
+ const registerModuleHooks = async ({ appDir, distDir, baseUrl, paths })=>{
36
35
  const TS_CONFIG_FILENAME = "tsconfig.json";
37
36
  const tsconfigPath = external_node_path_namespaceObject.resolve(appDir, TS_CONFIG_FILENAME);
38
- const hasTsconfig = await utils_namespaceObject.fs.pathExists(tsconfigPath);
39
- if (!hasTsconfig) return;
40
37
  const { register } = await import("node:module");
41
38
  process.env.TS_NODE_TRANSPILE_ONLY = true;
42
39
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -45,10 +42,8 @@ const registerModuleHooks = async ({ appDir, distDir, alias })=>{
45
42
  process.env.TS_NODE_IGNORE = `(?:^|/)node_modules/,(?:^|/)${external_node_path_namespaceObject.relative(appDir, distDir)}/`;
46
43
  register('./ts-node-loader.mjs', __rslib_import_meta_url__, {
47
44
  data: {
48
- appDir,
49
- distDir,
50
- alias,
51
- tsconfigPath
45
+ baseUrl,
46
+ paths
52
47
  }
53
48
  });
54
49
  };
@@ -1,10 +1,7 @@
1
1
  import path from 'node:path';
2
- import { fs } from '@modern-js/utils';
3
- export const registerModuleHooks = async ({ appDir, distDir, alias })=>{
2
+ export const registerModuleHooks = async ({ appDir, distDir, baseUrl, paths })=>{
4
3
  const TS_CONFIG_FILENAME = "tsconfig.json";
5
4
  const tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
6
- const hasTsconfig = await fs.pathExists(tsconfigPath);
7
- if (!hasTsconfig) return;
8
5
  const { register } = await import('node:module');
9
6
  process.env.TS_NODE_TRANSPILE_ONLY = true;
10
7
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -13,10 +10,8 @@ export const registerModuleHooks = async ({ appDir, distDir, alias })=>{
13
10
  process.env.TS_NODE_IGNORE = `(?:^|/)node_modules/,(?:^|/)${path.relative(appDir, distDir)}/`;
14
11
  register('./ts-node-loader.mjs', import.meta.url, {
15
12
  data: {
16
- appDir,
17
- distDir,
18
- alias,
19
- tsconfigPath
13
+ baseUrl,
14
+ paths
20
15
  }
21
16
  });
22
17
  };
@@ -29,19 +29,16 @@ __webpack_require__.d(__webpack_exports__, {
29
29
  resolve: ()=>resolve
30
30
  });
31
31
  const external_url_namespaceObject = require("url");
32
+ const utils_namespaceObject = require("@modern-js/utils");
33
+ const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
32
34
  const esm_namespaceObject = require("ts-node/esm");
33
- const external_utils_js_namespaceObject = require("./utils.js");
34
35
  let matchPath;
35
- async function initialize({ appDir, alias, tsconfigPath }) {
36
- matchPath = (0, external_utils_js_namespaceObject.createMatchPath)({
37
- alias,
38
- appDir,
39
- tsconfigPath
40
- });
36
+ async function initialize({ baseUrl, paths }) {
37
+ matchPath = (0, tsconfig_paths_namespaceObject.createMatchPath)(baseUrl || './', paths || {});
41
38
  }
42
39
  function resolve(specifier, context, defaultResolve) {
43
- const match = matchPath(specifier);
44
- return match ? (0, esm_namespaceObject.resolve)((0, external_url_namespaceObject.pathToFileURL)(match).href, context, defaultResolve) : (0, esm_namespaceObject.resolve)(specifier, context, defaultResolve);
40
+ const match = (0, utils_namespaceObject.findMatchedSourcePath)(matchPath, specifier);
41
+ return match ? (0, esm_namespaceObject.resolve)((0, external_url_namespaceObject.pathToFileURL)((0, utils_namespaceObject.findSourceEntry)(match) || match).href, context, defaultResolve) : (0, esm_namespaceObject.resolve)(specifier, context, defaultResolve);
45
42
  }
46
43
  function load(url, context, defaultLoad) {
47
44
  const filePath = new URL(url).pathname;
@@ -1,18 +1,15 @@
1
1
  import { pathToFileURL } from 'url';
2
+ import { findMatchedSourcePath, findSourceEntry } from '@modern-js/utils';
3
+ import { createMatchPath } from '@modern-js/utils/tsconfig-paths';
2
4
  import { resolve as tsNodeResolve } from 'ts-node/esm';
3
5
  import { load as tsNodeLoad } from 'ts-node/esm';
4
- import { createMatchPath } from './utils.mjs';
5
6
  let matchPath;
6
- export async function initialize({ appDir, alias, tsconfigPath }) {
7
- matchPath = createMatchPath({
8
- alias,
9
- appDir,
10
- tsconfigPath
11
- });
7
+ export async function initialize({ baseUrl, paths }) {
8
+ matchPath = createMatchPath(baseUrl || './', paths || {});
12
9
  }
13
10
  export function resolve(specifier, context, defaultResolve) {
14
- const match = matchPath(specifier);
15
- return match ? tsNodeResolve(pathToFileURL(match).href, context, defaultResolve) : tsNodeResolve(specifier, context, defaultResolve);
11
+ const match = findMatchedSourcePath(matchPath, specifier);
12
+ return match ? tsNodeResolve(pathToFileURL(findSourceEntry(match) || match).href, context, defaultResolve) : tsNodeResolve(specifier, context, defaultResolve);
16
13
  }
17
14
  export function load(url, context, defaultLoad) {
18
15
  const filePath = new URL(url).pathname;
@@ -30,58 +30,36 @@ __webpack_require__.d(__webpack_exports__, {
30
30
  const external_node_fs_namespaceObject = require("node:fs");
31
31
  const external_node_path_namespaceObject = require("node:path");
32
32
  const external_url_namespaceObject = require("url");
33
+ const utils_namespaceObject = require("@modern-js/utils");
33
34
  const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
34
35
  let matchPath;
35
36
  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;
37
+ const getParentPath = (parentURL)=>{
38
+ if (!parentURL) return process.cwd();
39
+ if (external_node_path_namespaceObject.isAbsolute(parentURL)) return external_node_path_namespaceObject.dirname(parentURL);
40
+ try {
41
+ const url = new URL(parentURL);
42
+ if ('file:' === url.protocol) return external_node_path_namespaceObject.dirname((0, external_url_namespaceObject.fileURLToPath)(url));
43
+ } catch {}
44
+ return process.cwd();
60
45
  };
61
46
  async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
62
47
  appDir = external_node_path_namespaceObject.resolve(currentAppDir);
63
48
  matchPath = (0, tsconfig_paths_namespaceObject.createMatchPath)(baseUrl || './', paths || {});
64
49
  }
65
50
  function resolve(specifier, context, defaultResolve) {
66
- const parentPath = context.parentURL ? external_node_path_namespaceObject.dirname((0, external_url_namespaceObject.fileURLToPath)(context.parentURL)) : process.cwd();
51
+ const parentPath = getParentPath(context.parentURL);
67
52
  const relativeFromApp = appDir ? external_node_path_namespaceObject.relative(appDir, parentPath) : '';
68
53
  const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !external_node_path_namespaceObject.isAbsolute(relativeFromApp));
69
54
  if ((specifier.startsWith('./') || specifier.startsWith('../')) && !external_node_path_namespaceObject.extname(specifier) && isAppFile) {
70
- const resolvedPath = resolvePathWithExtensions(external_node_path_namespaceObject.resolve(parentPath, specifier));
55
+ const matchedPath = external_node_path_namespaceObject.resolve(parentPath, specifier);
56
+ const resolvedPath = (0, utils_namespaceObject.findSourceEntry)(matchedPath) || matchedPath;
71
57
  if (resolvedPath && external_node_fs_namespaceObject.existsSync(resolvedPath)) return defaultResolve((0, external_url_namespaceObject.pathToFileURL)(resolvedPath).href, context, defaultResolve);
72
58
  }
73
59
  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
- ]);
60
+ const match = (0, utils_namespaceObject.findMatchedSourcePath)(matchPath, specifier);
83
61
  if (!match) return defaultResolve(specifier, context, defaultResolve);
84
- const resolvedPath = resolvePathWithExtensions(match);
62
+ const resolvedPath = (0, utils_namespaceObject.findSourceEntry)(match) || match;
85
63
  return defaultResolve((0, external_url_namespaceObject.pathToFileURL)(resolvedPath).href, context, defaultResolve);
86
64
  }
87
65
  exports.initialize = __webpack_exports__.initialize;
@@ -1,57 +1,35 @@
1
1
  import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import { fileURLToPath, pathToFileURL } from 'url';
4
+ import { findMatchedSourcePath, findSourceEntry } from '@modern-js/utils';
4
5
  import { createMatchPath as oCreateMatchPath } from '@modern-js/utils/tsconfig-paths';
5
6
  let matchPath;
6
7
  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;
8
+ const getParentPath = (parentURL)=>{
9
+ if (!parentURL) return process.cwd();
10
+ if (path.isAbsolute(parentURL)) return path.dirname(parentURL);
11
+ try {
12
+ const url = new URL(parentURL);
13
+ if ('file:' === url.protocol) return path.dirname(fileURLToPath(url));
14
+ } catch {}
15
+ return process.cwd();
31
16
  };
32
17
  export async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
33
18
  appDir = path.resolve(currentAppDir);
34
19
  matchPath = oCreateMatchPath(baseUrl || './', paths || {});
35
20
  }
36
21
  export function resolve(specifier, context, defaultResolve) {
37
- const parentPath = context.parentURL ? path.dirname(fileURLToPath(context.parentURL)) : process.cwd();
22
+ const parentPath = getParentPath(context.parentURL);
38
23
  const relativeFromApp = appDir ? path.relative(appDir, parentPath) : '';
39
24
  const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !path.isAbsolute(relativeFromApp));
40
25
  if ((specifier.startsWith('./') || specifier.startsWith('../')) && !path.extname(specifier) && isAppFile) {
41
- const resolvedPath = resolvePathWithExtensions(path.resolve(parentPath, specifier));
26
+ const matchedPath = path.resolve(parentPath, specifier);
27
+ const resolvedPath = findSourceEntry(matchedPath) || matchedPath;
42
28
  if (resolvedPath && fs.existsSync(resolvedPath)) return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
43
29
  }
44
30
  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
- ]);
31
+ const match = findMatchedSourcePath(matchPath, specifier);
54
32
  if (!match) return defaultResolve(specifier, context, defaultResolve);
55
- const resolvedPath = resolvePathWithExtensions(match);
33
+ const resolvedPath = findSourceEntry(match) || match;
56
34
  return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
57
35
  }
@@ -46,8 +46,8 @@ var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namesp
46
46
  const external_path_namespaceObject = require("path");
47
47
  var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
48
48
  const utils_namespaceObject = require("@modern-js/utils");
49
+ const core_namespaceObject = require("@swc/core");
49
50
  const external_es_module_lexer_namespaceObject = require("es-module-lexer");
50
- const external_esbuild_namespaceObject = require("esbuild");
51
51
  const walkDirectory = (dir)=>external_fs_default().readdirSync(dir).reduce((previous, filename)=>{
52
52
  const filePath = external_path_default().join(dir, filename);
53
53
  if (external_fs_default().statSync(filePath).isDirectory()) return [
@@ -66,13 +66,30 @@ const replaceWithAlias = (base, filePath, alias)=>{
66
66
  const parseModule = async ({ source, filename })=>{
67
67
  let content = source;
68
68
  if (utils_namespaceObject.JS_EXTENSIONS.some((ext)=>filename.endsWith(ext))) {
69
- const result = await (0, external_esbuild_namespaceObject.transform)(content, {
70
- loader: external_path_default().extname(filename).slice(1),
71
- format: 'esm',
72
- tsconfigRaw: {
73
- compilerOptions: {
74
- experimentalDecorators: true
75
- }
69
+ const ext = external_path_default().extname(filename);
70
+ const isTs = '.ts' === ext || '.tsx' === ext;
71
+ const isJsx = '.jsx' === ext || '.tsx' === ext;
72
+ const result = await (0, core_namespaceObject.transform)(content, {
73
+ filename,
74
+ isModule: true,
75
+ module: {
76
+ type: 'es6'
77
+ },
78
+ jsc: {
79
+ parser: isTs ? {
80
+ syntax: "typescript",
81
+ tsx: isJsx,
82
+ decorators: true
83
+ } : {
84
+ syntax: "ecmascript",
85
+ jsx: isJsx,
86
+ decorators: true
87
+ },
88
+ transform: {
89
+ legacyDecorator: true
90
+ },
91
+ target: 'es2022',
92
+ keepClassNames: true
76
93
  }
77
94
  });
78
95
  content = result.code;
@@ -60,7 +60,7 @@ const genPluginImportsCode = (plugins, isESM = false)=>plugins.map(([name, optio
60
60
  return `${im};const plugin_${index} = plugin_${index}_ns.default || plugin_${index}_ns`;
61
61
  }).join(';\n');
62
62
  const getPluginsCode = (plugins)=>`[${plugins.map(([, options], index)=>`plugin_${index}(${options ? JSON.stringify(options) : ''})`).join(',')}]`;
63
- const getServerConfigPath = (meta)=>`"${(0, external_index_js_namespaceObject.normalizePath)(external_node_path_default().join(utils_namespaceObject.SERVER_DIR, `${meta}.server`))}"`;
63
+ const getServerConfigPath = (meta)=>`path.join(__dirname, "${utils_namespaceObject.SERVER_DIR}", "${meta}.server")`;
64
64
  const generateHandler = async ({ template, appContext, config, serverConfig: modifyServerConfig, genAppContextTemplate = serverAppContextTemplate, genPluginImports = genPluginImportsCode, isESM })=>{
65
65
  const { serverPlugins, metaName } = appContext;
66
66
  const plugins = serverPlugins.map((plugin)=>[
@@ -39,12 +39,58 @@ __webpack_require__.d(__webpack_exports__, {
39
39
  const external_node_path_namespaceObject = require("node:path");
40
40
  var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
41
41
  const utils_namespaceObject = require("@modern-js/utils");
42
+ const normalizePathValue = ({ key, value, absoluteBaseUrl })=>{
43
+ let normalizedValue = value;
44
+ if (key.startsWith('@') && normalizedValue.startsWith('@')) try {
45
+ normalizedValue = require.resolve(normalizedValue, {
46
+ paths: [
47
+ process.cwd(),
48
+ ...module.paths
49
+ ]
50
+ });
51
+ } catch {}
52
+ return external_node_path_default().isAbsolute(normalizedValue) ? external_node_path_default().relative(absoluteBaseUrl, normalizedValue) : normalizedValue;
53
+ };
54
+ const normalizePathValues = ({ key, value, absoluteBaseUrl })=>{
55
+ const values = Array.isArray(value) ? value : [
56
+ value
57
+ ];
58
+ return values.map((item)=>normalizePathValue({
59
+ key,
60
+ value: item,
61
+ absoluteBaseUrl
62
+ }));
63
+ };
64
+ const addResolvedAlias = (paths, key, values)=>{
65
+ if (!key || paths[key]) return;
66
+ paths[key] = values;
67
+ };
68
+ const createRuntimePaths = ({ alias, paths, absoluteBaseUrl })=>{
69
+ const mergedAlias = (0, utils_namespaceObject.mergeAlias)(alias);
70
+ const normalizedPaths = Object.keys(paths).reduce((result, key)=>{
71
+ addResolvedAlias(result, key.endsWith('$') ? key.slice(0, -1) : key, normalizePathValues({
72
+ key,
73
+ value: paths[key],
74
+ absoluteBaseUrl
75
+ }));
76
+ return result;
77
+ }, {});
78
+ Object.keys(mergedAlias).forEach((key)=>{
79
+ if (key.includes('*') || key.endsWith('$')) return;
80
+ addResolvedAlias(normalizedPaths, `${key}/*`, normalizePathValues({
81
+ key,
82
+ value: mergedAlias[key],
83
+ absoluteBaseUrl
84
+ }).map((value)=>`${value}/*`));
85
+ });
86
+ return normalizedPaths;
87
+ };
42
88
  const resolveTsRuntimeRegisterMode = (hasTsNode)=>{
89
+ if (hasTsNode) return 'ts-node';
43
90
  const hasNativeTypeScriptSupport = process.features?.typescript;
44
91
  const nodeMajorVersion = Number(process.versions.node.split('.')[0]);
45
92
  const supportsNativeTypeScript = void 0 === hasNativeTypeScriptSupport ? nodeMajorVersion >= 22 : false !== hasNativeTypeScriptSupport;
46
93
  if (supportsNativeTypeScript) return 'node-loader';
47
- if (hasTsNode) return 'ts-node';
48
94
  return 'unsupported';
49
95
  };
50
96
  const setupTsRuntime = async (appDir, distDir, alias, options = {})=>{
@@ -53,32 +99,17 @@ const setupTsRuntime = async (appDir, distDir, alias, options = {})=>{
53
99
  const isTsProject = await utils_namespaceObject.fs.pathExists(tsconfigPath);
54
100
  const hasTsNode = (0, utils_namespaceObject.isDepExists)(appDir, 'ts-node');
55
101
  if (!isTsProject) return;
56
- const preferredRegisterMode = resolveTsRuntimeRegisterMode(hasTsNode);
57
- const registerMode = options.preferTsNodeForServerRuntime && hasTsNode ? 'ts-node' : preferredRegisterMode;
102
+ const registerMode = resolveTsRuntimeRegisterMode(hasTsNode);
58
103
  const aliasConfig = (0, utils_namespaceObject.getAliasConfig)(alias, {
59
104
  appDirectory: appDir,
60
105
  tsconfigPath
61
106
  });
62
107
  const { paths = {}, absoluteBaseUrl = './' } = aliasConfig;
63
- const tsPaths = Object.keys(paths).reduce((o, key)=>{
64
- let tsPath = paths[key];
65
- if ('string' == typeof tsPath && key.startsWith('@') && tsPath.startsWith('@')) try {
66
- tsPath = require.resolve(tsPath, {
67
- paths: [
68
- process.cwd(),
69
- ...module.paths
70
- ]
71
- });
72
- } catch {}
73
- if ('string' == typeof tsPath && external_node_path_default().isAbsolute(tsPath)) tsPath = external_node_path_default().relative(absoluteBaseUrl, tsPath);
74
- if ('string' == typeof tsPath) tsPath = [
75
- tsPath
76
- ];
77
- return {
78
- ...o,
79
- [`${key}`]: tsPath
80
- };
81
- }, {});
108
+ const runtimePaths = createRuntimePaths({
109
+ alias,
110
+ paths,
111
+ absoluteBaseUrl
112
+ });
82
113
  if ('unsupported' === registerMode) return;
83
114
  if ('ts-node' === registerMode) {
84
115
  if ('module' === options.moduleType) {
@@ -86,7 +117,14 @@ const setupTsRuntime = async (appDir, distDir, alias, options = {})=>{
86
117
  await registerModuleHooks({
87
118
  appDir,
88
119
  distDir,
89
- alias: alias || {}
120
+ baseUrl: absoluteBaseUrl || './',
121
+ paths: runtimePaths
122
+ });
123
+ } else {
124
+ const { register } = await import("@modern-js/utils/tsconfig-paths");
125
+ register({
126
+ baseUrl: absoluteBaseUrl || './',
127
+ paths: runtimePaths
90
128
  });
91
129
  }
92
130
  const tsConfig = (0, utils_namespaceObject.readTsConfigByFile)(tsconfigPath);
@@ -108,14 +146,9 @@ const setupTsRuntime = async (appDir, distDir, alias, options = {})=>{
108
146
  await registerPathsLoader({
109
147
  appDir,
110
148
  baseUrl: absoluteBaseUrl || './',
111
- paths: tsPaths
149
+ paths: runtimePaths
112
150
  });
113
151
  }
114
- const { register } = await import("@modern-js/utils/tsconfig-paths");
115
- register({
116
- baseUrl: absoluteBaseUrl || './',
117
- paths: tsPaths
118
- });
119
152
  };
120
153
  exports.resolveTsRuntimeRegisterMode = __webpack_exports__.resolveTsRuntimeRegisterMode;
121
154
  exports.setupTsRuntime = __webpack_exports__.setupTsRuntime;
@@ -14,8 +14,7 @@ const dev = async (api, options, devServerOptions)=>{
14
14
  const hooks = api.getHooks();
15
15
  const combinedAlias = [].concat(normalizedConfig?.resolve?.alias ?? []).concat(normalizedConfig?.source?.alias ?? []);
16
16
  await setupTsRuntime(appContext.appDirectory, appContext.distDirectory, combinedAlias, {
17
- moduleType: appContext.moduleType,
18
- preferTsNodeForServerRuntime: true
17
+ moduleType: appContext.moduleType
19
18
  });
20
19
  const { appDirectory, port, apiOnly, metaName, serverRoutes } = appContext;
21
20
  const meta = getMeta(metaName);
@@ -1,10 +1,7 @@
1
1
  import node_path from "node:path";
2
- import { fs } from "@modern-js/utils";
3
- const registerModuleHooks = async ({ appDir, distDir, alias })=>{
2
+ const registerModuleHooks = async ({ appDir, distDir, baseUrl, paths })=>{
4
3
  const TS_CONFIG_FILENAME = "tsconfig.json";
5
4
  const tsconfigPath = node_path.resolve(appDir, TS_CONFIG_FILENAME);
6
- const hasTsconfig = await fs.pathExists(tsconfigPath);
7
- if (!hasTsconfig) return;
8
5
  const { register } = await import("node:module");
9
6
  process.env.TS_NODE_TRANSPILE_ONLY = true;
10
7
  process.env.TS_NODE_PROJECT = tsconfigPath;
@@ -13,10 +10,8 @@ const registerModuleHooks = async ({ appDir, distDir, alias })=>{
13
10
  process.env.TS_NODE_IGNORE = `(?:^|/)node_modules/,(?:^|/)${node_path.relative(appDir, distDir)}/`;
14
11
  register('./ts-node-loader.mjs', import.meta.url, {
15
12
  data: {
16
- appDir,
17
- distDir,
18
- alias,
19
- tsconfigPath
13
+ baseUrl,
14
+ paths
20
15
  }
21
16
  });
22
17
  };
@@ -1,17 +1,14 @@
1
1
  import { pathToFileURL } from "url";
2
+ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
3
+ import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
2
4
  import { load, resolve } from "ts-node/esm";
3
- import { createMatchPath } from "./utils.mjs";
4
5
  let matchPath;
5
- async function initialize({ appDir, alias, tsconfigPath }) {
6
- matchPath = createMatchPath({
7
- alias,
8
- appDir,
9
- tsconfigPath
10
- });
6
+ async function initialize({ baseUrl, paths }) {
7
+ matchPath = createMatchPath(baseUrl || './', paths || {});
11
8
  }
12
9
  function ts_node_loader_resolve(specifier, context, defaultResolve) {
13
- const match = matchPath(specifier);
14
- return match ? resolve(pathToFileURL(match).href, context, defaultResolve) : resolve(specifier, context, defaultResolve);
10
+ const match = findMatchedSourcePath(matchPath, specifier);
11
+ return match ? resolve(pathToFileURL(findSourceEntry(match) || match).href, context, defaultResolve) : resolve(specifier, context, defaultResolve);
15
12
  }
16
13
  function ts_node_loader_load(url, context, defaultLoad) {
17
14
  const filePath = new URL(url).pathname;
@@ -1,58 +1,36 @@
1
1
  import node_fs from "node:fs";
2
2
  import node_path from "node:path";
3
3
  import { fileURLToPath, pathToFileURL } from "url";
4
+ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
4
5
  import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
5
6
  let matchPath;
6
7
  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;
8
+ const getParentPath = (parentURL)=>{
9
+ if (!parentURL) return process.cwd();
10
+ if (node_path.isAbsolute(parentURL)) return node_path.dirname(parentURL);
11
+ try {
12
+ const url = new URL(parentURL);
13
+ if ('file:' === url.protocol) return node_path.dirname(fileURLToPath(url));
14
+ } catch {}
15
+ return process.cwd();
31
16
  };
32
17
  async function initialize({ appDir: currentAppDir, baseUrl, paths }) {
33
18
  appDir = node_path.resolve(currentAppDir);
34
19
  matchPath = createMatchPath(baseUrl || './', paths || {});
35
20
  }
36
21
  function resolve(specifier, context, defaultResolve) {
37
- const parentPath = context.parentURL ? node_path.dirname(fileURLToPath(context.parentURL)) : process.cwd();
22
+ const parentPath = getParentPath(context.parentURL);
38
23
  const relativeFromApp = appDir ? node_path.relative(appDir, parentPath) : '';
39
24
  const isAppFile = appDir && (parentPath === appDir || relativeFromApp && !relativeFromApp.startsWith('..') && !node_path.isAbsolute(relativeFromApp));
40
25
  if ((specifier.startsWith('./') || specifier.startsWith('../')) && !node_path.extname(specifier) && isAppFile) {
41
- const resolvedPath = resolvePathWithExtensions(node_path.resolve(parentPath, specifier));
26
+ const matchedPath = node_path.resolve(parentPath, specifier);
27
+ const resolvedPath = findSourceEntry(matchedPath) || matchedPath;
42
28
  if (resolvedPath && node_fs.existsSync(resolvedPath)) return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
43
29
  }
44
30
  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
- ]);
31
+ const match = findMatchedSourcePath(matchPath, specifier);
54
32
  if (!match) return defaultResolve(specifier, context, defaultResolve);
55
- const resolvedPath = resolvePathWithExtensions(match);
33
+ const resolvedPath = findSourceEntry(match) || match;
56
34
  return defaultResolve(pathToFileURL(resolvedPath).href, context, defaultResolve);
57
35
  }
58
36
  export { initialize, resolve };
@@ -1,8 +1,8 @@
1
1
  import fs from "fs";
2
2
  import path from "path";
3
3
  import { JS_EXTENSIONS, getCommand, normalizeToPosixPath } from "@modern-js/utils";
4
+ import { transform } from "@swc/core";
4
5
  import { parse } from "es-module-lexer";
5
- import { transform } from "esbuild";
6
6
  const walkDirectory = (dir)=>fs.readdirSync(dir).reduce((previous, filename)=>{
7
7
  const filePath = path.join(dir, filename);
8
8
  if (fs.statSync(filePath).isDirectory()) return [
@@ -21,13 +21,30 @@ const replaceWithAlias = (base, filePath, alias)=>{
21
21
  const parseModule = async ({ source, filename })=>{
22
22
  let content = source;
23
23
  if (JS_EXTENSIONS.some((ext)=>filename.endsWith(ext))) {
24
+ const ext = path.extname(filename);
25
+ const isTs = '.ts' === ext || '.tsx' === ext;
26
+ const isJsx = '.jsx' === ext || '.tsx' === ext;
24
27
  const result = await transform(content, {
25
- loader: path.extname(filename).slice(1),
26
- format: 'esm',
27
- tsconfigRaw: {
28
- compilerOptions: {
29
- experimentalDecorators: true
30
- }
28
+ filename,
29
+ isModule: true,
30
+ module: {
31
+ type: 'es6'
32
+ },
33
+ jsc: {
34
+ parser: isTs ? {
35
+ syntax: "typescript",
36
+ tsx: isJsx,
37
+ decorators: true
38
+ } : {
39
+ syntax: "ecmascript",
40
+ jsx: isJsx,
41
+ decorators: true
42
+ },
43
+ transform: {
44
+ legacyDecorator: true
45
+ },
46
+ target: 'es2022',
47
+ keepClassNames: true
31
48
  }
32
49
  });
33
50
  content = result.code;
@@ -18,7 +18,7 @@ const genPluginImportsCode = (plugins, isESM = false)=>plugins.map(([name, optio
18
18
  return `${im};const plugin_${index} = plugin_${index}_ns.default || plugin_${index}_ns`;
19
19
  }).join(';\n');
20
20
  const getPluginsCode = (plugins)=>`[${plugins.map(([, options], index)=>`plugin_${index}(${options ? JSON.stringify(options) : ''})`).join(',')}]`;
21
- const getServerConfigPath = (meta)=>`"${normalizePath(node_path.join(SERVER_DIR, `${meta}.server`))}"`;
21
+ const getServerConfigPath = (meta)=>`path.join(__dirname, "${SERVER_DIR}", "${meta}.server")`;
22
22
  const generateHandler = async ({ template, appContext, config, serverConfig: modifyServerConfig, genAppContextTemplate = serverAppContextTemplate, genPluginImports = genPluginImportsCode, isESM })=>{
23
23
  const { serverPlugins, metaName } = appContext;
24
24
  const plugins = serverPlugins.map((plugin)=>[