@modern-js/server-utils 3.8.1 → 3.8.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.
@@ -96,9 +96,13 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
96
96
  }
97
97
  });
98
98
  const tsconfigPathsPlugin = (0, external_tsconfigPathsPlugin_js_namespaceObject.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
99
+ const tsconfigPathsDeclarationPlugin = (0, external_tsconfigPathsPlugin_js_namespaceObject.tsconfigPathsAfterDeclarationsHookFactory)(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
99
100
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
100
101
  before: tsconfigPathsPlugin ? [
101
102
  tsconfigPathsPlugin
103
+ ] : [],
104
+ afterDeclarations: tsconfigPathsDeclarationPlugin ? [
105
+ tsconfigPathsDeclarationPlugin
102
106
  ] : []
103
107
  });
104
108
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
@@ -37,6 +37,7 @@ var __webpack_require__ = {};
37
37
  var __webpack_exports__ = {};
38
38
  __webpack_require__.r(__webpack_exports__);
39
39
  __webpack_require__.d(__webpack_exports__, {
40
+ tsconfigPathsAfterDeclarationsHookFactory: ()=>tsconfigPathsAfterDeclarationsHookFactory,
40
41
  tsconfigPathsBeforeHookFactory: ()=>tsconfigPathsBeforeHookFactory
41
42
  });
42
43
  const external_os_namespaceObject = require("os");
@@ -104,7 +105,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
104
105
  };
105
106
  };
106
107
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === external_typescript_namespaceObject.SyntaxKind.ImportKeyword;
107
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
108
+ const createTsMatchPath = (baseUrl, paths)=>{
108
109
  const tsPaths = {};
109
110
  const alias = {};
110
111
  Object.keys(paths).forEach((key)=>{
@@ -115,11 +116,14 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
115
116
  const matchTsPath = (0, tsconfig_paths_namespaceObject.createMatchPath)(baseUrl, tsPaths, [
116
117
  'main'
117
118
  ]);
118
- const matchPath = (requestedModule, readJSONSync, fileExists, extensions)=>{
119
+ return (requestedModule, readJSONSync, fileExists, extensions)=>{
119
120
  const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
120
121
  if (result) return result;
121
122
  return matchAliasPath(requestedModule);
122
123
  };
124
+ };
125
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
126
+ const matchPath = createTsMatchPath(baseUrl, paths);
123
127
  if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
124
128
  return (ctx)=>(sf)=>{
125
129
  const visitNode = (node)=>{
@@ -153,6 +157,45 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
153
157
  return tsBinary.visitNode(sf, visitNode);
154
158
  };
155
159
  }
160
+ function tsconfigPathsAfterDeclarationsHookFactory(tsBinary, baseUrl, paths, moduleType) {
161
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
162
+ const matchPath = createTsMatchPath(baseUrl, paths);
163
+ const rewrite = (sf, text)=>getNotAliasedPath(sf, matchPath, text, moduleType);
164
+ return (ctx)=>{
165
+ const { factory } = ctx;
166
+ return (sourceFile)=>{
167
+ if (!tsBinary.isSourceFile(sourceFile)) return sourceFile;
168
+ const visitNode = (node)=>{
169
+ if (tsBinary.isImportTypeNode(node)) {
170
+ const { argument } = node;
171
+ if (tsBinary.isLiteralTypeNode(argument) && tsBinary.isStringLiteral(argument.literal)) {
172
+ const result = rewrite(sourceFile, argument.literal.text);
173
+ if (result) {
174
+ const updated = factory.updateImportTypeNode(node, factory.createLiteralTypeNode(factory.createStringLiteral(result)), node.attributes ?? node.assertions, node.qualifier, node.typeArguments, node.isTypeOf);
175
+ return tsBinary.visitEachChild(updated, visitNode, ctx);
176
+ }
177
+ }
178
+ return tsBinary.visitEachChild(node, visitNode, ctx);
179
+ }
180
+ if ((tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node)) && node.moduleSpecifier && tsBinary.isStringLiteral(node.moduleSpecifier)) {
181
+ const result = rewrite(sourceFile, node.moduleSpecifier.text);
182
+ if (!result) return node;
183
+ const moduleSpecifier = factory.createStringLiteral(result);
184
+ const importAttributes = node.attributes ?? node.assertClause;
185
+ if (tsBinary.isImportDeclaration(node)) return factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes);
186
+ return factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
187
+ }
188
+ if (tsBinary.isImportEqualsDeclaration(node) && tsBinary.isExternalModuleReference(node.moduleReference) && tsBinary.isStringLiteral(node.moduleReference.expression)) {
189
+ const result = rewrite(sourceFile, node.moduleReference.expression.text);
190
+ if (!result) return node;
191
+ return factory.updateImportEqualsDeclaration(node, node.modifiers, node.isTypeOnly, node.name, factory.createExternalModuleReference(factory.createStringLiteral(result)));
192
+ }
193
+ return tsBinary.visitEachChild(node, visitNode, ctx);
194
+ };
195
+ return tsBinary.visitEachChild(sourceFile, visitNode, ctx);
196
+ };
197
+ };
198
+ }
156
199
  function getNotAliasedPath(sf, matcher, text, moduleType) {
157
200
  let result = (0, utils_namespaceObject.findMatchedSourcePath)(matcher, text);
158
201
  if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
@@ -182,8 +225,10 @@ function getNotAliasedPath(sf, matcher, text, moduleType) {
182
225
  const resolvedPath = external_path_namespaceObject.posix.relative((0, external_path_namespaceObject.dirname)(sf.fileName), result) || './';
183
226
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
184
227
  }
228
+ exports.tsconfigPathsAfterDeclarationsHookFactory = __webpack_exports__.tsconfigPathsAfterDeclarationsHookFactory;
185
229
  exports.tsconfigPathsBeforeHookFactory = __webpack_exports__.tsconfigPathsBeforeHookFactory;
186
230
  for(var __rspack_i in __webpack_exports__)if (-1 === [
231
+ "tsconfigPathsAfterDeclarationsHookFactory",
187
232
  "tsconfigPathsBeforeHookFactory"
188
233
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
189
234
  Object.defineProperty(exports, '__esModule', {
@@ -1,6 +1,6 @@
1
1
  import path_0 from "path";
2
2
  import { fs, getAliasConfig, logger } from "@modern-js/utils";
3
- import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin.mjs";
3
+ import { tsconfigPathsAfterDeclarationsHookFactory, tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin.mjs";
4
4
  import { TypescriptLoader } from "./typescriptLoader.mjs";
5
5
  const readTsConfigByFile = (tsConfigFile, tsInstance)=>{
6
6
  const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
@@ -54,9 +54,13 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
54
54
  }
55
55
  });
56
56
  const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
57
+ const tsconfigPathsDeclarationPlugin = tsconfigPathsAfterDeclarationsHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
57
58
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
58
59
  before: tsconfigPathsPlugin ? [
59
60
  tsconfigPathsPlugin
61
+ ] : [],
62
+ afterDeclarations: tsconfigPathsDeclarationPlugin ? [
63
+ tsconfigPathsDeclarationPlugin
60
64
  ] : []
61
65
  });
62
66
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
@@ -62,7 +62,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
62
62
  };
63
63
  };
64
64
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === __rspack_external_typescript.SyntaxKind.ImportKeyword;
65
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
65
+ const createTsMatchPath = (baseUrl, paths)=>{
66
66
  const tsPaths = {};
67
67
  const alias = {};
68
68
  Object.keys(paths).forEach((key)=>{
@@ -73,11 +73,14 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
73
73
  const matchTsPath = createMatchPath(baseUrl, tsPaths, [
74
74
  'main'
75
75
  ]);
76
- const matchPath = (requestedModule, readJSONSync, fileExists, extensions)=>{
76
+ return (requestedModule, readJSONSync, fileExists, extensions)=>{
77
77
  const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
78
78
  if (result) return result;
79
79
  return matchAliasPath(requestedModule);
80
80
  };
81
+ };
82
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
83
+ const matchPath = createTsMatchPath(baseUrl, paths);
81
84
  if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
82
85
  return (ctx)=>(sf)=>{
83
86
  const visitNode = (node)=>{
@@ -111,6 +114,45 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
111
114
  return tsBinary.visitNode(sf, visitNode);
112
115
  };
113
116
  }
117
+ function tsconfigPathsAfterDeclarationsHookFactory(tsBinary, baseUrl, paths, moduleType) {
118
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
119
+ const matchPath = createTsMatchPath(baseUrl, paths);
120
+ const rewrite = (sf, text)=>getNotAliasedPath(sf, matchPath, text, moduleType);
121
+ return (ctx)=>{
122
+ const { factory } = ctx;
123
+ return (sourceFile)=>{
124
+ if (!tsBinary.isSourceFile(sourceFile)) return sourceFile;
125
+ const visitNode = (node)=>{
126
+ if (tsBinary.isImportTypeNode(node)) {
127
+ const { argument } = node;
128
+ if (tsBinary.isLiteralTypeNode(argument) && tsBinary.isStringLiteral(argument.literal)) {
129
+ const result = rewrite(sourceFile, argument.literal.text);
130
+ if (result) {
131
+ const updated = factory.updateImportTypeNode(node, factory.createLiteralTypeNode(factory.createStringLiteral(result)), node.attributes ?? node.assertions, node.qualifier, node.typeArguments, node.isTypeOf);
132
+ return tsBinary.visitEachChild(updated, visitNode, ctx);
133
+ }
134
+ }
135
+ return tsBinary.visitEachChild(node, visitNode, ctx);
136
+ }
137
+ if ((tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node)) && node.moduleSpecifier && tsBinary.isStringLiteral(node.moduleSpecifier)) {
138
+ const result = rewrite(sourceFile, node.moduleSpecifier.text);
139
+ if (!result) return node;
140
+ const moduleSpecifier = factory.createStringLiteral(result);
141
+ const importAttributes = node.attributes ?? node.assertClause;
142
+ if (tsBinary.isImportDeclaration(node)) return factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes);
143
+ return factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
144
+ }
145
+ if (tsBinary.isImportEqualsDeclaration(node) && tsBinary.isExternalModuleReference(node.moduleReference) && tsBinary.isStringLiteral(node.moduleReference.expression)) {
146
+ const result = rewrite(sourceFile, node.moduleReference.expression.text);
147
+ if (!result) return node;
148
+ return factory.updateImportEqualsDeclaration(node, node.modifiers, node.isTypeOnly, node.name, factory.createExternalModuleReference(factory.createStringLiteral(result)));
149
+ }
150
+ return tsBinary.visitEachChild(node, visitNode, ctx);
151
+ };
152
+ return tsBinary.visitEachChild(sourceFile, visitNode, ctx);
153
+ };
154
+ };
155
+ }
114
156
  function getNotAliasedPath(sf, matcher, text, moduleType) {
115
157
  let result = findMatchedSourcePath(matcher, text);
116
158
  if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
@@ -140,4 +182,4 @@ function getNotAliasedPath(sf, matcher, text, moduleType) {
140
182
  const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
141
183
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
142
184
  }
143
- export { tsconfigPathsBeforeHookFactory };
185
+ export { tsconfigPathsAfterDeclarationsHookFactory, tsconfigPathsBeforeHookFactory };
@@ -1,7 +1,7 @@
1
1
  import "node:module";
2
2
  import path_0 from "path";
3
3
  import { fs, getAliasConfig, logger } from "@modern-js/utils";
4
- import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin.mjs";
4
+ import { tsconfigPathsAfterDeclarationsHookFactory, tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin.mjs";
5
5
  import { TypescriptLoader } from "./typescriptLoader.mjs";
6
6
  const readTsConfigByFile = (tsConfigFile, tsInstance)=>{
7
7
  const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
@@ -55,9 +55,13 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
55
55
  }
56
56
  });
57
57
  const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
58
+ const tsconfigPathsDeclarationPlugin = tsconfigPathsAfterDeclarationsHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
58
59
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
59
60
  before: tsconfigPathsPlugin ? [
60
61
  tsconfigPathsPlugin
62
+ ] : [],
63
+ afterDeclarations: tsconfigPathsDeclarationPlugin ? [
64
+ tsconfigPathsDeclarationPlugin
61
65
  ] : []
62
66
  });
63
67
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
@@ -64,7 +64,7 @@ const createAliasMatcher = (baseUrl, alias)=>{
64
64
  };
65
65
  };
66
66
  const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === __rspack_external_typescript.SyntaxKind.ImportKeyword;
67
- function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
67
+ const createTsMatchPath = (baseUrl, paths)=>{
68
68
  const tsPaths = {};
69
69
  const alias = {};
70
70
  Object.keys(paths).forEach((key)=>{
@@ -75,11 +75,14 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
75
75
  const matchTsPath = createMatchPath(baseUrl, tsPaths, [
76
76
  'main'
77
77
  ]);
78
- const matchPath = (requestedModule, readJSONSync, fileExists, extensions)=>{
78
+ return (requestedModule, readJSONSync, fileExists, extensions)=>{
79
79
  const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
80
80
  if (result) return result;
81
81
  return matchAliasPath(requestedModule);
82
82
  };
83
+ };
84
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
85
+ const matchPath = createTsMatchPath(baseUrl, paths);
83
86
  if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
84
87
  return (ctx)=>(sf)=>{
85
88
  const visitNode = (node)=>{
@@ -113,6 +116,45 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
113
116
  return tsBinary.visitNode(sf, visitNode);
114
117
  };
115
118
  }
119
+ function tsconfigPathsAfterDeclarationsHookFactory(tsBinary, baseUrl, paths, moduleType) {
120
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
121
+ const matchPath = createTsMatchPath(baseUrl, paths);
122
+ const rewrite = (sf, text)=>getNotAliasedPath(sf, matchPath, text, moduleType);
123
+ return (ctx)=>{
124
+ const { factory } = ctx;
125
+ return (sourceFile)=>{
126
+ if (!tsBinary.isSourceFile(sourceFile)) return sourceFile;
127
+ const visitNode = (node)=>{
128
+ if (tsBinary.isImportTypeNode(node)) {
129
+ const { argument } = node;
130
+ if (tsBinary.isLiteralTypeNode(argument) && tsBinary.isStringLiteral(argument.literal)) {
131
+ const result = rewrite(sourceFile, argument.literal.text);
132
+ if (result) {
133
+ const updated = factory.updateImportTypeNode(node, factory.createLiteralTypeNode(factory.createStringLiteral(result)), node.attributes ?? node.assertions, node.qualifier, node.typeArguments, node.isTypeOf);
134
+ return tsBinary.visitEachChild(updated, visitNode, ctx);
135
+ }
136
+ }
137
+ return tsBinary.visitEachChild(node, visitNode, ctx);
138
+ }
139
+ if ((tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node)) && node.moduleSpecifier && tsBinary.isStringLiteral(node.moduleSpecifier)) {
140
+ const result = rewrite(sourceFile, node.moduleSpecifier.text);
141
+ if (!result) return node;
142
+ const moduleSpecifier = factory.createStringLiteral(result);
143
+ const importAttributes = node.attributes ?? node.assertClause;
144
+ if (tsBinary.isImportDeclaration(node)) return factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes);
145
+ return factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
146
+ }
147
+ if (tsBinary.isImportEqualsDeclaration(node) && tsBinary.isExternalModuleReference(node.moduleReference) && tsBinary.isStringLiteral(node.moduleReference.expression)) {
148
+ const result = rewrite(sourceFile, node.moduleReference.expression.text);
149
+ if (!result) return node;
150
+ return factory.updateImportEqualsDeclaration(node, node.modifiers, node.isTypeOnly, node.name, factory.createExternalModuleReference(factory.createStringLiteral(result)));
151
+ }
152
+ return tsBinary.visitEachChild(node, visitNode, ctx);
153
+ };
154
+ return tsBinary.visitEachChild(sourceFile, visitNode, ctx);
155
+ };
156
+ };
157
+ }
116
158
  function getNotAliasedPath(sf, matcher, text, moduleType) {
117
159
  let result = findMatchedSourcePath(matcher, text);
118
160
  if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sf, text);
@@ -142,4 +184,4 @@ function getNotAliasedPath(sf, matcher, text, moduleType) {
142
184
  const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
143
185
  return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
144
186
  }
145
- export { tsconfigPathsBeforeHookFactory };
187
+ export { tsconfigPathsAfterDeclarationsHookFactory, tsconfigPathsBeforeHookFactory };
@@ -1,2 +1,3 @@
1
1
  import * as ts from 'typescript';
2
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;
3
+ export declare function tsconfigPathsAfterDeclarationsHookFactory(tsBinary: typeof ts, baseUrl: string, paths: Record<string, string[] | string>, moduleType?: 'module' | 'commonjs'): ((ctx: ts.TransformationContext) => ts.Transformer<ts.SourceFile | ts.Bundle>) | undefined;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.8.1",
18
+ "version": "3.8.2",
19
19
  "types": "./dist/types/index.d.ts",
20
20
  "main": "./dist/cjs/index.js",
21
21
  "exports": {
@@ -38,14 +38,14 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@swc/helpers": "^0.5.17",
41
- "@modern-js/utils": "3.8.1"
41
+ "@modern-js/utils": "3.8.2"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@rslib/core": "0.23.2",
45
45
  "@types/node": "^20",
46
46
  "typescript": "^5",
47
47
  "@modern-js/rslib": "2.68.10",
48
- "@modern-js/server-core": "3.8.1",
48
+ "@modern-js/server-core": "3.8.2",
49
49
  "@scripts/rstest-config": "2.66.0"
50
50
  },
51
51
  "sideEffects": false,