@modern-js/server-utils 3.7.0 → 3.8.1

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.
@@ -60,7 +60,9 @@ const copyFiles = async (from, to, appDirectory)=>{
60
60
  await utils_namespaceObject.fs.copy(from, targetDir, {
61
61
  filter: (src)=>![
62
62
  '.ts',
63
- '.js'
63
+ '.tsx',
64
+ '.js',
65
+ '.jsx'
64
66
  ].includes(external_path_default().extname(src)) && !src.endsWith('tsconfig.json')
65
67
  });
66
68
  }
@@ -89,14 +91,15 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
89
91
  options: {
90
92
  ...options,
91
93
  rootDir: appDirectory,
92
- outDir: distDir
94
+ outDir: distDir,
95
+ jsx: void 0 === options.jsx || options.jsx === ts.JsxEmit.Preserve ? ts.JsxEmit.ReactJSX : options.jsx
93
96
  }
94
97
  });
95
98
  const tsconfigPathsPlugin = (0, external_tsconfigPathsPlugin_js_namespaceObject.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
96
99
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
97
- before: [
100
+ before: tsconfigPathsPlugin ? [
98
101
  tsconfigPathsPlugin
99
- ]
102
+ ] : []
100
103
  });
101
104
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
102
105
  const { noEmitOnError } = options;
@@ -45,10 +45,18 @@ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_na
45
45
  const utils_namespaceObject = require("@modern-js/utils");
46
46
  const tsconfig_paths_namespaceObject = require("@modern-js/utils/tsconfig-paths");
47
47
  const external_typescript_namespaceObject = require("typescript");
48
+ const COMPILED_TO_JS_EXTENSIONS = new Set([
49
+ '.ts',
50
+ '.tsx',
51
+ '.js',
52
+ '.jsx'
53
+ ]);
48
54
  const toEsmOutputPath = (resolvedPath)=>{
49
- const sourcePath = (0, utils_namespaceObject.findSourceEntry)(resolvedPath) || resolvedPath;
55
+ const sourcePath = ((0, utils_namespaceObject.findSourceEntry)(resolvedPath) || resolvedPath).replace(/\\/g, '/');
50
56
  const ext = external_path_default().extname(sourcePath);
51
- return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
57
+ if (!ext) return `${sourcePath}.js`;
58
+ if (!COMPILED_TO_JS_EXTENSIONS.has(ext)) return sourcePath;
59
+ return `${sourcePath.slice(0, -ext.length)}.js`;
52
60
  };
53
61
  const resolveRelativeEsmSpecifier = (sf, text)=>{
54
62
  if (!text.startsWith('./') && !text.startsWith('../')) return;
@@ -112,28 +120,29 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
112
120
  if (result) return result;
113
121
  return matchAliasPath(requestedModule);
114
122
  };
115
- if (0 === Object.keys(paths).length) return;
123
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
116
124
  return (ctx)=>(sf)=>{
117
125
  const visitNode = (node)=>{
118
126
  if (isDynamicImport(tsBinary, node)) {
119
- const importPathWithQuotes = node.arguments[0].getText(sf);
120
- const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
121
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
127
+ const [specifier] = node.arguments;
128
+ if (!specifier || !(tsBinary.isStringLiteral(specifier) || tsBinary.isNoSubstitutionTemplateLiteral(specifier))) return tsBinary.visitEachChild(node, visitNode, ctx);
129
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
122
130
  if (!result) return node;
123
131
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
124
- tsBinary.factory.createStringLiteral(result)
132
+ tsBinary.factory.createStringLiteral(result),
133
+ ...node.arguments.slice(1)
125
134
  ]));
126
135
  }
127
136
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) try {
128
- const importPathWithQuotes = node?.moduleSpecifier?.getText();
129
- if (!importPathWithQuotes) return node;
130
- const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
131
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
137
+ const { moduleSpecifier: specifier } = node;
138
+ if (!specifier || !tsBinary.isStringLiteral(specifier)) return node;
139
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
132
140
  if (!result) return node;
133
141
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
134
142
  moduleSpecifier.parent = node.moduleSpecifier.parent;
143
+ const importAttributes = node.attributes ?? node.assertClause;
135
144
  let newNode;
136
- newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
145
+ newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
137
146
  newNode.flags = node.flags;
138
147
  return newNode;
139
148
  } catch {
@@ -18,7 +18,9 @@ const copyFiles = async (from, to, appDirectory)=>{
18
18
  await fs.copy(from, targetDir, {
19
19
  filter: (src)=>![
20
20
  '.ts',
21
- '.js'
21
+ '.tsx',
22
+ '.js',
23
+ '.jsx'
22
24
  ].includes(path_0.extname(src)) && !src.endsWith('tsconfig.json')
23
25
  });
24
26
  }
@@ -47,14 +49,15 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
47
49
  options: {
48
50
  ...options,
49
51
  rootDir: appDirectory,
50
- outDir: distDir
52
+ outDir: distDir,
53
+ jsx: void 0 === options.jsx || options.jsx === ts.JsxEmit.Preserve ? ts.JsxEmit.ReactJSX : options.jsx
51
54
  }
52
55
  });
53
56
  const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
54
57
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
55
- before: [
58
+ before: tsconfigPathsPlugin ? [
56
59
  tsconfigPathsPlugin
57
- ]
60
+ ] : []
58
61
  });
59
62
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
60
63
  const { noEmitOnError } = options;
@@ -3,10 +3,18 @@ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
3
3
  import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
4
4
  import * as __rspack_external_os from "os";
5
5
  import * as __rspack_external_typescript from "typescript";
6
+ const COMPILED_TO_JS_EXTENSIONS = new Set([
7
+ '.ts',
8
+ '.tsx',
9
+ '.js',
10
+ '.jsx'
11
+ ]);
6
12
  const toEsmOutputPath = (resolvedPath)=>{
7
- const sourcePath = findSourceEntry(resolvedPath) || resolvedPath;
13
+ const sourcePath = (findSourceEntry(resolvedPath) || resolvedPath).replace(/\\/g, '/');
8
14
  const ext = path.extname(sourcePath);
9
- return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
15
+ if (!ext) return `${sourcePath}.js`;
16
+ if (!COMPILED_TO_JS_EXTENSIONS.has(ext)) return sourcePath;
17
+ return `${sourcePath.slice(0, -ext.length)}.js`;
10
18
  };
11
19
  const resolveRelativeEsmSpecifier = (sf, text)=>{
12
20
  if (!text.startsWith('./') && !text.startsWith('../')) return;
@@ -70,28 +78,29 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
70
78
  if (result) return result;
71
79
  return matchAliasPath(requestedModule);
72
80
  };
73
- if (0 === Object.keys(paths).length) return;
81
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
74
82
  return (ctx)=>(sf)=>{
75
83
  const visitNode = (node)=>{
76
84
  if (isDynamicImport(tsBinary, node)) {
77
- const importPathWithQuotes = node.arguments[0].getText(sf);
78
- const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
79
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
85
+ const [specifier] = node.arguments;
86
+ if (!specifier || !(tsBinary.isStringLiteral(specifier) || tsBinary.isNoSubstitutionTemplateLiteral(specifier))) return tsBinary.visitEachChild(node, visitNode, ctx);
87
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
80
88
  if (!result) return node;
81
89
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
82
- tsBinary.factory.createStringLiteral(result)
90
+ tsBinary.factory.createStringLiteral(result),
91
+ ...node.arguments.slice(1)
83
92
  ]));
84
93
  }
85
94
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) try {
86
- const importPathWithQuotes = node?.moduleSpecifier?.getText();
87
- if (!importPathWithQuotes) return node;
88
- const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
89
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
95
+ const { moduleSpecifier: specifier } = node;
96
+ if (!specifier || !tsBinary.isStringLiteral(specifier)) return node;
97
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
90
98
  if (!result) return node;
91
99
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
92
100
  moduleSpecifier.parent = node.moduleSpecifier.parent;
101
+ const importAttributes = node.attributes ?? node.assertClause;
93
102
  let newNode;
94
- newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
103
+ newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
95
104
  newNode.flags = node.flags;
96
105
  return newNode;
97
106
  } catch {
@@ -19,7 +19,9 @@ const copyFiles = async (from, to, appDirectory)=>{
19
19
  await fs.copy(from, targetDir, {
20
20
  filter: (src)=>![
21
21
  '.ts',
22
- '.js'
22
+ '.tsx',
23
+ '.js',
24
+ '.jsx'
23
25
  ].includes(path_0.extname(src)) && !src.endsWith('tsconfig.json')
24
26
  });
25
27
  }
@@ -48,14 +50,15 @@ const compileByTs = async (appDirectory, config, compileOptions)=>{
48
50
  options: {
49
51
  ...options,
50
52
  rootDir: appDirectory,
51
- outDir: distDir
53
+ outDir: distDir,
54
+ jsx: void 0 === options.jsx || options.jsx === ts.JsxEmit.Preserve ? ts.JsxEmit.ReactJSX : options.jsx
52
55
  }
53
56
  });
54
57
  const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths, compileOptions.moduleType);
55
58
  const emitResult = program.emit(void 0, void 0, void 0, void 0, {
56
- before: [
59
+ before: tsconfigPathsPlugin ? [
57
60
  tsconfigPathsPlugin
58
- ]
61
+ ] : []
59
62
  });
60
63
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
61
64
  const { noEmitOnError } = options;
@@ -5,10 +5,18 @@ import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
5
5
  import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
6
6
  import * as __rspack_external_os from "os";
7
7
  import * as __rspack_external_typescript from "typescript";
8
+ const COMPILED_TO_JS_EXTENSIONS = new Set([
9
+ '.ts',
10
+ '.tsx',
11
+ '.js',
12
+ '.jsx'
13
+ ]);
8
14
  const toEsmOutputPath = (resolvedPath)=>{
9
- const sourcePath = findSourceEntry(resolvedPath) || resolvedPath;
15
+ const sourcePath = (findSourceEntry(resolvedPath) || resolvedPath).replace(/\\/g, '/');
10
16
  const ext = path.extname(sourcePath);
11
- return ext ? `${sourcePath.slice(0, -ext.length)}.js` : `${sourcePath}.js`;
17
+ if (!ext) return `${sourcePath}.js`;
18
+ if (!COMPILED_TO_JS_EXTENSIONS.has(ext)) return sourcePath;
19
+ return `${sourcePath.slice(0, -ext.length)}.js`;
12
20
  };
13
21
  const resolveRelativeEsmSpecifier = (sf, text)=>{
14
22
  if (!text.startsWith('./') && !text.startsWith('../')) return;
@@ -72,28 +80,29 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths, moduleType) {
72
80
  if (result) return result;
73
81
  return matchAliasPath(requestedModule);
74
82
  };
75
- if (0 === Object.keys(paths).length) return;
83
+ if (0 === Object.keys(paths).length && 'module' !== moduleType) return;
76
84
  return (ctx)=>(sf)=>{
77
85
  const visitNode = (node)=>{
78
86
  if (isDynamicImport(tsBinary, node)) {
79
- const importPathWithQuotes = node.arguments[0].getText(sf);
80
- const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
81
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
87
+ const [specifier] = node.arguments;
88
+ if (!specifier || !(tsBinary.isStringLiteral(specifier) || tsBinary.isNoSubstitutionTemplateLiteral(specifier))) return tsBinary.visitEachChild(node, visitNode, ctx);
89
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
82
90
  if (!result) return node;
83
91
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
84
- tsBinary.factory.createStringLiteral(result)
92
+ tsBinary.factory.createStringLiteral(result),
93
+ ...node.arguments.slice(1)
85
94
  ]));
86
95
  }
87
96
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) try {
88
- const importPathWithQuotes = node?.moduleSpecifier?.getText();
89
- if (!importPathWithQuotes) return node;
90
- const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
91
- const result = getNotAliasedPath(sf, matchPath, text, moduleType);
97
+ const { moduleSpecifier: specifier } = node;
98
+ if (!specifier || !tsBinary.isStringLiteral(specifier)) return node;
99
+ const result = getNotAliasedPath(sf, matchPath, specifier.text, moduleType);
92
100
  if (!result) return node;
93
101
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
94
102
  moduleSpecifier.parent = node.moduleSpecifier.parent;
103
+ const importAttributes = node.attributes ?? node.assertClause;
95
104
  let newNode;
96
- newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
105
+ newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, importAttributes) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, importAttributes);
97
106
  newNode.flags = node.flags;
98
107
  return newNode;
99
108
  } catch {
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "3.7.0",
18
+ "version": "3.8.1",
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.7.0"
41
+ "@modern-js/utils": "3.8.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@rslib/core": "0.23.2",
45
45
  "@types/node": "^20",
46
46
  "typescript": "^5",
47
- "@modern-js/server-core": "3.7.0",
48
47
  "@modern-js/rslib": "2.68.10",
48
+ "@modern-js/server-core": "3.8.1",
49
49
  "@scripts/rstest-config": "2.66.0"
50
50
  },
51
51
  "sideEffects": false,