@modern-js/server-utils 1.15.1-beta.2 → 1.18.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,59 @@
1
1
  # @modern-js/server-utils
2
2
 
3
+ ## 1.18.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 2b7406d: feat: use typescript instead of babel as typescript compiler in server
8
+ feat: 服务端,增加 typescript 作为 typescipt 编译器
9
+ - 0a4d622: fix: compile ts with dynamic import should replace to correct alias name
10
+ fix: 编译 ts 时,动态导入的路径应该替换为正确的别名
11
+ - 60a2e3a: feat: add compiler option for server
12
+ feat: 为 server 添加编译选项
13
+ - Updated dependencies [8280920]
14
+ - Updated dependencies [5227370]
15
+ - Updated dependencies [7928bae]
16
+ - @modern-js/utils@1.18.0
17
+ - @modern-js/babel-preset-lib@1.18.0
18
+ - @modern-js/babel-compiler@1.18.0
19
+ - @modern-js/plugin@1.18.0
20
+
21
+ ## 1.17.0
22
+
23
+ ### Patch Changes
24
+
25
+ - Updated dependencies [1b9176f]
26
+ - Updated dependencies [77d3a38]
27
+ - Updated dependencies [151329d]
28
+ - Updated dependencies [5af9472]
29
+ - Updated dependencies [6b6a534]
30
+ - Updated dependencies [6b43a2b]
31
+ - Updated dependencies [a7be124]
32
+ - Updated dependencies [31547b4]
33
+ - @modern-js/utils@1.17.0
34
+ - @modern-js/babel-preset-lib@1.17.0
35
+ - @modern-js/plugin@1.17.0
36
+
37
+ ## 1.16.0
38
+
39
+ ### Minor Changes
40
+
41
+ - 1100dd58c: chore: support react 18
42
+
43
+ chore: 支持 React 18
44
+
45
+ ### Patch Changes
46
+
47
+ - Updated dependencies [641592f52]
48
+ - Updated dependencies [3904b30a5]
49
+ - Updated dependencies [1100dd58c]
50
+ - Updated dependencies [e04e6e76a]
51
+ - Updated dependencies [81c66e4a4]
52
+ - Updated dependencies [2c305b6f5]
53
+ - @modern-js/utils@1.16.0
54
+ - @modern-js/babel-preset-lib@1.16.0
55
+ - @modern-js/plugin@1.16.0
56
+
3
57
  ## 1.15.0
4
58
 
5
59
  ### Patch Changes
@@ -15,6 +15,8 @@ const validateAbsolutePaths = (filenames, messageFunc) => {
15
15
  };
16
16
 
17
17
  export const compile = async (appDirectory, modernConfig, compileOptions) => {
18
+ var _modernConfig$server;
19
+
18
20
  const {
19
21
  sourceDirs,
20
22
  distDir,
@@ -22,11 +24,12 @@ export const compile = async (appDirectory, modernConfig, compileOptions) => {
22
24
  } = compileOptions;
23
25
  validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
24
26
  validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
27
+ const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
25
28
  const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
26
29
 
27
- if (isTsProject) {
28
- await compileByTs(appDirectory, modernConfig, compileOptions);
29
- } else {
30
+ if (!isTsProject || compiler === 'babel') {
30
31
  await compileByBabel(appDirectory, modernConfig, compileOptions);
32
+ } else {
33
+ await compileByTs(appDirectory, modernConfig, compileOptions);
31
34
  }
32
35
  };
@@ -67,8 +67,9 @@ export const compileByTs = async (appDirectory, modernConfig, compileOptions) =>
67
67
  fileNames,
68
68
  projectReferences
69
69
  } = readTsConfigByFile(tsconfigPath);
70
+ const sourcePosixPaths = sourceDirs.map(sourceDir => sourceDir.split(path.sep).join(path.posix.sep));
70
71
  const rootNames = fileNames.filter(fileName => {
71
- return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
72
+ return fileName.endsWith('.d.ts') || sourcePosixPaths.some(sourceDir => {
72
73
  return fileName.includes(sourceDir);
73
74
  });
74
75
  });
@@ -1,5 +1,6 @@
1
1
  import * as os from 'os';
2
2
  import path, { dirname, posix } from 'path';
3
+ import * as ts from 'typescript';
3
4
  import { createMatchPath } from 'tsconfig-paths';
4
5
 
5
6
  const isRegExpKey = str => {
@@ -51,6 +52,10 @@ const createAliasMatcher = (baseUrl, alias) => {
51
52
  };
52
53
  };
53
54
 
55
+ const isDynamicImport = (tsBinary, node) => {
56
+ return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
57
+ };
58
+
54
59
  export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
55
60
  const tsPaths = {};
56
61
  const alias = {};
@@ -81,6 +86,18 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
81
86
  return ctx => {
82
87
  return sf => {
83
88
  const visitNode = node => {
89
+ if (isDynamicImport(tsBinary, node)) {
90
+ const importPathWithQuotes = node.arguments[0].getText(sf);
91
+ const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
92
+ const result = getNotAliasedPath(sf, matchPath, text);
93
+
94
+ if (!result) {
95
+ return node;
96
+ }
97
+
98
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
99
+ }
100
+
84
101
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
85
102
  try {
86
103
  var _node$moduleSpecifier;
@@ -31,6 +31,8 @@ const validateAbsolutePaths = (filenames, messageFunc) => {
31
31
  };
32
32
 
33
33
  const compile = async (appDirectory, modernConfig, compileOptions) => {
34
+ var _modernConfig$server;
35
+
34
36
  const {
35
37
  sourceDirs,
36
38
  distDir,
@@ -38,12 +40,13 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
38
40
  } = compileOptions;
39
41
  validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
40
42
  validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
43
+ const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
41
44
  const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
42
45
 
43
- if (isTsProject) {
44
- await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
45
- } else {
46
+ if (!isTsProject || compiler === 'babel') {
46
47
  await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
48
+ } else {
49
+ await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
47
50
  }
48
51
  };
49
52
 
@@ -84,8 +84,9 @@ const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
84
84
  fileNames,
85
85
  projectReferences
86
86
  } = readTsConfigByFile(tsconfigPath);
87
+ const sourcePosixPaths = sourceDirs.map(sourceDir => sourceDir.split(_path.default.sep).join(_path.default.posix.sep));
87
88
  const rootNames = fileNames.filter(fileName => {
88
- return fileName.endsWith('.d.ts') || sourceDirs.some(sourceDir => {
89
+ return fileName.endsWith('.d.ts') || sourcePosixPaths.some(sourceDir => {
89
90
  return fileName.includes(sourceDir);
90
91
  });
91
92
  });
@@ -9,6 +9,8 @@ var os = _interopRequireWildcard(require("os"));
9
9
 
10
10
  var _path = _interopRequireWildcard(require("path"));
11
11
 
12
+ var ts = _interopRequireWildcard(require("typescript"));
13
+
12
14
  var _tsconfigPaths = require("tsconfig-paths");
13
15
 
14
16
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
@@ -64,6 +66,10 @@ const createAliasMatcher = (baseUrl, alias) => {
64
66
  };
65
67
  };
66
68
 
69
+ const isDynamicImport = (tsBinary, node) => {
70
+ return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
71
+ };
72
+
67
73
  function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
68
74
  const tsPaths = {};
69
75
  const alias = {};
@@ -94,6 +100,18 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
94
100
  return ctx => {
95
101
  return sf => {
96
102
  const visitNode = node => {
103
+ if (isDynamicImport(tsBinary, node)) {
104
+ const importPathWithQuotes = node.arguments[0].getText(sf);
105
+ const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
106
+ const result = getNotAliasedPath(sf, matchPath, text);
107
+
108
+ if (!result) {
109
+ return node;
110
+ }
111
+
112
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
113
+ }
114
+
97
115
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
98
116
  try {
99
117
  var _node$moduleSpecifier;
@@ -20,7 +20,9 @@ var validateAbsolutePaths = function validateAbsolutePaths(filenames, messageFun
20
20
 
21
21
  export var compile = /*#__PURE__*/function () {
22
22
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
23
- var sourceDirs, distDir, tsconfigPath, isTsProject;
23
+ var _modernConfig$server;
24
+
25
+ var sourceDirs, distDir, tsconfigPath, compiler, isTsProject;
24
26
  return _regeneratorRuntime().wrap(function _callee$(_context) {
25
27
  while (1) {
26
28
  switch (_context.prev = _context.next) {
@@ -30,39 +32,40 @@ export var compile = /*#__PURE__*/function () {
30
32
  return "source dir ".concat(dir, " is not an absolute path.");
31
33
  });
32
34
  validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
35
+ compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
33
36
  _context.t0 = tsconfigPath;
34
37
 
35
38
  if (!_context.t0) {
36
- _context.next = 8;
39
+ _context.next = 9;
37
40
  break;
38
41
  }
39
42
 
40
- _context.next = 7;
43
+ _context.next = 8;
41
44
  return fs.pathExists(tsconfigPath);
42
45
 
43
- case 7:
46
+ case 8:
44
47
  _context.t0 = _context.sent;
45
48
 
46
- case 8:
49
+ case 9:
47
50
  isTsProject = _context.t0;
48
51
 
49
- if (!isTsProject) {
50
- _context.next = 14;
52
+ if (!(!isTsProject || compiler === 'babel')) {
53
+ _context.next = 15;
51
54
  break;
52
55
  }
53
56
 
54
- _context.next = 12;
55
- return compileByTs(appDirectory, modernConfig, compileOptions);
57
+ _context.next = 13;
58
+ return compileByBabel(appDirectory, modernConfig, compileOptions);
56
59
 
57
- case 12:
58
- _context.next = 16;
60
+ case 13:
61
+ _context.next = 17;
59
62
  break;
60
63
 
61
- case 14:
62
- _context.next = 16;
63
- return compileByBabel(appDirectory, modernConfig, compileOptions);
64
+ case 15:
65
+ _context.next = 17;
66
+ return compileByTs(appDirectory, modernConfig, compileOptions);
64
67
 
65
- case 16:
68
+ case 17:
66
69
  case "end":
67
70
  return _context.stop();
68
71
  }
@@ -61,7 +61,7 @@ var copyFiles = /*#__PURE__*/function () {
61
61
 
62
62
  export var compileByTs = /*#__PURE__*/function () {
63
63
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
64
- var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iterator, _step, source;
64
+ var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iterator, _step, source;
65
65
 
66
66
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
67
  while (1) {
@@ -90,8 +90,11 @@ export var compileByTs = /*#__PURE__*/function () {
90
90
  });
91
91
  _aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
92
92
  _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
93
+ sourcePosixPaths = sourceDirs.map(function (sourceDir) {
94
+ return sourceDir.split(path.sep).join(path.posix.sep);
95
+ });
93
96
  rootNames = fileNames.filter(function (fileName) {
94
- return fileName.endsWith('.d.ts') || sourceDirs.some(function (sourceDir) {
97
+ return fileName.endsWith('.d.ts') || sourcePosixPaths.some(function (sourceDir) {
95
98
  return fileName.includes(sourceDir);
96
99
  });
97
100
  });
@@ -116,47 +119,47 @@ export var compileByTs = /*#__PURE__*/function () {
116
119
  }
117
120
 
118
121
  _iterator = _createForOfIteratorHelper(sourceDirs);
119
- _context2.prev = 18;
122
+ _context2.prev = 19;
120
123
 
121
124
  _iterator.s();
122
125
 
123
- case 20:
126
+ case 21:
124
127
  if ((_step = _iterator.n()).done) {
125
- _context2.next = 26;
128
+ _context2.next = 27;
126
129
  break;
127
130
  }
128
131
 
129
132
  source = _step.value;
130
- _context2.next = 24;
133
+ _context2.next = 25;
131
134
  return copyFiles(source, distDir, tsconfigPath);
132
135
 
133
- case 24:
134
- _context2.next = 20;
136
+ case 25:
137
+ _context2.next = 21;
135
138
  break;
136
139
 
137
- case 26:
138
- _context2.next = 31;
140
+ case 27:
141
+ _context2.next = 32;
139
142
  break;
140
143
 
141
- case 28:
142
- _context2.prev = 28;
143
- _context2.t0 = _context2["catch"](18);
144
+ case 29:
145
+ _context2.prev = 29;
146
+ _context2.t0 = _context2["catch"](19);
144
147
 
145
148
  _iterator.e(_context2.t0);
146
149
 
147
- case 31:
148
- _context2.prev = 31;
150
+ case 32:
151
+ _context2.prev = 32;
149
152
 
150
153
  _iterator.f();
151
154
 
152
- return _context2.finish(31);
155
+ return _context2.finish(32);
153
156
 
154
- case 34:
157
+ case 35:
155
158
  case "end":
156
159
  return _context2.stop();
157
160
  }
158
161
  }
159
- }, _callee2, null, [[18, 28, 31, 34]]);
162
+ }, _callee2, null, [[19, 29, 32, 35]]);
160
163
  }));
161
164
 
162
165
  return function compileByTs(_x4, _x5, _x6) {
@@ -3,6 +3,7 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
4
4
  import * as os from 'os';
5
5
  import path, { dirname, posix } from 'path';
6
+ import * as ts from 'typescript';
6
7
  import { createMatchPath } from 'tsconfig-paths';
7
8
 
8
9
  var isRegExpKey = function isRegExpKey(str) {
@@ -68,6 +69,10 @@ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
68
69
  };
69
70
  };
70
71
 
72
+ var isDynamicImport = function isDynamicImport(tsBinary, node) {
73
+ return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
74
+ };
75
+
71
76
  export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
72
77
  var tsPaths = {};
73
78
  var alias = {};
@@ -98,24 +103,37 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
98
103
  return function (ctx) {
99
104
  return function (sf) {
100
105
  var visitNode = function visitNode(node) {
106
+ if (isDynamicImport(tsBinary, node)) {
107
+ var importPathWithQuotes = node.arguments[0].getText(sf);
108
+ var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
109
+ var result = getNotAliasedPath(sf, matchPath, text);
110
+
111
+ if (!result) {
112
+ return node;
113
+ }
114
+
115
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
116
+ }
117
+
101
118
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
102
119
  try {
103
120
  var _node$moduleSpecifier;
104
121
 
105
- var importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
122
+ var _importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
106
123
 
107
- if (!importPathWithQuotes) {
124
+ if (!_importPathWithQuotes) {
108
125
  return node;
109
126
  }
110
127
 
111
- var text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
112
- var result = getNotAliasedPath(sf, matchPath, text);
128
+ var _text = _importPathWithQuotes.substring(1, _importPathWithQuotes.length - 1);
129
+
130
+ var _result = getNotAliasedPath(sf, matchPath, _text);
113
131
 
114
- if (!result) {
132
+ if (!_result) {
115
133
  return node;
116
134
  }
117
135
 
118
- var moduleSpecifier = tsBinary.factory.createStringLiteral(result);
136
+ var moduleSpecifier = tsBinary.factory.createStringLiteral(_result);
119
137
  moduleSpecifier.parent = node.moduleSpecifier.parent;
120
138
 
121
139
  if (tsBinary.isImportDeclaration(node)) {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.15.1-beta.2",
14
+ "version": "1.18.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -35,23 +35,21 @@
35
35
  "@babel/preset-env": "^7.18.0",
36
36
  "@babel/preset-typescript": "^7.17.12",
37
37
  "@babel/runtime": "^7.18.0",
38
- "@modern-js/babel-compiler": "^1.15.0",
39
- "@modern-js/babel-preset-lib": "1.15.0",
40
- "@modern-js/plugin": "1.15.0",
41
- "@modern-js/utils": "1.15.0",
38
+ "@modern-js/babel-compiler": "^1.18.0",
39
+ "@modern-js/babel-preset-lib": "1.18.0",
40
+ "@modern-js/plugin": "1.18.0",
41
+ "@modern-js/utils": "1.18.0",
42
42
  "babel-plugin-module-resolver": "^4.1.0",
43
43
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
44
44
  "tsconfig-paths": "3.14.1"
45
45
  },
46
46
  "devDependencies": {
47
- "@modern-js/core": "1.15.0",
48
- "@scripts/build": "1.15.0",
49
- "@scripts/jest-config": "1.15.0",
47
+ "@modern-js/core": "1.18.0",
48
+ "@scripts/build": "1.18.0",
49
+ "@scripts/jest-config": "1.18.0",
50
50
  "@types/babel__core": "^7.1.15",
51
51
  "@types/jest": "^27",
52
52
  "@types/node": "^14",
53
- "@types/react": "^17",
54
- "@types/react-dom": "^17",
55
53
  "jest": "^27",
56
54
  "ts-jest": "^27.0.4",
57
55
  "typescript": "^4"
@@ -59,8 +57,7 @@
59
57
  "sideEffects": false,
60
58
  "publishConfig": {
61
59
  "registry": "https://registry.npmjs.org/",
62
- "access": "public",
63
- "types": "./dist/types/index.d.ts"
60
+ "access": "public"
64
61
  },
65
62
  "wireit": {
66
63
  "build": {