@modern-js/server-utils 1.15.1-beta.1 → 1.15.1-beta.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.
@@ -107,8 +107,8 @@ export const compileByBabel = async (appDirectory, modernConfig, compileOptions)
107
107
 
108
108
  return compiler({
109
109
  rootDir: appDirectory,
110
- distDir: distDir,
111
- sourceDir: sourceDir,
110
+ distDir,
111
+ sourceDir,
112
112
  extensions: FILE_EXTENSIONS
113
113
  }, babelConfig);
114
114
  }));
@@ -6,9 +6,9 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
6
6
 
7
7
  import path from 'path';
8
8
  import { logger, getAlias, fs } from '@modern-js/utils';
9
+ import ts from 'typescript';
9
10
  import { TypescriptLoader } from "./typescript-loader";
10
11
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
11
- import ts from 'typescript';
12
12
 
13
13
  const readTsConfigByFile = tsConfigFile => {
14
14
  const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
@@ -41,7 +41,11 @@ export const compileByTs = async (appDirectory, modernConfig, compileOptions) =>
41
41
  distDir,
42
42
  tsconfigPath
43
43
  } = compileOptions;
44
- if (!tsconfigPath) return;
44
+
45
+ if (!tsconfigPath) {
46
+ return;
47
+ }
48
+
45
49
  const ts = new TypescriptLoader({
46
50
  appDirectory
47
51
  }).load();
@@ -68,13 +72,13 @@ export const compileByTs = async (appDirectory, modernConfig, compileOptions) =>
68
72
  return fileName.includes(sourceDir);
69
73
  });
70
74
  });
71
- const targetDir = path.join(appDirectory, './dist');
72
- let program = createProgram.call(ts, {
73
- rootNames: rootNames,
75
+ const program = createProgram.call(ts, {
76
+ rootNames,
74
77
  projectReferences,
75
- options: _objectSpread(_objectSpread({}, options), {}, {
76
- outDir: targetDir
77
- })
78
+ options: _objectSpread({
79
+ rootDir: appDirectory,
80
+ outDir: distDir
81
+ }, options)
78
82
  });
79
83
  const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
80
84
  const emitResult = program.emit(undefined, undefined, undefined, undefined, {
@@ -83,7 +87,8 @@ export const compileByTs = async (appDirectory, modernConfig, compileOptions) =>
83
87
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
84
88
 
85
89
  if (allDiagnostics.length > 0) {
86
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
90
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
91
+
87
92
  process.exit(1);
88
93
  }
89
94
 
@@ -7,11 +7,12 @@ const isRegExpKey = str => {
7
7
  };
8
8
 
9
9
  const resolveAliasPath = (baseUrl, filePath) => {
10
- if (path.isAbsolute(filePath)) {
11
- return filePath;
10
+ // exclude absolute path and alias
11
+ if (filePath.startsWith('.') || filePath.startsWith('..')) {
12
+ return path.resolve(baseUrl, filePath);
12
13
  }
13
14
 
14
- return path.resolve(baseUrl, filePath);
15
+ return filePath;
15
16
  };
16
17
 
17
18
  const createAliasMatcher = (baseUrl, alias) => {
@@ -27,15 +28,23 @@ const createAliasMatcher = (baseUrl, alias) => {
27
28
 
28
29
  return o;
29
30
  }, []);
31
+ const cacheMap = new Map(); // eslint-disable-next-line consistent-return
32
+
30
33
  return requestedModule => {
34
+ if (cacheMap.has(requestedModule)) {
35
+ return cacheMap.get(requestedModule);
36
+ }
37
+
31
38
  for (const [key, value] of aliasPairs) {
32
39
  if (key instanceof RegExp) {
33
40
  if (key.test(requestedModule)) {
41
+ cacheMap.set(requestedModule, value);
34
42
  return value;
35
43
  }
36
44
  }
37
45
 
38
46
  if (requestedModule === key) {
47
+ cacheMap.set(requestedModule, value);
39
48
  return value;
40
49
  }
41
50
  }
@@ -74,7 +83,9 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
74
83
  const visitNode = node => {
75
84
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
76
85
  try {
77
- const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
86
+ var _node$moduleSpecifier;
87
+
88
+ const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
78
89
 
79
90
  if (!importPathWithQuotes) {
80
91
  return node;
@@ -128,11 +139,29 @@ function getNotAliasedPath(sf, matcher, text) {
128
139
  });
129
140
 
130
141
  if (packagePath) {
142
+ // eslint-disable-next-line consistent-return
131
143
  return text;
132
144
  }
133
- } catch (_unused2) {}
145
+ } catch (_unused2) {} // handle alias to alias
146
+
147
+
148
+ if (!result.startsWith('.') && !result.startsWith('..')) {
149
+ try {
150
+ // Installed packages (node modules) should take precedence over root files with the same name.
151
+ // Ref: https://github.com/nestjs/nest-cli/issues/838
152
+ const packagePath = require.resolve(result, {
153
+ paths: [process.cwd(), ...module.paths]
154
+ });
155
+
156
+ if (packagePath) {
157
+ // eslint-disable-next-line consistent-return
158
+ return result;
159
+ }
160
+ } catch (_unused3) {}
161
+ }
134
162
  }
135
163
 
136
- const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
137
- return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
164
+ const resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
165
+
166
+ return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
138
167
  }
@@ -156,8 +156,8 @@ const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
156
156
 
157
157
  return (0, _babelCompiler.compiler)({
158
158
  rootDir: appDirectory,
159
- distDir: distDir,
160
- sourceDir: sourceDir,
159
+ distDir,
160
+ sourceDir,
161
161
  extensions: _common.FILE_EXTENSIONS
162
162
  }, babelConfig);
163
163
  }));
@@ -9,12 +9,12 @@ var _path = _interopRequireDefault(require("path"));
9
9
 
10
10
  var _utils = require("@modern-js/utils");
11
11
 
12
+ var _typescript = _interopRequireDefault(require("typescript"));
13
+
12
14
  var _typescriptLoader = require("./typescript-loader");
13
15
 
14
16
  var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
15
17
 
16
- var _typescript = _interopRequireDefault(require("typescript"));
17
-
18
18
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
19
 
20
20
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -58,7 +58,11 @@ const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
58
58
  distDir,
59
59
  tsconfigPath
60
60
  } = compileOptions;
61
- if (!tsconfigPath) return;
61
+
62
+ if (!tsconfigPath) {
63
+ return;
64
+ }
65
+
62
66
  const ts = new _typescriptLoader.TypescriptLoader({
63
67
  appDirectory
64
68
  }).load();
@@ -85,15 +89,13 @@ const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
85
89
  return fileName.includes(sourceDir);
86
90
  });
87
91
  });
88
-
89
- const targetDir = _path.default.join(appDirectory, './dist');
90
-
91
- let program = createProgram.call(ts, {
92
- rootNames: rootNames,
92
+ const program = createProgram.call(ts, {
93
+ rootNames,
93
94
  projectReferences,
94
- options: _objectSpread(_objectSpread({}, options), {}, {
95
- outDir: targetDir
96
- })
95
+ options: _objectSpread({
96
+ rootDir: appDirectory,
97
+ outDir: distDir
98
+ }, options)
97
99
  });
98
100
  const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
99
101
  const emitResult = program.emit(undefined, undefined, undefined, undefined, {
@@ -102,7 +104,8 @@ const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
102
104
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
103
105
 
104
106
  if (allDiagnostics.length > 0) {
105
- _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
107
+ _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
108
+
106
109
 
107
110
  process.exit(1);
108
111
  }
@@ -20,11 +20,12 @@ const isRegExpKey = str => {
20
20
  };
21
21
 
22
22
  const resolveAliasPath = (baseUrl, filePath) => {
23
- if (_path.default.isAbsolute(filePath)) {
24
- return filePath;
23
+ // exclude absolute path and alias
24
+ if (filePath.startsWith('.') || filePath.startsWith('..')) {
25
+ return _path.default.resolve(baseUrl, filePath);
25
26
  }
26
27
 
27
- return _path.default.resolve(baseUrl, filePath);
28
+ return filePath;
28
29
  };
29
30
 
30
31
  const createAliasMatcher = (baseUrl, alias) => {
@@ -40,15 +41,23 @@ const createAliasMatcher = (baseUrl, alias) => {
40
41
 
41
42
  return o;
42
43
  }, []);
44
+ const cacheMap = new Map(); // eslint-disable-next-line consistent-return
45
+
43
46
  return requestedModule => {
47
+ if (cacheMap.has(requestedModule)) {
48
+ return cacheMap.get(requestedModule);
49
+ }
50
+
44
51
  for (const [key, value] of aliasPairs) {
45
52
  if (key instanceof RegExp) {
46
53
  if (key.test(requestedModule)) {
54
+ cacheMap.set(requestedModule, value);
47
55
  return value;
48
56
  }
49
57
  }
50
58
 
51
59
  if (requestedModule === key) {
60
+ cacheMap.set(requestedModule, value);
52
61
  return value;
53
62
  }
54
63
  }
@@ -87,7 +96,9 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
87
96
  const visitNode = node => {
88
97
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
89
98
  try {
90
- const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
99
+ var _node$moduleSpecifier;
100
+
101
+ const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
91
102
 
92
103
  if (!importPathWithQuotes) {
93
104
  return node;
@@ -142,11 +153,29 @@ function getNotAliasedPath(sf, matcher, text) {
142
153
  });
143
154
 
144
155
  if (packagePath) {
156
+ // eslint-disable-next-line consistent-return
145
157
  return text;
146
158
  }
147
- } catch (_unused2) {}
159
+ } catch (_unused2) {} // handle alias to alias
160
+
161
+
162
+ if (!result.startsWith('.') && !result.startsWith('..')) {
163
+ try {
164
+ // Installed packages (node modules) should take precedence over root files with the same name.
165
+ // Ref: https://github.com/nestjs/nest-cli/issues/838
166
+ const packagePath = require.resolve(result, {
167
+ paths: [process.cwd(), ...module.paths]
168
+ });
169
+
170
+ if (packagePath) {
171
+ // eslint-disable-next-line consistent-return
172
+ return result;
173
+ }
174
+ } catch (_unused3) {}
175
+ }
148
176
  }
149
177
 
150
- const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './';
151
- return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
178
+ const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
179
+
180
+ return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
152
181
  }
@@ -4,9 +4,9 @@ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
4
  import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
5
  import path from 'path';
6
6
  import { logger, getAlias, fs } from '@modern-js/utils';
7
+ import ts from 'typescript';
7
8
  import { TypescriptLoader } from "./typescript-loader";
8
9
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
9
- import ts from 'typescript';
10
10
 
11
11
  var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
12
12
  var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
@@ -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, targetDir, 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, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, _iterator, _step, source;
65
65
 
66
66
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
67
  while (1) {
@@ -95,13 +95,13 @@ export var compileByTs = /*#__PURE__*/function () {
95
95
  return fileName.includes(sourceDir);
96
96
  });
97
97
  });
98
- targetDir = path.join(appDirectory, './dist');
99
98
  program = createProgram.call(ts, {
100
99
  rootNames: rootNames,
101
100
  projectReferences: projectReferences,
102
- options: _objectSpread(_objectSpread({}, options), {}, {
103
- outDir: targetDir
104
- })
101
+ options: _objectSpread({
102
+ rootDir: appDirectory,
103
+ outDir: distDir
104
+ }, options)
105
105
  });
106
106
  tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
107
107
  emitResult = program.emit(undefined, undefined, undefined, undefined, {
@@ -110,52 +110,53 @@ export var compileByTs = /*#__PURE__*/function () {
110
110
  allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
111
111
 
112
112
  if (allDiagnostics.length > 0) {
113
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
113
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
114
+
114
115
  process.exit(1);
115
116
  }
116
117
 
117
118
  _iterator = _createForOfIteratorHelper(sourceDirs);
118
- _context2.prev = 19;
119
+ _context2.prev = 18;
119
120
 
120
121
  _iterator.s();
121
122
 
122
- case 21:
123
+ case 20:
123
124
  if ((_step = _iterator.n()).done) {
124
- _context2.next = 27;
125
+ _context2.next = 26;
125
126
  break;
126
127
  }
127
128
 
128
129
  source = _step.value;
129
- _context2.next = 25;
130
+ _context2.next = 24;
130
131
  return copyFiles(source, distDir, tsconfigPath);
131
132
 
132
- case 25:
133
- _context2.next = 21;
133
+ case 24:
134
+ _context2.next = 20;
134
135
  break;
135
136
 
136
- case 27:
137
- _context2.next = 32;
137
+ case 26:
138
+ _context2.next = 31;
138
139
  break;
139
140
 
140
- case 29:
141
- _context2.prev = 29;
142
- _context2.t0 = _context2["catch"](19);
141
+ case 28:
142
+ _context2.prev = 28;
143
+ _context2.t0 = _context2["catch"](18);
143
144
 
144
145
  _iterator.e(_context2.t0);
145
146
 
146
- case 32:
147
- _context2.prev = 32;
147
+ case 31:
148
+ _context2.prev = 31;
148
149
 
149
150
  _iterator.f();
150
151
 
151
- return _context2.finish(32);
152
+ return _context2.finish(31);
152
153
 
153
- case 35:
154
+ case 34:
154
155
  case "end":
155
156
  return _context2.stop();
156
157
  }
157
158
  }
158
- }, _callee2, null, [[19, 29, 32, 35]]);
159
+ }, _callee2, null, [[18, 28, 31, 34]]);
159
160
  }));
160
161
 
161
162
  return function compileByTs(_x4, _x5, _x6) {
@@ -10,11 +10,12 @@ var isRegExpKey = function isRegExpKey(str) {
10
10
  };
11
11
 
12
12
  var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
13
- if (path.isAbsolute(filePath)) {
14
- return filePath;
13
+ // exclude absolute path and alias
14
+ if (filePath.startsWith('.') || filePath.startsWith('..')) {
15
+ return path.resolve(baseUrl, filePath);
15
16
  }
16
17
 
17
- return path.resolve(baseUrl, filePath);
18
+ return filePath;
18
19
  };
19
20
 
20
21
  var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
@@ -31,7 +32,13 @@ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
31
32
 
32
33
  return o;
33
34
  }, []);
35
+ var cacheMap = new Map(); // eslint-disable-next-line consistent-return
36
+
34
37
  return function (requestedModule) {
38
+ if (cacheMap.has(requestedModule)) {
39
+ return cacheMap.get(requestedModule);
40
+ }
41
+
35
42
  var _iterator = _createForOfIteratorHelper(aliasPairs),
36
43
  _step;
37
44
 
@@ -43,11 +50,13 @@ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
43
50
 
44
51
  if (key instanceof RegExp) {
45
52
  if (key.test(requestedModule)) {
53
+ cacheMap.set(requestedModule, value);
46
54
  return value;
47
55
  }
48
56
  }
49
57
 
50
58
  if (requestedModule === key) {
59
+ cacheMap.set(requestedModule, value);
51
60
  return value;
52
61
  }
53
62
  }
@@ -91,7 +100,9 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
91
100
  var visitNode = function visitNode(node) {
92
101
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
93
102
  try {
94
- var importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
103
+ var _node$moduleSpecifier;
104
+
105
+ var importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
95
106
 
96
107
  if (!importPathWithQuotes) {
97
108
  return node;
@@ -145,11 +156,29 @@ function getNotAliasedPath(sf, matcher, text) {
145
156
  });
146
157
 
147
158
  if (packagePath) {
159
+ // eslint-disable-next-line consistent-return
148
160
  return text;
149
161
  }
150
- } catch (_unused2) {}
162
+ } catch (_unused2) {} // handle alias to alias
163
+
164
+
165
+ if (!result.startsWith('.') && !result.startsWith('..')) {
166
+ try {
167
+ // Installed packages (node modules) should take precedence over root files with the same name.
168
+ // Ref: https://github.com/nestjs/nest-cli/issues/838
169
+ var _packagePath = require.resolve(result, {
170
+ paths: [process.cwd()].concat(_toConsumableArray(module.paths))
171
+ });
172
+
173
+ if (_packagePath) {
174
+ // eslint-disable-next-line consistent-return
175
+ return result;
176
+ }
177
+ } catch (_unused3) {}
178
+ }
151
179
  }
152
180
 
153
- var resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
154
- return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
181
+ var resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
182
+
183
+ return resolvedPath[0] === '.' ? resolvedPath : "./".concat(resolvedPath);
155
184
  }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.15.1-beta.1",
14
+ "version": "1.15.1-beta.2",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -41,7 +41,6 @@
41
41
  "@modern-js/utils": "1.15.0",
42
42
  "babel-plugin-module-resolver": "^4.1.0",
43
43
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
44
- "recursive-readdir": "2.2.2",
45
44
  "tsconfig-paths": "3.14.1"
46
45
  },
47
46
  "devDependencies": {
@@ -53,7 +52,6 @@
53
52
  "@types/node": "^14",
54
53
  "@types/react": "^17",
55
54
  "@types/react-dom": "^17",
56
- "@types/recursive-readdir": "2.2.0",
57
55
  "jest": "^27",
58
56
  "ts-jest": "^27.0.4",
59
57
  "typescript": "^4"