@modern-js/server-utils 2.0.0-beta.0 → 2.0.0-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.
@@ -4,28 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.compileByTs = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _typescript = _interopRequireDefault(require("typescript"));
13
-
14
10
  var _typescriptLoader = require("./typescript-loader");
15
-
16
11
  var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
17
-
18
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
13
  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; }
21
-
22
14
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
23
-
24
15
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
25
-
26
16
  const readTsConfigByFile = tsConfigFile => {
27
17
  const parsedCmd = _typescript.default.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, _typescript.default.sys);
28
-
29
18
  const {
30
19
  options,
31
20
  fileNames,
@@ -37,32 +26,25 @@ const readTsConfigByFile = tsConfigFile => {
37
26
  projectReferences
38
27
  };
39
28
  };
40
-
41
29
  const copyFiles = async (from, to, tsconfigPath) => {
42
30
  if (await _utils.fs.pathExists(from)) {
43
31
  const basename = _path.default.basename(from);
44
-
45
32
  const targetDir = _path.default.join(to, basename);
46
-
47
33
  await _utils.fs.copy(from, targetDir, {
48
34
  filter: src => !['.ts'].includes(_path.default.extname(src)) && src !== tsconfigPath
49
35
  });
50
36
  }
51
37
  };
52
-
53
38
  const compileByTs = async (appDirectory, config, compileOptions) => {
54
39
  _utils.logger.info(`Running ts compile...`);
55
-
56
40
  const {
57
41
  sourceDirs,
58
42
  distDir,
59
43
  tsconfigPath
60
44
  } = compileOptions;
61
-
62
45
  if (!tsconfigPath) {
63
46
  return;
64
47
  }
65
-
66
48
  const ts = new _typescriptLoader.TypescriptLoader({
67
49
  appDirectory
68
50
  }).load();
@@ -103,23 +85,17 @@ const compileByTs = async (appDirectory, config, compileOptions) => {
103
85
  before: [tsconfigPathsPlugin]
104
86
  });
105
87
  const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
106
-
107
88
  if (allDiagnostics.length > 0) {
108
- _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
109
-
110
-
89
+ _utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
90
+ // eslint-disable-next-line no-process-exit
111
91
  process.exit(1);
112
92
  }
113
-
114
93
  for (const source of sourceDirs) {
115
94
  await copyFiles(source, distDir, tsconfigPath);
116
95
  }
117
-
118
96
  _utils.logger.info(`Ts compile succeed`);
119
97
  };
120
-
121
98
  exports.compileByTs = compileByTs;
122
-
123
99
  const getFormatHost = ts => {
124
100
  return {
125
101
  getCanonicalFileName: path => path,
@@ -4,32 +4,22 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.tsconfigPathsBeforeHookFactory = tsconfigPathsBeforeHookFactory;
7
-
8
7
  var os = _interopRequireWildcard(require("os"));
9
-
10
8
  var _path = _interopRequireWildcard(require("path"));
11
-
12
9
  var ts = _interopRequireWildcard(require("typescript"));
13
-
14
- var _tsconfigPaths = require("tsconfig-paths");
15
-
10
+ var _tsconfigPaths = require("@modern-js/utils/tsconfig-paths");
16
11
  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); }
17
-
18
12
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
19
-
20
13
  const isRegExpKey = str => {
21
14
  return str.startsWith('^') || str.endsWith('$');
22
15
  };
23
-
24
16
  const resolveAliasPath = (baseUrl, filePath) => {
25
17
  // exclude absolute path and alias
26
18
  if (filePath.startsWith('.') || filePath.startsWith('..')) {
27
19
  return _path.default.resolve(baseUrl, filePath);
28
20
  }
29
-
30
21
  return filePath;
31
22
  };
32
-
33
23
  const createAliasMatcher = (baseUrl, alias) => {
34
24
  const aliasPairs = Object.keys(alias).reduce((o, key) => {
35
25
  if (isRegExpKey(key)) {
@@ -40,16 +30,15 @@ const createAliasMatcher = (baseUrl, alias) => {
40
30
  const aliasPath = resolveAliasPath(baseUrl, alias[key]);
41
31
  o.push([key, aliasPath]);
42
32
  }
43
-
44
33
  return o;
45
34
  }, []);
46
- const cacheMap = new Map(); // eslint-disable-next-line consistent-return
35
+ const cacheMap = new Map();
47
36
 
37
+ // eslint-disable-next-line consistent-return
48
38
  return requestedModule => {
49
39
  if (cacheMap.has(requestedModule)) {
50
40
  return cacheMap.get(requestedModule);
51
41
  }
52
-
53
42
  for (const [key, value] of aliasPairs) {
54
43
  if (key instanceof RegExp) {
55
44
  if (key.test(requestedModule)) {
@@ -57,7 +46,6 @@ const createAliasMatcher = (baseUrl, alias) => {
57
46
  return value;
58
47
  }
59
48
  }
60
-
61
49
  if (requestedModule === key) {
62
50
  cacheMap.set(requestedModule, value);
63
51
  return value;
@@ -65,11 +53,9 @@ const createAliasMatcher = (baseUrl, alias) => {
65
53
  }
66
54
  };
67
55
  };
68
-
69
56
  const isDynamicImport = (tsBinary, node) => {
70
57
  return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
71
58
  };
72
-
73
59
  function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
74
60
  const tsPaths = {};
75
61
  const alias = {};
@@ -82,21 +68,16 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
82
68
  });
83
69
  const matchAliasPath = createAliasMatcher(baseUrl, alias);
84
70
  const matchTsPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, tsPaths, ['main']);
85
-
86
71
  const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
87
72
  const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
88
-
89
73
  if (result) {
90
74
  return result;
91
75
  }
92
-
93
76
  return matchAliasPath(requestedModule);
94
77
  };
95
-
96
78
  if (Object.keys(paths).length === 0) {
97
79
  return undefined;
98
80
  }
99
-
100
81
  return ctx => {
101
82
  return sf => {
102
83
  const visitNode = node => {
@@ -104,68 +85,53 @@ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
104
85
  const importPathWithQuotes = node.arguments[0].getText(sf);
105
86
  const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
106
87
  const result = getNotAliasedPath(sf, matchPath, text);
107
-
108
88
  if (!result) {
109
89
  return node;
110
90
  }
111
-
112
91
  return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
113
92
  }
114
-
115
93
  if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
116
94
  try {
117
95
  var _node$moduleSpecifier;
118
-
119
96
  const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
120
-
121
97
  if (!importPathWithQuotes) {
122
98
  return node;
123
99
  }
124
-
125
100
  const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
126
101
  const result = getNotAliasedPath(sf, matchPath, text);
127
-
128
102
  if (!result) {
129
103
  return node;
130
104
  }
131
-
132
105
  const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
133
106
  moduleSpecifier.parent = node.moduleSpecifier.parent;
134
107
  let newNode;
135
-
136
108
  if (tsBinary.isImportDeclaration(node)) {
137
109
  newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
138
110
  } else {
139
111
  newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
140
112
  }
141
-
142
113
  newNode.flags = node.flags;
143
114
  return newNode;
144
115
  } catch (_unused) {
145
116
  return node;
146
117
  }
147
118
  }
148
-
149
119
  return tsBinary.visitEachChild(node, visitNode, ctx);
150
120
  };
151
-
152
121
  return tsBinary.visitNode(sf, visitNode);
153
122
  };
154
123
  };
155
- } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
156
-
124
+ }
157
125
 
126
+ // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
158
127
  function getNotAliasedPath(sf, matcher, text) {
159
128
  let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
160
-
161
129
  if (!result) {
162
130
  return;
163
131
  }
164
-
165
132
  if (os.platform() === 'win32') {
166
133
  result = result.replace(/\\/g, '/');
167
134
  }
168
-
169
135
  if (!_path.default.isAbsolute(result)) {
170
136
  // handle alias to alias
171
137
  if (!result.startsWith('.') && !result.startsWith('..')) {
@@ -175,29 +141,25 @@ function getNotAliasedPath(sf, matcher, text) {
175
141
  const packagePath = require.resolve(result, {
176
142
  paths: [process.cwd(), ...module.paths]
177
143
  });
178
-
179
144
  if (packagePath) {
180
145
  // eslint-disable-next-line consistent-return
181
146
  return result;
182
147
  }
183
148
  } catch (_unused2) {}
184
149
  }
185
-
186
150
  try {
187
151
  // Installed packages (node modules) should take precedence over root files with the same name.
188
152
  // Ref: https://github.com/nestjs/nest-cli/issues/838
189
153
  const packagePath = require.resolve(text, {
190
154
  paths: [process.cwd(), ...module.paths]
191
155
  });
192
-
193
156
  if (packagePath) {
194
157
  // eslint-disable-next-line consistent-return
195
158
  return text;
196
159
  }
197
160
  } catch (_unused3) {}
198
161
  }
199
-
200
- const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
201
-
162
+ const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './';
163
+ // eslint-disable-next-line consistent-return
202
164
  return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
203
165
  }
@@ -4,38 +4,28 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.TypescriptLoader = void 0;
7
-
8
7
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
-
10
8
  class TypescriptLoader {
11
9
  constructor({
12
10
  appDirectory
13
11
  }) {
14
12
  _defineProperty(this, "tsBinary", void 0);
15
-
16
13
  _defineProperty(this, "appDirectory", void 0);
17
-
18
14
  this.appDirectory = appDirectory;
19
15
  }
20
-
21
16
  load() {
22
17
  if (this.tsBinary) {
23
18
  return this.tsBinary;
24
19
  }
25
-
26
20
  try {
27
21
  const tsPath = require.resolve('typescript', {
28
22
  paths: [this.appDirectory || process.cwd()]
29
23
  });
30
-
31
24
  const ts = require(tsPath);
32
-
33
25
  return ts;
34
26
  } catch (error) {
35
27
  throw new Error('TypeScript could not be found! Please, install "typescript" package.');
36
28
  }
37
29
  }
38
-
39
30
  }
40
-
41
31
  exports.TypescriptLoader = TypescriptLoader;
@@ -12,9 +12,7 @@ Object.defineProperty(exports, "compile", {
12
12
  return _common.compile;
13
13
  }
14
14
  });
15
-
16
15
  var _babel = require("./compilers/babel");
17
-
18
16
  Object.keys(_babel).forEach(function (key) {
19
17
  if (key === "default" || key === "__esModule") return;
20
18
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -26,5 +24,4 @@ Object.keys(_babel).forEach(function (key) {
26
24
  }
27
25
  });
28
26
  });
29
-
30
27
  var _common = require("./common");
@@ -5,23 +5,19 @@ import { fs } from '@modern-js/utils';
5
5
  import { compileByTs } from "../compilers/typescript";
6
6
  import { compileByBabel } from "../compilers/babel";
7
7
  export var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
8
-
9
8
  var validateAbsolutePath = function validateAbsolutePath(filename, message) {
10
9
  if (!path.isAbsolute(filename)) {
11
10
  throw new Error(message);
12
11
  }
13
12
  };
14
-
15
13
  var validateAbsolutePaths = function validateAbsolutePaths(filenames, messageFunc) {
16
14
  filenames.forEach(function (filename) {
17
15
  return validateAbsolutePath(filename, messageFunc(filename));
18
16
  });
19
17
  };
20
-
21
18
  export var compile = /*#__PURE__*/function () {
22
19
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
23
20
  var _modernConfig$server;
24
-
25
21
  var sourceDirs, distDir, tsconfigPath, compiler, isTsProject;
26
22
  return _regeneratorRuntime().wrap(function _callee$(_context) {
27
23
  while (1) {
@@ -34,37 +30,28 @@ export var compile = /*#__PURE__*/function () {
34
30
  validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
35
31
  compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
36
32
  _context.t0 = tsconfigPath;
37
-
38
33
  if (!_context.t0) {
39
34
  _context.next = 9;
40
35
  break;
41
36
  }
42
-
43
37
  _context.next = 8;
44
38
  return fs.pathExists(tsconfigPath);
45
-
46
39
  case 8:
47
40
  _context.t0 = _context.sent;
48
-
49
41
  case 9:
50
42
  isTsProject = _context.t0;
51
-
52
43
  if (!(!isTsProject || compiler === 'babel')) {
53
44
  _context.next = 15;
54
45
  break;
55
46
  }
56
-
57
47
  _context.next = 13;
58
48
  return compileByBabel(appDirectory, modernConfig, compileOptions);
59
-
60
49
  case 13:
61
50
  _context.next = 17;
62
51
  break;
63
-
64
52
  case 15:
65
53
  _context.next = 17;
66
54
  return compileByTs(appDirectory, modernConfig, compileOptions);
67
-
68
55
  case 17:
69
56
  case "end":
70
57
  return _context.stop();
@@ -72,7 +59,6 @@ export var compile = /*#__PURE__*/function () {
72
59
  }
73
60
  }, _callee);
74
61
  }));
75
-
76
62
  return function compile(_x, _x2, _x3) {
77
63
  return _ref.apply(this, arguments);
78
64
  };
@@ -9,12 +9,10 @@ import { FILE_EXTENSIONS } from "../../common";
9
9
  export * from '@babel/core';
10
10
  export var readTsConfig = function readTsConfig(tsconfigPath) {
11
11
  var noExistReturn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
12
-
13
12
  // 如果不存在,则返回 noExistReturn
14
13
  if (!fs.existsSync(tsconfigPath)) {
15
14
  return noExistReturn;
16
15
  }
17
-
18
16
  var content = fs.readFileSync(tsconfigPath, 'utf-8');
19
17
  return json5.parse(content);
20
18
  };
@@ -28,23 +26,27 @@ export var getBabelConfig = function getBabelConfig(libPresetOption, syntaxOptio
28
26
  sourceType: 'unambiguous'
29
27
  }, chain.toJSON());
30
28
  };
31
- export var resolveBabelConfig = function resolveBabelConfig(appDirectory, config, option // FIXME: babel type can't pass type checking
29
+ export var resolveBabelConfig = function resolveBabelConfig(appDirectory, config, option
30
+ // FIXME: babel type can't pass type checking
32
31
  ) {
33
- var envVars = config.envVars,
34
- globalVars = config.globalVars,
35
- alias = config.alias,
36
- babelConfig = config.babelConfig; // alias config
32
+ var define = config.define,
33
+ globalVars = config.globalVars,
34
+ alias = config.alias,
35
+ babelConfig = config.babelConfig;
37
36
 
37
+ // alias config
38
38
  var aliasConfig = getAliasConfig(alias, _objectSpread({
39
39
  appDirectory: appDirectory
40
- }, option)); // babel config
40
+ }, option));
41
41
 
42
+ // babel config
42
43
  var babelChain = getBabelChain({
43
44
  appDirectory: appDirectory,
44
45
  enableReactPreset: true,
45
46
  enableTypescriptPreset: true,
46
47
  alias: aliasConfig,
47
- envVars: envVars,
48
+ // FIXME: handle the define envVars
49
+ envVars: define,
48
50
  globalVars: globalVars
49
51
  }, {
50
52
  type: option.type,
@@ -57,14 +59,13 @@ export var resolveBabelConfig = function resolveBabelConfig(appDirectory, config
57
59
  babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
58
60
  babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
59
61
  legacy: true
60
- }]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
62
+ }]);
61
63
 
64
+ // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
62
65
  babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
63
66
  loose: true
64
67
  }]);
65
-
66
68
  var internalBabelConfig = _objectSpread({}, babelChain.toJSON());
67
-
68
69
  return applyUserBabelConfig(internalBabelConfig, babelConfig);
69
70
  };
70
71
  export var compileByBabel = /*#__PURE__*/function () {
@@ -90,13 +91,11 @@ export var compileByBabel = /*#__PURE__*/function () {
90
91
  });
91
92
  _context.next = 3;
92
93
  return fs.pathExists(sourceDir);
93
-
94
94
  case 3:
95
95
  if (!_context.sent) {
96
96
  _context.next = 8;
97
97
  break;
98
98
  }
99
-
100
99
  basename = path.basename(sourceDir);
101
100
  targetDir = path.join(distDir, basename);
102
101
  _context.next = 8;
@@ -105,7 +104,6 @@ export var compileByBabel = /*#__PURE__*/function () {
105
104
  return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
106
105
  }
107
106
  });
108
-
109
107
  case 8:
110
108
  return _context.abrupt("return", compiler({
111
109
  rootDir: appDirectory,
@@ -113,7 +111,6 @@ export var compileByBabel = /*#__PURE__*/function () {
113
111
  sourceDir: sourceDir,
114
112
  extensions: FILE_EXTENSIONS
115
113
  }, babelConfig));
116
-
117
114
  case 9:
118
115
  case "end":
119
116
  return _context.stop();
@@ -121,12 +118,10 @@ export var compileByBabel = /*#__PURE__*/function () {
121
118
  }
122
119
  }, _callee);
123
120
  }));
124
-
125
121
  return function (_x4) {
126
122
  return _ref2.apply(this, arguments);
127
123
  };
128
124
  }()));
129
-
130
125
  case 3:
131
126
  results = _context2.sent;
132
127
  results.forEach(function (result) {
@@ -134,7 +129,6 @@ export var compileByBabel = /*#__PURE__*/function () {
134
129
  throw new Error(result.message);
135
130
  }
136
131
  });
137
-
138
132
  case 5:
139
133
  case "end":
140
134
  return _context2.stop();
@@ -142,7 +136,6 @@ export var compileByBabel = /*#__PURE__*/function () {
142
136
  }
143
137
  }, _callee2);
144
138
  }));
145
-
146
139
  return function compileByBabel(_x, _x2, _x3) {
147
140
  return _ref.apply(this, arguments);
148
141
  };
@@ -7,20 +7,18 @@ 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
@@ -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) {