@modern-js/server-utils 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.
@@ -88,7 +88,7 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
88
88
  outDir: distDir
89
89
  }
90
90
  });
91
- const tsconfigPathsPlugin = (0, external_tsconfigPathsPlugin_js_namespaceObject.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
91
+ const tsconfigPathsPlugin = (0, external_tsconfigPathsPlugin_js_namespaceObject.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
92
92
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
93
93
  before: [
94
94
  tsconfigPathsPlugin
@@ -38,8 +38,19 @@ __webpack_require__.d(__webpack_exports__, {
38
38
  const external_os_namespaceObject = require("os");
39
39
  const external_path_namespaceObject = require("path");
40
40
  var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
41
+ const utils_namespaceObject = require("@modern-js/utils");
41
42
  const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
42
43
  const external_typescript_namespaceObject = require("typescript");
44
+ const toEsmOutputPath = (resolvedPath)=>{
45
+ const sourcePath = (0, utils_namespaceObject.findSourceEntry)(resolvedPath) || resolvedPath;
46
+ const ext = external_path_default().extname(sourcePath);
47
+ return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
48
+ };
49
+ const resolveRelativeEsmSpecifier = (sf, text)=>{
50
+ if (!text.startsWith('./') && !text.startsWith('../')) return;
51
+ const importerDir = (0, external_path_namespaceObject.dirname)(sf.fileName);
52
+ return external_path_default().resolve(importerDir, text);
53
+ };
43
54
  const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
44
55
  const resolveAliasPath = (baseUrl, filePath)=>{
45
56
  if (filePath.startsWith('.') || filePath.startsWith('..')) return external_path_default().resolve(baseUrl, filePath);
@@ -81,7 +92,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
81
92
  };
82
93
  };
83
94
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === external_typescript_namespaceObject.SyntaxKind.ImportKeyword;
84
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
95
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
85
96
  const tsPaths = {};
86
97
  const alias = {};
87
98
  Object.keys(paths).forEach((key)=>{
@@ -103,7 +114,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
103
114
  if (isDynamicImport(tsBinary, node)) {
104
115
  const importPathWithQuotes = node.arguments[0].getText(sf);
105
116
  const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
106
- const result = getNotAliasedPath(sf, matchPath, text);
117
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
107
118
  if (!result) return node;
108
119
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
109
120
  tsBinary.factory.createStringLiteral(result)
@@ -113,7 +124,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
113
124
  const importPathWithQuotes = node?.moduleSpecifier?.getText();
114
125
  if (!importPathWithQuotes) return node;
115
126
  const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
116
- const result = getNotAliasedPath(sf, matchPath, text);
127
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
117
128
  if (!result) return node;
118
129
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
119
130
  moduleSpecifier.parent = node.moduleSpecifier.parent;
@@ -129,13 +140,9 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
129
140
  return tsBinary.visitNode(sf, visitNode);
130
141
  };
131
142
  }
132
- function getNotAliasedPath(sf, matcher, text) {
133
- let result = matcher(text, void 0, void 0, [
134
- '.ts',
135
- '.tsx',
136
- '.js',
137
- '.jsx'
138
- ]);
143
+ function getNotAliasedPath(sf, matcher, text, moduleType) {
144
+ let result = (0, utils_namespaceObject.findMatchedSourcePath)(matcher, text);
145
+ if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
139
146
  if (!result) return;
140
147
  if ('win32' === external_os_namespaceObject.platform()) result = result.replace(/\\/g, '/');
141
148
  if (!external_path_default().isAbsolute(result)) {
@@ -158,6 +165,7 @@ function getNotAliasedPath(sf, matcher, text) {
158
165
  if (packagePath) return text;
159
166
  } catch {}
160
167
  }
168
+ if ('module' === moduleType) result = toEsmOutputPath(result);
161
169
  const resolvedPath = external_path_namespaceObject.posix.relative((0, external_path_namespaceObject.dirname)(sf.fileName), result) || './';
162
170
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
163
171
  }
@@ -50,7 +50,7 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
50
50
  outDir: distDir
51
51
  }
52
52
  });
53
- const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
53
+ const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
54
54
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
55
55
  before: [
56
56
  tsconfigPathsPlugin
@@ -1,7 +1,18 @@
1
1
  import path, { dirname, posix } from "path";
2
+ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
2
3
  import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
3
4
  import * as __rspack_external_os from "os";
4
5
  import * as __rspack_external_typescript from "typescript";
6
+ const toEsmOutputPath = (resolvedPath)=>{
7
+ const sourcePath = findSourceEntry(resolvedPath) || resolvedPath;
8
+ const ext = path.extname(sourcePath);
9
+ return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
10
+ };
11
+ const resolveRelativeEsmSpecifier = (sf, text)=>{
12
+ if (!text.startsWith('./') && !text.startsWith('../')) return;
13
+ const importerDir = dirname(sf.fileName);
14
+ return path.resolve(importerDir, text);
15
+ };
5
16
  const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
6
17
  const resolveAliasPath = (baseUrl, filePath)=>{
7
18
  if (filePath.startsWith('.') || filePath.startsWith('..')) return path.resolve(baseUrl, filePath);
@@ -43,7 +54,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
43
54
  };
44
55
  };
45
56
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === __rspack_external_typescript.SyntaxKind.ImportKeyword;
46
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
57
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
47
58
  const tsPaths = {};
48
59
  const alias = {};
49
60
  Object.keys(paths).forEach((key)=>{
@@ -65,7 +76,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
65
76
  if (isDynamicImport(tsBinary, node)) {
66
77
  const importPathWithQuotes = node.arguments[0].getText(sf);
67
78
  const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
68
- const result = getNotAliasedPath(sf, matchPath, text);
79
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
69
80
  if (!result) return node;
70
81
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
71
82
  tsBinary.factory.createStringLiteral(result)
@@ -75,7 +86,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
75
86
  const importPathWithQuotes = node?.moduleSpecifier?.getText();
76
87
  if (!importPathWithQuotes) return node;
77
88
  const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
78
- const result = getNotAliasedPath(sf, matchPath, text);
89
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
79
90
  if (!result) return node;
80
91
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
81
92
  moduleSpecifier.parent = node.moduleSpecifier.parent;
@@ -91,13 +102,9 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
91
102
  return tsBinary.visitNode(sf, visitNode);
92
103
  };
93
104
  }
94
- function getNotAliasedPath(sf, matcher, text) {
95
- let result = matcher(text, void 0, void 0, [
96
- '.ts',
97
- '.tsx',
98
- '.js',
99
- '.jsx'
100
- ]);
105
+ function getNotAliasedPath(sf, matcher, text, moduleType) {
106
+ let result = findMatchedSourcePath(matcher, text);
107
+ if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
101
108
  if (!result) return;
102
109
  if ('win32' === __rspack_external_os.platform()) result = result.replace(/\\/g, '/');
103
110
  if (!path.isAbsolute(result)) {
@@ -120,6 +127,7 @@ function getNotAliasedPath(sf, matcher, text) {
120
127
  if (packagePath) return text;
121
128
  } catch {}
122
129
  }
130
+ if ('module' === moduleType) result = toEsmOutputPath(result);
123
131
  const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
124
132
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
125
133
  }
@@ -51,7 +51,7 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
51
51
  outDir: distDir
52
52
  }
53
53
  });
54
- const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
54
+ const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
55
55
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
56
56
  before: [
57
57
  tsconfigPathsPlugin
@@ -1,9 +1,20 @@
1
1
  import __rslib_shim_module__ from "node:module";
2
2
  const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
3
3
  import path, { dirname, posix } from "path";
4
+ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
4
5
  import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
5
6
  import * as __rspack_external_os from "os";
6
7
  import * as __rspack_external_typescript from "typescript";
8
+ const toEsmOutputPath = (resolvedPath)=>{
9
+ const sourcePath = findSourceEntry(resolvedPath) || resolvedPath;
10
+ const ext = path.extname(sourcePath);
11
+ return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
12
+ };
13
+ const resolveRelativeEsmSpecifier = (sf, text)=>{
14
+ if (!text.startsWith('./') && !text.startsWith('../')) return;
15
+ const importerDir = dirname(sf.fileName);
16
+ return path.resolve(importerDir, text);
17
+ };
7
18
  const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
8
19
  const resolveAliasPath = (baseUrl, filePath)=>{
9
20
  if (filePath.startsWith('.') || filePath.startsWith('..')) return path.resolve(baseUrl, filePath);
@@ -45,7 +56,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
45
56
  };
46
57
  };
47
58
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === __rspack_external_typescript.SyntaxKind.ImportKeyword;
48
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
59
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
49
60
  const tsPaths = {};
50
61
  const alias = {};
51
62
  Object.keys(paths).forEach((key)=>{
@@ -67,7 +78,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
67
78
  if (isDynamicImport(tsBinary, node)) {
68
79
  const importPathWithQuotes = node.arguments[0].getText(sf);
69
80
  const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
70
- const result = getNotAliasedPath(sf, matchPath, text);
81
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
71
82
  if (!result) return node;
72
83
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
73
84
  tsBinary.factory.createStringLiteral(result)
@@ -77,7 +88,7 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
77
88
  const importPathWithQuotes = node?.moduleSpecifier?.getText();
78
89
  if (!importPathWithQuotes) return node;
79
90
  const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
80
- const result = getNotAliasedPath(sf, matchPath, text);
91
+ const result = getNotAliasedPath(sf, matchPath, text, moduleType);
81
92
  if (!result) return node;
82
93
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
83
94
  moduleSpecifier.parent = node.moduleSpecifier.parent;
@@ -93,13 +104,9 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
93
104
  return tsBinary.visitNode(sf, visitNode);
94
105
  };
95
106
  }
96
- function getNotAliasedPath(sf, matcher, text) {
97
- let result = matcher(text, void 0, void 0, [
98
- '.ts',
99
- '.tsx',
100
- '.js',
101
- '.jsx'
102
- ]);
107
+ function getNotAliasedPath(sf, matcher, text, moduleType) {
108
+ let result = findMatchedSourcePath(matcher, text);
109
+ if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
103
110
  if (!result) return;
104
111
  if ('win32' === __rspack_external_os.platform()) result = result.replace(/\\/g, '/');
105
112
  if (!path.isAbsolute(result)) {
@@ -122,6 +129,7 @@ function getNotAliasedPath(sf, matcher, text) {
122
129
  if (packagePath) return text;
123
130
  } catch {}
124
131
  }
132
+ if ('module' === moduleType) result = toEsmOutputPath(result);
125
133
  const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
126
134
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
127
135
  }
@@ -1,2 +1,2 @@
1
1
  import * as ts from 'typescript';
2
- export declare function tsconfigPathsBeforeHookFactory(tsBinary: typeof ts, baseUrl: string, paths: Record<string, string[] | string>): ((ctx: ts.TransformationContext) => ts.Transformer<any>) | undefined;
2
+ export declare function tsconfigPathsBeforeHookFactory(tsBinary: typeof ts, baseUrl: string, paths: Record<string, string[] | string>, moduleType?: 'module' | 'commonjs'): ((ctx: ts.TransformationContext) => ts.Transformer<any>) | undefined;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.1.3",
18
+ "version": "3.1.4",
19
19
  "types": "./dist/types/index.d.ts",
20
20
  "main": "./dist/cjs/index.js",
21
21
  "exports": {
@@ -38,15 +38,15 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@swc/helpers": "^0.5.17",
41
- "@modern-js/utils": "3.1.3"
41
+ "@modern-js/utils": "3.1.4"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@rslib/core": "0.21.0",
45
45
  "@types/node": "^20",
46
46
  "typescript": "^5",
47
47
  "@modern-js/rslib": "2.68.10",
48
- "@modern-js/server-core": "3.1.3",
49
- "@scripts/rstest-config": "2.66.0"
48
+ "@scripts/rstest-config": "2.66.0",
49
+ "@modern-js/server-core": "3.1.4"
50
50
  },
51
51
  "sideEffects": false,
52
52
  "publishConfig": {