@modern-js/server-utils 1.21.5 → 2.0.0-beta.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.
@@ -3,24 +3,22 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
3
  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
- import { logger, getAlias, fs } from '@modern-js/utils';
6
+ import { logger, getAliasConfig, fs } from '@modern-js/utils';
7
7
  import ts from 'typescript';
8
8
  import { TypescriptLoader } from "./typescript-loader";
9
9
  import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
10
-
11
10
  var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
12
11
  var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
13
12
  var _ref = parsedCmd,
14
- options = _ref.options,
15
- fileNames = _ref.fileNames,
16
- projectReferences = _ref.projectReferences;
13
+ options = _ref.options,
14
+ fileNames = _ref.fileNames,
15
+ projectReferences = _ref.projectReferences;
17
16
  return {
18
17
  options: options,
19
18
  fileNames: fileNames,
20
19
  projectReferences: projectReferences
21
20
  };
22
21
  };
23
-
24
22
  var copyFiles = /*#__PURE__*/function () {
25
23
  var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(from, to, tsconfigPath) {
26
24
  var basename, targetDir;
@@ -30,13 +28,11 @@ var copyFiles = /*#__PURE__*/function () {
30
28
  case 0:
31
29
  _context.next = 2;
32
30
  return fs.pathExists(from);
33
-
34
31
  case 2:
35
32
  if (!_context.sent) {
36
33
  _context.next = 7;
37
34
  break;
38
35
  }
39
-
40
36
  basename = path.basename(from);
41
37
  targetDir = path.join(to, basename);
42
38
  _context.next = 7;
@@ -45,7 +41,6 @@ var copyFiles = /*#__PURE__*/function () {
45
41
  return !['.ts'].includes(path.extname(src)) && src !== tsconfigPath;
46
42
  }
47
43
  });
48
-
49
44
  case 7:
50
45
  case "end":
51
46
  return _context.stop();
@@ -53,30 +48,24 @@ var copyFiles = /*#__PURE__*/function () {
53
48
  }
54
49
  }, _callee);
55
50
  }));
56
-
57
51
  return function copyFiles(_x, _x2, _x3) {
58
52
  return _ref2.apply(this, arguments);
59
53
  };
60
54
  }();
61
-
62
55
  export var compileByTs = /*#__PURE__*/function () {
63
56
  var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, config, compileOptions) {
64
57
  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
-
66
58
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
59
  while (1) {
68
60
  switch (_context2.prev = _context2.next) {
69
61
  case 0:
70
62
  logger.info("Running ts compile...");
71
63
  sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
72
-
73
64
  if (tsconfigPath) {
74
65
  _context2.next = 4;
75
66
  break;
76
67
  }
77
-
78
68
  return _context2.abrupt("return");
79
-
80
69
  case 4:
81
70
  ts = new TypescriptLoader({
82
71
  appDirectory: appDirectory
@@ -84,7 +73,7 @@ export var compileByTs = /*#__PURE__*/function () {
84
73
  createProgram = ts.createIncrementalProgram || ts.createProgram;
85
74
  formatHost = getFormatHost(ts);
86
75
  alias = config.alias;
87
- aliasOption = getAlias(alias || {}, {
76
+ aliasOption = getAliasConfig(alias, {
88
77
  appDirectory: appDirectory,
89
78
  tsconfigPath: tsconfigPath
90
79
  });
@@ -111,52 +100,38 @@ export var compileByTs = /*#__PURE__*/function () {
111
100
  before: [tsconfigPathsPlugin]
112
101
  });
113
102
  allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
114
-
115
103
  if (allDiagnostics.length > 0) {
116
- logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
117
-
104
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
105
+ // eslint-disable-next-line no-process-exit
118
106
  process.exit(1);
119
107
  }
120
-
121
108
  _iterator = _createForOfIteratorHelper(sourceDirs);
122
109
  _context2.prev = 19;
123
-
124
110
  _iterator.s();
125
-
126
111
  case 21:
127
112
  if ((_step = _iterator.n()).done) {
128
113
  _context2.next = 27;
129
114
  break;
130
115
  }
131
-
132
116
  source = _step.value;
133
117
  _context2.next = 25;
134
118
  return copyFiles(source, distDir, tsconfigPath);
135
-
136
119
  case 25:
137
120
  _context2.next = 21;
138
121
  break;
139
-
140
122
  case 27:
141
123
  _context2.next = 32;
142
124
  break;
143
-
144
125
  case 29:
145
126
  _context2.prev = 29;
146
127
  _context2.t0 = _context2["catch"](19);
147
-
148
128
  _iterator.e(_context2.t0);
149
-
150
129
  case 32:
151
130
  _context2.prev = 32;
152
-
153
131
  _iterator.f();
154
-
155
132
  return _context2.finish(32);
156
-
157
133
  case 35:
158
134
  logger.info("Ts compile succeed");
159
-
160
135
  case 36:
161
136
  case "end":
162
137
  return _context2.stop();
@@ -164,12 +139,10 @@ export var compileByTs = /*#__PURE__*/function () {
164
139
  }
165
140
  }, _callee2, null, [[19, 29, 32, 35]]);
166
141
  }));
167
-
168
142
  return function compileByTs(_x4, _x5, _x6) {
169
143
  return _ref3.apply(this, arguments);
170
144
  };
171
145
  }();
172
-
173
146
  var getFormatHost = function getFormatHost(ts) {
174
147
  return {
175
148
  getCanonicalFileName: function getCanonicalFileName(path) {
@@ -5,20 +5,16 @@ import * as os from 'os';
5
5
  import path, { dirname, posix } from 'path';
6
6
  import * as ts from 'typescript';
7
7
  import { createMatchPath } from 'tsconfig-paths';
8
-
9
8
  var isRegExpKey = function isRegExpKey(str) {
10
9
  return str.startsWith('^') || str.endsWith('$');
11
10
  };
12
-
13
11
  var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
14
12
  // exclude absolute path and alias
15
13
  if (filePath.startsWith('.') || filePath.startsWith('..')) {
16
14
  return path.resolve(baseUrl, filePath);
17
15
  }
18
-
19
16
  return filePath;
20
17
  };
21
-
22
18
  var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
23
19
  var aliasPairs = Object.keys(alias).reduce(function (o, key) {
24
20
  if (isRegExpKey(key)) {
@@ -27,35 +23,30 @@ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
27
23
  o.push([regexp, aliasPath]);
28
24
  } else {
29
25
  var _aliasPath = resolveAliasPath(baseUrl, alias[key]);
30
-
31
26
  o.push([key, _aliasPath]);
32
27
  }
33
-
34
28
  return o;
35
29
  }, []);
36
- var cacheMap = new Map(); // eslint-disable-next-line consistent-return
30
+ var cacheMap = new Map();
37
31
 
32
+ // eslint-disable-next-line consistent-return
38
33
  return function (requestedModule) {
39
34
  if (cacheMap.has(requestedModule)) {
40
35
  return cacheMap.get(requestedModule);
41
36
  }
42
-
43
37
  var _iterator = _createForOfIteratorHelper(aliasPairs),
44
- _step;
45
-
38
+ _step;
46
39
  try {
47
40
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
48
41
  var _step$value = _slicedToArray(_step.value, 2),
49
- key = _step$value[0],
50
- value = _step$value[1];
51
-
42
+ key = _step$value[0],
43
+ value = _step$value[1];
52
44
  if (key instanceof RegExp) {
53
45
  if (key.test(requestedModule)) {
54
46
  cacheMap.set(requestedModule, value);
55
47
  return value;
56
48
  }
57
49
  }
58
-
59
50
  if (requestedModule === key) {
60
51
  cacheMap.set(requestedModule, value);
61
52
  return value;
@@ -68,11 +59,9 @@ var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
68
59
  }
69
60
  };
70
61
  };
71
-
72
62
  var isDynamicImport = function isDynamicImport(tsBinary, node) {
73
63
  return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
74
64
  };
75
-
76
65
  export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
77
66
  var tsPaths = {};
78
67
  var alias = {};
@@ -85,21 +74,16 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
85
74
  });
86
75
  var matchAliasPath = createAliasMatcher(baseUrl, alias);
87
76
  var matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
88
-
89
77
  var matchPath = function matchPath(requestedModule, readJSONSync, fileExists, extensions) {
90
78
  var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
91
-
92
79
  if (result) {
93
80
  return result;
94
81
  }
95
-
96
82
  return matchAliasPath(requestedModule);
97
83
  };
98
-
99
84
  if (Object.keys(paths).length === 0) {
100
85
  return undefined;
101
86
  }
102
-
103
87
  return function (ctx) {
104
88
  return function (sf) {
105
89
  var visitNode = function visitNode(node) {
@@ -107,64 +91,53 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
107
91
  var importPathWithQuotes = node.arguments[0].getText(sf);
108
92
  var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
109
93
  var result = getNotAliasedPath(sf, matchPath, text);
110
-
111
94
  if (!result) {
112
95
  return node;
113
96
  }
114
-
115
97
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
116
98
  }
117
-
118
99
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
119
100
  try {
120
101
  var _node$moduleSpecifier;
121
-
122
102
  var _importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
123
-
124
103
  if (!_importPathWithQuotes) {
125
104
  return node;
126
105
  }
127
-
128
106
  var _text = _importPathWithQuotes.substring(1, _importPathWithQuotes.length - 1);
129
-
130
107
  var _result = getNotAliasedPath(sf, matchPath, _text);
131
-
132
108
  if (!_result) {
133
109
  return node;
134
110
  }
135
-
136
111
  var moduleSpecifier = tsBinary.factory.createStringLiteral(_result);
137
112
  moduleSpecifier.parent = node.moduleSpecifier.parent;
138
-
113
+ var newNode;
139
114
  if (tsBinary.isImportDeclaration(node)) {
140
- return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
115
+ newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
141
116
  } else {
142
- return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
117
+ newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
143
118
  }
119
+ newNode.flags = node.flags;
120
+ return newNode;
144
121
  } catch (_unused) {
145
122
  return node;
146
123
  }
147
124
  }
148
-
149
125
  return tsBinary.visitEachChild(node, visitNode, ctx);
150
126
  };
151
-
152
127
  return tsBinary.visitNode(sf, visitNode);
153
128
  };
154
129
  };
155
- } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
130
+ }
156
131
 
132
+ // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
157
133
  function getNotAliasedPath(sf, matcher, text) {
158
134
  var result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
159
-
160
135
  if (!result) {
161
136
  return;
162
137
  }
163
-
164
138
  if (os.platform() === 'win32') {
165
139
  result = result.replace(/\\/g, '/');
166
140
  }
167
-
168
141
  if (!path.isAbsolute(result)) {
169
142
  // handle alias to alias
170
143
  if (!result.startsWith('.') && !result.startsWith('..')) {
@@ -174,29 +147,25 @@ function getNotAliasedPath(sf, matcher, text) {
174
147
  var packagePath = require.resolve(result, {
175
148
  paths: [process.cwd()].concat(_toConsumableArray(module.paths))
176
149
  });
177
-
178
150
  if (packagePath) {
179
151
  // eslint-disable-next-line consistent-return
180
152
  return result;
181
153
  }
182
154
  } catch (_unused2) {}
183
155
  }
184
-
185
156
  try {
186
157
  // Installed packages (node modules) should take precedence over root files with the same name.
187
158
  // Ref: https://github.com/nestjs/nest-cli/issues/838
188
159
  var _packagePath = require.resolve(text, {
189
160
  paths: [process.cwd()].concat(_toConsumableArray(module.paths))
190
161
  });
191
-
192
162
  if (_packagePath) {
193
163
  // eslint-disable-next-line consistent-return
194
164
  return text;
195
165
  }
196
166
  } catch (_unused3) {}
197
167
  }
198
-
199
- var resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
200
-
168
+ var resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
169
+ // eslint-disable-next-line consistent-return
201
170
  return resolvedPath[0] === '.' ? resolvedPath : "./".concat(resolvedPath);
202
171
  }
@@ -4,36 +4,27 @@ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
4
  export var TypescriptLoader = /*#__PURE__*/function () {
5
5
  function TypescriptLoader(_ref) {
6
6
  var appDirectory = _ref.appDirectory;
7
-
8
7
  _classCallCheck(this, TypescriptLoader);
9
-
10
8
  _defineProperty(this, "tsBinary", void 0);
11
-
12
9
  _defineProperty(this, "appDirectory", void 0);
13
-
14
10
  this.appDirectory = appDirectory;
15
11
  }
16
-
17
12
  _createClass(TypescriptLoader, [{
18
13
  key: "load",
19
14
  value: function load() {
20
15
  if (this.tsBinary) {
21
16
  return this.tsBinary;
22
17
  }
23
-
24
18
  try {
25
19
  var tsPath = require.resolve('typescript', {
26
20
  paths: [this.appDirectory || process.cwd()]
27
21
  });
28
-
29
22
  var _ts = require(tsPath);
30
-
31
23
  return _ts;
32
24
  } catch (error) {
33
25
  throw new Error('TypeScript could not be found! Please, install "typescript" package.');
34
26
  }
35
27
  }
36
28
  }]);
37
-
38
29
  return TypescriptLoader;
39
30
  }();
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.21.5",
14
+ "version": "2.0.0-beta.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -35,13 +35,13 @@
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.21.5",
39
38
  "babel-plugin-module-resolver": "^4.1.0",
40
39
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
41
40
  "tsconfig-paths": "3.14.1",
42
- "@modern-js/babel-preset-lib": "1.21.5",
43
- "@modern-js/plugin": "1.21.5",
44
- "@modern-js/utils": "1.21.5"
41
+ "@modern-js/babel-compiler": "2.0.0-beta.1",
42
+ "@modern-js/babel-preset-lib": "2.0.0-beta.1",
43
+ "@modern-js/plugin": "2.0.0-beta.1",
44
+ "@modern-js/utils": "2.0.0-beta.1"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/babel__core": "^7.1.15",
@@ -50,42 +50,19 @@
50
50
  "jest": "^27",
51
51
  "ts-jest": "^27.0.4",
52
52
  "typescript": "^4",
53
- "@modern-js/core": "1.21.5",
54
- "@scripts/build": "1.21.5",
55
- "@scripts/jest-config": "1.21.5"
53
+ "@modern-js/core": "2.0.0-beta.1",
54
+ "@scripts/build": "2.0.0-beta.1",
55
+ "@scripts/jest-config": "2.0.0-beta.1"
56
56
  },
57
57
  "sideEffects": false,
58
58
  "publishConfig": {
59
59
  "registry": "https://registry.npmjs.org/",
60
60
  "access": "public"
61
61
  },
62
- "wireit": {
63
- "build": {
64
- "command": "modern build",
65
- "files": [
66
- "src/**/*",
67
- "tsconfig.json",
68
- "package.json"
69
- ],
70
- "output": [
71
- "dist/**/*"
72
- ]
73
- },
74
- "test": {
75
- "command": "jest --passWithNoTests",
76
- "files": [
77
- "src/**/*",
78
- "tsconfig.json",
79
- "package.json",
80
- "tests/**/*"
81
- ],
82
- "output": []
83
- }
84
- },
85
62
  "scripts": {
86
63
  "new": "modern new",
87
64
  "dev": "modern build --watch",
88
- "build": "wireit",
89
- "test": "wireit"
65
+ "build": "modern build",
66
+ "test": "jest --passWithNoTests"
90
67
  }
91
68
  }