@modern-js/server-utils 2.0.0-beta.3 → 2.0.0-beta.4

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.
@@ -1,171 +1,246 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
- import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
- import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
4
- import * as os from 'os';
5
- import path, { dirname, posix } from 'path';
6
- import * as ts from 'typescript';
7
- import { createMatchPath } from '@modern-js/utils/tsconfig-paths';
8
- var isRegExpKey = function isRegExpKey(str) {
9
- return str.startsWith('^') || str.endsWith('$');
10
- };
11
- var resolveAliasPath = function resolveAliasPath(baseUrl, filePath) {
12
- // exclude absolute path and alias
13
- if (filePath.startsWith('.') || filePath.startsWith('..')) {
14
- return path.resolve(baseUrl, filePath);
15
- }
16
- return filePath;
17
- };
18
- var createAliasMatcher = function createAliasMatcher(baseUrl, alias) {
19
- var aliasPairs = Object.keys(alias).reduce(function (o, key) {
20
- if (isRegExpKey(key)) {
21
- var regexp = new RegExp(key);
22
- var aliasPath = resolveAliasPath(baseUrl, alias[key]);
23
- o.push([regexp, aliasPath]);
1
+ function _arrayLikeToArray(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _arrayWithHoles(arr) {
7
+ if (Array.isArray(arr)) return arr;
8
+ }
9
+ function _arrayWithoutHoles(arr) {
10
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
11
+ }
12
+ function _instanceof(left, right) {
13
+ if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
14
+ return !!right[Symbol.hasInstance](left);
24
15
  } else {
25
- var _aliasPath = resolveAliasPath(baseUrl, alias[key]);
26
- o.push([key, _aliasPath]);
16
+ return left instanceof right;
27
17
  }
28
- return o;
29
- }, []);
30
- var cacheMap = new Map();
31
-
32
- // eslint-disable-next-line consistent-return
33
- return function (requestedModule) {
34
- if (cacheMap.has(requestedModule)) {
35
- return cacheMap.get(requestedModule);
36
- }
37
- var _iterator = _createForOfIteratorHelper(aliasPairs),
38
- _step;
18
+ }
19
+ function _iterableToArray(iter) {
20
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
21
+ }
22
+ function _iterableToArrayLimit(arr, i) {
23
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
24
+ if (_i == null) return;
25
+ var _arr = [];
26
+ var _n = true;
27
+ var _d = false;
28
+ var _s, _e;
39
29
  try {
40
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
41
- var _step$value = _slicedToArray(_step.value, 2),
42
- key = _step$value[0],
43
- value = _step$value[1];
44
- if (key instanceof RegExp) {
45
- if (key.test(requestedModule)) {
46
- cacheMap.set(requestedModule, value);
47
- return value;
48
- }
49
- }
50
- if (requestedModule === key) {
51
- cacheMap.set(requestedModule, value);
52
- return value;
30
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
31
+ _arr.push(_s.value);
32
+ if (i && _arr.length === i) break;
53
33
  }
54
- }
55
34
  } catch (err) {
56
- _iterator.e(err);
57
- } finally {
58
- _iterator.f();
35
+ _d = true;
36
+ _e = err;
37
+ } finally{
38
+ try {
39
+ if (!_n && _i["return"] != null) _i["return"]();
40
+ } finally{
41
+ if (_d) throw _e;
42
+ }
59
43
  }
60
- };
61
- };
62
- var isDynamicImport = function isDynamicImport(tsBinary, node) {
63
- return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
44
+ return _arr;
45
+ }
46
+ function _nonIterableRest() {
47
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
48
+ }
49
+ function _nonIterableSpread() {
50
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
51
+ }
52
+ function _slicedToArray(arr, i) {
53
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
54
+ }
55
+ function _toConsumableArray(arr) {
56
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
57
+ }
58
+ function _unsupportedIterableToArray(o, minLen) {
59
+ if (!o) return;
60
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
61
+ var n = Object.prototype.toString.call(o).slice(8, -1);
62
+ if (n === "Object" && o.constructor) n = o.constructor.name;
63
+ if (n === "Map" || n === "Set") return Array.from(n);
64
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
65
+ }
66
+ import * as os from "os";
67
+ import path, { dirname, posix } from "path";
68
+ import * as ts from "typescript";
69
+ import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
70
+ var isRegExpKey = function(str) {
71
+ return str.startsWith("^") || str.endsWith("$");
64
72
  };
65
- export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
66
- var tsPaths = {};
67
- var alias = {};
68
- Object.keys(paths).forEach(function (key) {
69
- if (Array.isArray(paths[key])) {
70
- tsPaths[key] = paths[key];
71
- } else {
72
- alias[key] = paths[key];
73
- }
74
- });
75
- var matchAliasPath = createAliasMatcher(baseUrl, alias);
76
- var matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
77
- var matchPath = function matchPath(requestedModule, readJSONSync, fileExists, extensions) {
78
- var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
79
- if (result) {
80
- return result;
73
+ var resolveAliasPath = function(baseUrl, filePath) {
74
+ if (filePath.startsWith(".") || filePath.startsWith("..")) {
75
+ return path.resolve(baseUrl, filePath);
81
76
  }
82
- return matchAliasPath(requestedModule);
83
- };
84
- if (Object.keys(paths).length === 0) {
85
- return undefined;
86
- }
87
- return function (ctx) {
88
- return function (sf) {
89
- var visitNode = function visitNode(node) {
90
- if (isDynamicImport(tsBinary, node)) {
91
- var importPathWithQuotes = node.arguments[0].getText(sf);
92
- var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
93
- var result = getNotAliasedPath(sf, matchPath, text);
94
- if (!result) {
95
- return node;
96
- }
97
- return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
77
+ return filePath;
78
+ };
79
+ var createAliasMatcher = function(baseUrl, alias) {
80
+ var aliasPairs = Object.keys(alias).reduce(function(o, key) {
81
+ if (isRegExpKey(key)) {
82
+ var regexp = new RegExp(key);
83
+ var aliasPath = resolveAliasPath(baseUrl, alias[key]);
84
+ o.push([
85
+ regexp,
86
+ aliasPath
87
+ ]);
88
+ } else {
89
+ var aliasPath1 = resolveAliasPath(baseUrl, alias[key]);
90
+ o.push([
91
+ key,
92
+ aliasPath1
93
+ ]);
98
94
  }
99
- if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
100
- try {
101
- var _node$moduleSpecifier;
102
- var _importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
103
- if (!_importPathWithQuotes) {
104
- return node;
105
- }
106
- var _text = _importPathWithQuotes.substring(1, _importPathWithQuotes.length - 1);
107
- var _result = getNotAliasedPath(sf, matchPath, _text);
108
- if (!_result) {
109
- return node;
95
+ return o;
96
+ }, []);
97
+ var cacheMap = /* @__PURE__ */ new Map();
98
+ return function(requestedModule) {
99
+ if (cacheMap.has(requestedModule)) {
100
+ return cacheMap.get(requestedModule);
101
+ }
102
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
103
+ try {
104
+ for(var _iterator = aliasPairs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
105
+ var _value = _slicedToArray(_step.value, 2), key = _value[0], value = _value[1];
106
+ if (_instanceof(key, RegExp)) {
107
+ if (key.test(requestedModule)) {
108
+ cacheMap.set(requestedModule, value);
109
+ return value;
110
+ }
111
+ }
112
+ if (requestedModule === key) {
113
+ cacheMap.set(requestedModule, value);
114
+ return value;
115
+ }
110
116
  }
111
- var moduleSpecifier = tsBinary.factory.createStringLiteral(_result);
112
- moduleSpecifier.parent = node.moduleSpecifier.parent;
113
- var newNode;
114
- if (tsBinary.isImportDeclaration(node)) {
115
- newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
116
- } else {
117
- newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
117
+ } catch (err) {
118
+ _didIteratorError = true;
119
+ _iteratorError = err;
120
+ } finally{
121
+ try {
122
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
123
+ _iterator.return();
124
+ }
125
+ } finally{
126
+ if (_didIteratorError) {
127
+ throw _iteratorError;
128
+ }
118
129
  }
119
- newNode.flags = node.flags;
120
- return newNode;
121
- } catch (_unused) {
122
- return node;
123
- }
124
130
  }
125
- return tsBinary.visitEachChild(node, visitNode, ctx);
126
- };
127
- return tsBinary.visitNode(sf, visitNode);
128
131
  };
129
- };
132
+ };
133
+ var isDynamicImport = function(tsBinary, node) {
134
+ return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
135
+ };
136
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
137
+ var tsPaths = {};
138
+ var alias = {};
139
+ Object.keys(paths).forEach(function(key) {
140
+ if (Array.isArray(paths[key])) {
141
+ tsPaths[key] = paths[key];
142
+ } else {
143
+ alias[key] = paths[key];
144
+ }
145
+ });
146
+ var matchAliasPath = createAliasMatcher(baseUrl, alias);
147
+ var matchTsPath = createMatchPath(baseUrl, tsPaths, [
148
+ "main"
149
+ ]);
150
+ var matchPath = function(requestedModule, readJSONSync, fileExists, extensions) {
151
+ var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
152
+ if (result) {
153
+ return result;
154
+ }
155
+ return matchAliasPath(requestedModule);
156
+ };
157
+ if (Object.keys(paths).length === 0) {
158
+ return void 0;
159
+ }
160
+ return function(ctx) {
161
+ return function(sf) {
162
+ var visitNode = function(node) {
163
+ if (isDynamicImport(tsBinary, node)) {
164
+ var importPathWithQuotes = node.arguments[0].getText(sf);
165
+ var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
166
+ var result = getNotAliasedPath(sf, matchPath, text);
167
+ if (!result) {
168
+ return node;
169
+ }
170
+ return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
171
+ tsBinary.factory.createStringLiteral(result)
172
+ ]));
173
+ }
174
+ if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
175
+ try {
176
+ var ref;
177
+ var importPathWithQuotes1 = node === null || node === void 0 ? void 0 : (ref = node.moduleSpecifier) === null || ref === void 0 ? void 0 : ref.getText();
178
+ if (!importPathWithQuotes1) {
179
+ return node;
180
+ }
181
+ var text1 = importPathWithQuotes1.substring(1, importPathWithQuotes1.length - 1);
182
+ var result1 = getNotAliasedPath(sf, matchPath, text1);
183
+ if (!result1) {
184
+ return node;
185
+ }
186
+ var moduleSpecifier = tsBinary.factory.createStringLiteral(result1);
187
+ moduleSpecifier.parent = node.moduleSpecifier.parent;
188
+ var newNode;
189
+ if (tsBinary.isImportDeclaration(node)) {
190
+ newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
191
+ } else {
192
+ newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
193
+ }
194
+ newNode.flags = node.flags;
195
+ return newNode;
196
+ } catch (e) {
197
+ return node;
198
+ }
199
+ }
200
+ return tsBinary.visitEachChild(node, visitNode, ctx);
201
+ };
202
+ return tsBinary.visitNode(sf, visitNode);
203
+ };
204
+ };
130
205
  }
131
-
132
- // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
133
206
  function getNotAliasedPath(sf, matcher, text) {
134
- var result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
135
- if (!result) {
136
- return;
137
- }
138
- if (os.platform() === 'win32') {
139
- result = result.replace(/\\/g, '/');
140
- }
141
- if (!path.isAbsolute(result)) {
142
- // handle alias to alias
143
- if (!result.startsWith('.') && !result.startsWith('..')) {
144
- try {
145
- // Installed packages (node modules) should take precedence over root files with the same name.
146
- // Ref: https://github.com/nestjs/nest-cli/issues/838
147
- var packagePath = require.resolve(result, {
148
- paths: [process.cwd()].concat(_toConsumableArray(module.paths))
149
- });
150
- if (packagePath) {
151
- // eslint-disable-next-line consistent-return
152
- return result;
207
+ var result = matcher(text, void 0, void 0, [
208
+ ".ts",
209
+ ".tsx",
210
+ ".js",
211
+ ".jsx"
212
+ ]);
213
+ if (!result) {
214
+ return;
215
+ }
216
+ if (os.platform() === "win32") {
217
+ result = result.replace(/\\/g, "/");
218
+ }
219
+ if (!path.isAbsolute(result)) {
220
+ if (!result.startsWith(".") && !result.startsWith("..")) {
221
+ try {
222
+ var packagePath = require.resolve(result, {
223
+ paths: [
224
+ process.cwd()
225
+ ].concat(_toConsumableArray(module.paths))
226
+ });
227
+ if (packagePath) {
228
+ return result;
229
+ }
230
+ } catch (e) {}
153
231
  }
154
- } catch (_unused2) {}
232
+ try {
233
+ var packagePath1 = require.resolve(text, {
234
+ paths: [
235
+ process.cwd()
236
+ ].concat(_toConsumableArray(module.paths))
237
+ });
238
+ if (packagePath1) {
239
+ return text;
240
+ }
241
+ } catch (e1) {}
155
242
  }
156
- try {
157
- // Installed packages (node modules) should take precedence over root files with the same name.
158
- // Ref: https://github.com/nestjs/nest-cli/issues/838
159
- var _packagePath = require.resolve(text, {
160
- paths: [process.cwd()].concat(_toConsumableArray(module.paths))
161
- });
162
- if (_packagePath) {
163
- // eslint-disable-next-line consistent-return
164
- return text;
165
- }
166
- } catch (_unused3) {}
167
- }
168
- var resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
169
- // eslint-disable-next-line consistent-return
170
- return resolvedPath[0] === '.' ? resolvedPath : "./".concat(resolvedPath);
171
- }
243
+ var resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
244
+ return resolvedPath[0] === "." ? resolvedPath : "./".concat(resolvedPath);
245
+ }
246
+ export { tsconfigPathsBeforeHookFactory };
@@ -1,30 +1,65 @@
1
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/esm/createClass";
3
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- export var TypescriptLoader = /*#__PURE__*/function () {
5
- function TypescriptLoader(_ref) {
6
- var appDirectory = _ref.appDirectory;
7
- _classCallCheck(this, TypescriptLoader);
8
- _defineProperty(this, "tsBinary", void 0);
9
- _defineProperty(this, "appDirectory", void 0);
10
- this.appDirectory = appDirectory;
11
- }
12
- _createClass(TypescriptLoader, [{
13
- key: "load",
14
- value: function load() {
15
- if (this.tsBinary) {
16
- return this.tsBinary;
17
- }
18
- try {
19
- var tsPath = require.resolve('typescript', {
20
- paths: [this.appDirectory || process.cwd()]
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
4
+ }
5
+ }
6
+ function _defineProperties(target, props) {
7
+ for(var i = 0; i < props.length; i++){
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
13
+ }
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
20
+ function _defineProperty(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
21
27
  });
22
- var _ts = require(tsPath);
23
- return _ts;
24
- } catch (error) {
25
- throw new Error('TypeScript could not be found! Please, install "typescript" package.');
26
- }
28
+ } else {
29
+ obj[key] = value;
30
+ }
31
+ return obj;
32
+ }
33
+ var TypescriptLoader = /*#__PURE__*/ function() {
34
+ "use strict";
35
+ function TypescriptLoader(param) {
36
+ var appDirectory = param.appDirectory;
37
+ _classCallCheck(this, TypescriptLoader);
38
+ _defineProperty(this, "tsBinary", void 0);
39
+ _defineProperty(this, "appDirectory", void 0);
40
+ this.appDirectory = appDirectory;
27
41
  }
28
- }]);
29
- return TypescriptLoader;
30
- }();
42
+ _createClass(TypescriptLoader, [
43
+ {
44
+ key: "load",
45
+ value: function load() {
46
+ if (this.tsBinary) {
47
+ return this.tsBinary;
48
+ }
49
+ try {
50
+ var tsPath = require.resolve("typescript", {
51
+ paths: [
52
+ this.appDirectory || process.cwd()
53
+ ]
54
+ });
55
+ var ts = require(tsPath);
56
+ return ts;
57
+ } catch (error) {
58
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
59
+ }
60
+ }
61
+ }
62
+ ]);
63
+ return TypescriptLoader;
64
+ }();
65
+ export { TypescriptLoader };
@@ -1,2 +1,3 @@
1
1
  export * from "./compilers/babel";
2
- export { compile } from "./common";
2
+ import { compile } from "./common";
3
+ export { compile };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.0.0-beta.3",
14
+ "version": "2.0.0-beta.4",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -28,7 +28,6 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@babel/compat-data": "^7.17.10",
32
31
  "@babel/core": "^7.18.0",
33
32
  "@babel/plugin-proposal-class-properties": "^7.17.12",
34
33
  "@babel/plugin-proposal-decorators": "^7.18.0",
@@ -37,10 +36,10 @@
37
36
  "@babel/runtime": "^7.18.0",
38
37
  "babel-plugin-module-resolver": "^4.1.0",
39
38
  "babel-plugin-transform-typescript-metadata": "^0.3.2",
40
- "@modern-js/babel-compiler": "2.0.0-beta.3",
41
- "@modern-js/babel-preset-lib": "2.0.0-beta.3",
42
- "@modern-js/plugin": "2.0.0-beta.3",
43
- "@modern-js/utils": "2.0.0-beta.3"
39
+ "@modern-js/babel-compiler": "2.0.0-beta.4",
40
+ "@modern-js/babel-preset-lib": "2.0.0-beta.4",
41
+ "@modern-js/plugin": "2.0.0-beta.4",
42
+ "@modern-js/utils": "2.0.0-beta.4"
44
43
  },
45
44
  "devDependencies": {
46
45
  "@types/babel__core": "^7.1.15",
@@ -49,9 +48,9 @@
49
48
  "jest": "^27",
50
49
  "ts-jest": "^27.0.4",
51
50
  "typescript": "^4",
52
- "@modern-js/server-core": "2.0.0-beta.3",
53
- "@scripts/build": "2.0.0-beta.3",
54
- "@scripts/jest-config": "2.0.0-beta.3"
51
+ "@modern-js/server-core": "2.0.0-beta.4",
52
+ "@scripts/build": "2.0.0-beta.4",
53
+ "@scripts/jest-config": "2.0.0-beta.4"
55
54
  },
56
55
  "sideEffects": false,
57
56
  "publishConfig": {