@modern-js/server-utils 1.15.0 → 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.
Files changed (26) hide show
  1. package/dist/js/modern/common/index.js +32 -0
  2. package/dist/js/modern/{babel.js → compilers/babel/index.js} +37 -0
  3. package/dist/js/modern/compilers/typescript/index.js +106 -0
  4. package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +167 -0
  5. package/dist/js/modern/compilers/typescript/typescript-loader.js +32 -0
  6. package/dist/js/modern/index.js +2 -1
  7. package/dist/js/node/common/index.js +50 -0
  8. package/dist/js/node/{babel.js → compilers/babel/index.js} +51 -3
  9. package/dist/js/node/compilers/typescript/index.js +126 -0
  10. package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +181 -0
  11. package/dist/js/node/compilers/typescript/typescript-loader.js +41 -0
  12. package/dist/js/node/index.js +14 -2
  13. package/dist/js/treeshaking/common/index.js +76 -0
  14. package/dist/js/treeshaking/compilers/babel/index.js +159 -0
  15. package/dist/js/treeshaking/compilers/typescript/index.js +177 -0
  16. package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +184 -0
  17. package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +39 -0
  18. package/dist/js/treeshaking/index.js +2 -1
  19. package/dist/types/common/index.d.ts +14 -0
  20. package/dist/types/{babel.d.ts → compilers/babel/index.d.ts} +3 -1
  21. package/dist/types/compilers/typescript/index.d.ts +2 -0
  22. package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +2 -0
  23. package/dist/types/compilers/typescript/typescript-loader.d.ts +11 -0
  24. package/dist/types/index.d.ts +2 -1
  25. package/package.json +7 -3
  26. package/dist/js/treeshaking/babel.js +0 -74
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.tsconfigPathsBeforeHookFactory = tsconfigPathsBeforeHookFactory;
7
+
8
+ var os = _interopRequireWildcard(require("os"));
9
+
10
+ var _path = _interopRequireWildcard(require("path"));
11
+
12
+ var _tsconfigPaths = require("tsconfig-paths");
13
+
14
+ 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); }
15
+
16
+ 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; }
17
+
18
+ const isRegExpKey = str => {
19
+ return str.startsWith('^') || str.endsWith('$');
20
+ };
21
+
22
+ const resolveAliasPath = (baseUrl, filePath) => {
23
+ // exclude absolute path and alias
24
+ if (filePath.startsWith('.') || filePath.startsWith('..')) {
25
+ return _path.default.resolve(baseUrl, filePath);
26
+ }
27
+
28
+ return filePath;
29
+ };
30
+
31
+ const createAliasMatcher = (baseUrl, alias) => {
32
+ const aliasPairs = Object.keys(alias).reduce((o, key) => {
33
+ if (isRegExpKey(key)) {
34
+ const regexp = new RegExp(key);
35
+ const aliasPath = resolveAliasPath(baseUrl, alias[key]);
36
+ o.push([regexp, aliasPath]);
37
+ } else {
38
+ const aliasPath = resolveAliasPath(baseUrl, alias[key]);
39
+ o.push([key, aliasPath]);
40
+ }
41
+
42
+ return o;
43
+ }, []);
44
+ const cacheMap = new Map(); // eslint-disable-next-line consistent-return
45
+
46
+ return requestedModule => {
47
+ if (cacheMap.has(requestedModule)) {
48
+ return cacheMap.get(requestedModule);
49
+ }
50
+
51
+ for (const [key, value] of aliasPairs) {
52
+ if (key instanceof RegExp) {
53
+ if (key.test(requestedModule)) {
54
+ cacheMap.set(requestedModule, value);
55
+ return value;
56
+ }
57
+ }
58
+
59
+ if (requestedModule === key) {
60
+ cacheMap.set(requestedModule, value);
61
+ return value;
62
+ }
63
+ }
64
+ };
65
+ };
66
+
67
+ function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
68
+ const tsPaths = {};
69
+ const alias = {};
70
+ Object.keys(paths).forEach(key => {
71
+ if (Array.isArray(paths[key])) {
72
+ tsPaths[key] = paths[key];
73
+ } else {
74
+ alias[key] = paths[key];
75
+ }
76
+ });
77
+ const matchAliasPath = createAliasMatcher(baseUrl, alias);
78
+ const matchTsPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, tsPaths, ['main']);
79
+
80
+ const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
81
+ const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
82
+
83
+ if (result) {
84
+ return result;
85
+ }
86
+
87
+ return matchAliasPath(requestedModule);
88
+ };
89
+
90
+ if (Object.keys(paths).length === 0) {
91
+ return undefined;
92
+ }
93
+
94
+ return ctx => {
95
+ return sf => {
96
+ const visitNode = node => {
97
+ if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
98
+ try {
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();
102
+
103
+ if (!importPathWithQuotes) {
104
+ return node;
105
+ }
106
+
107
+ const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
108
+ const result = getNotAliasedPath(sf, matchPath, text);
109
+
110
+ if (!result) {
111
+ return node;
112
+ }
113
+
114
+ const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
115
+ moduleSpecifier.parent = node.moduleSpecifier.parent;
116
+
117
+ if (tsBinary.isImportDeclaration(node)) {
118
+ return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
119
+ } else {
120
+ return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
121
+ }
122
+ } catch (_unused) {
123
+ return node;
124
+ }
125
+ }
126
+
127
+ return tsBinary.visitEachChild(node, visitNode, ctx);
128
+ };
129
+
130
+ return tsBinary.visitNode(sf, visitNode);
131
+ };
132
+ };
133
+ } // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
134
+
135
+
136
+ function getNotAliasedPath(sf, matcher, text) {
137
+ let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
138
+
139
+ if (!result) {
140
+ return;
141
+ }
142
+
143
+ if (os.platform() === 'win32') {
144
+ result = result.replace(/\\/g, '/');
145
+ }
146
+
147
+ if (!_path.default.isAbsolute(result)) {
148
+ try {
149
+ // Installed packages (node modules) should take precedence over root files with the same name.
150
+ // Ref: https://github.com/nestjs/nest-cli/issues/838
151
+ const packagePath = require.resolve(text, {
152
+ paths: [process.cwd(), ...module.paths]
153
+ });
154
+
155
+ if (packagePath) {
156
+ // eslint-disable-next-line consistent-return
157
+ return text;
158
+ }
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
+ }
176
+ }
177
+
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}`;
181
+ }
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TypescriptLoader = void 0;
7
+
8
+ 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
+ class TypescriptLoader {
11
+ constructor({
12
+ appDirectory
13
+ }) {
14
+ _defineProperty(this, "tsBinary", void 0);
15
+
16
+ _defineProperty(this, "appDirectory", void 0);
17
+
18
+ this.appDirectory = appDirectory;
19
+ }
20
+
21
+ load() {
22
+ if (this.tsBinary) {
23
+ return this.tsBinary;
24
+ }
25
+
26
+ try {
27
+ const tsPath = require.resolve('typescript', {
28
+ paths: [this.appDirectory || process.cwd()]
29
+ });
30
+
31
+ const ts = require(tsPath);
32
+
33
+ return ts;
34
+ } catch (error) {
35
+ throw new Error('TypeScript could not be found! Please, install "typescript" package.');
36
+ }
37
+ }
38
+
39
+ }
40
+
41
+ exports.TypescriptLoader = TypescriptLoader;
@@ -3,11 +3,21 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ var _exportNames = {
7
+ compile: true
8
+ };
9
+ Object.defineProperty(exports, "compile", {
10
+ enumerable: true,
11
+ get: function () {
12
+ return _common.compile;
13
+ }
14
+ });
6
15
 
7
- var _babel = require("./babel");
16
+ var _babel = require("./compilers/babel");
8
17
 
9
18
  Object.keys(_babel).forEach(function (key) {
10
19
  if (key === "default" || key === "__esModule") return;
20
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
11
21
  if (key in exports && exports[key] === _babel[key]) return;
12
22
  Object.defineProperty(exports, key, {
13
23
  enumerable: true,
@@ -15,4 +25,6 @@ Object.keys(_babel).forEach(function (key) {
15
25
  return _babel[key];
16
26
  }
17
27
  });
18
- });
28
+ });
29
+
30
+ var _common = require("./common");
@@ -0,0 +1,76 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import * as path from 'path';
4
+ import { fs } from '@modern-js/utils';
5
+ import { compileByTs } from "../compilers/typescript";
6
+ import { compileByBabel } from "../compilers/babel";
7
+ export var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
8
+
9
+ var validateAbsolutePath = function validateAbsolutePath(filename, message) {
10
+ if (!path.isAbsolute(filename)) {
11
+ throw new Error(message);
12
+ }
13
+ };
14
+
15
+ var validateAbsolutePaths = function validateAbsolutePaths(filenames, messageFunc) {
16
+ filenames.forEach(function (filename) {
17
+ return validateAbsolutePath(filename, messageFunc(filename));
18
+ });
19
+ };
20
+
21
+ export var compile = /*#__PURE__*/function () {
22
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
23
+ var sourceDirs, distDir, tsconfigPath, isTsProject;
24
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
25
+ while (1) {
26
+ switch (_context.prev = _context.next) {
27
+ case 0:
28
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
29
+ validateAbsolutePaths(sourceDirs, function (dir) {
30
+ return "source dir ".concat(dir, " is not an absolute path.");
31
+ });
32
+ validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
33
+ _context.t0 = tsconfigPath;
34
+
35
+ if (!_context.t0) {
36
+ _context.next = 8;
37
+ break;
38
+ }
39
+
40
+ _context.next = 7;
41
+ return fs.pathExists(tsconfigPath);
42
+
43
+ case 7:
44
+ _context.t0 = _context.sent;
45
+
46
+ case 8:
47
+ isTsProject = _context.t0;
48
+
49
+ if (!isTsProject) {
50
+ _context.next = 14;
51
+ break;
52
+ }
53
+
54
+ _context.next = 12;
55
+ return compileByTs(appDirectory, modernConfig, compileOptions);
56
+
57
+ case 12:
58
+ _context.next = 16;
59
+ break;
60
+
61
+ case 14:
62
+ _context.next = 16;
63
+ return compileByBabel(appDirectory, modernConfig, compileOptions);
64
+
65
+ case 16:
66
+ case "end":
67
+ return _context.stop();
68
+ }
69
+ }
70
+ }, _callee);
71
+ }));
72
+
73
+ return function compile(_x, _x2, _x3) {
74
+ return _ref.apply(this, arguments);
75
+ };
76
+ }();
@@ -0,0 +1,159 @@
1
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
+ import * as path from 'path';
5
+ import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
6
+ import { fs, json5, getAlias, applyOptionsChain } from '@modern-js/utils';
7
+ import { compiler } from '@modern-js/babel-compiler';
8
+ import { FILE_EXTENSIONS } from "../../common";
9
+ export * from '@babel/core';
10
+ export var readTsConfig = function readTsConfig(tsconfigPath) {
11
+ var noExistReturn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
12
+
13
+ // 如果不存在,则返回 noExistReturn
14
+ if (!fs.existsSync(tsconfigPath)) {
15
+ return noExistReturn;
16
+ }
17
+
18
+ var content = fs.readFileSync(tsconfigPath, 'utf-8');
19
+ return json5.parse(content);
20
+ };
21
+ export var existTsConfigFile = function existTsConfigFile(tsconfigAbsolutePath) {
22
+ var tsconfig = readTsConfig(tsconfigAbsolutePath);
23
+ return Boolean(tsconfig);
24
+ };
25
+ export var getBabelConfig = function getBabelConfig(libPresetOption, syntaxOption) {
26
+ var chain = getBabelChain(libPresetOption, syntaxOption);
27
+ return _objectSpread({
28
+ sourceType: 'unambiguous'
29
+ }, chain.toJSON());
30
+ };
31
+ export var resolveBabelConfig = function resolveBabelConfig(appDirectory, modernConfig, option // FIXME: babel type can't pass type checking
32
+ ) {
33
+ var _modernConfig$source = modernConfig.source,
34
+ envVars = _modernConfig$source.envVars,
35
+ globalVars = _modernConfig$source.globalVars,
36
+ _modernConfig$source$ = _modernConfig$source.jsxTransformRuntime,
37
+ jsxTransformRuntime = _modernConfig$source$ === void 0 ? 'automatic' : _modernConfig$source$,
38
+ userLodashOption = modernConfig.tools.lodash; // alias config
39
+
40
+ var aliasConfig = getAlias(modernConfig.source.alias, _objectSpread({
41
+ appDirectory: appDirectory
42
+ }, option)); // lodash config
43
+
44
+ var lodashOptions = applyOptionsChain({
45
+ id: ['lodash', 'ramda']
46
+ }, // TODO: 需要处理类型问题
47
+ userLodashOption); // babel config
48
+
49
+ var babelChain = getBabelChain({
50
+ appDirectory: appDirectory,
51
+ enableReactPreset: true,
52
+ enableTypescriptPreset: true,
53
+ alias: aliasConfig,
54
+ envVars: envVars,
55
+ globalVars: globalVars,
56
+ lodashOptions: lodashOptions,
57
+ jsxTransformRuntime: jsxTransformRuntime
58
+ }, {
59
+ type: option.type,
60
+ syntax: option.syntax
61
+ });
62
+ var envOptions = babelChain.preset('@babel/preset-env').options();
63
+ babelChain.preset('@babel/preset-env').use(require.resolve('@babel/preset-env'), [_objectSpread(_objectSpread({}, envOptions[0]), {}, {
64
+ loose: true
65
+ })]);
66
+ babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
67
+ babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
68
+ legacy: true
69
+ }]); // resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
70
+
71
+ babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
72
+ loose: true
73
+ }]);
74
+
75
+ var internalBabelConfig = _objectSpread({}, babelChain.toJSON());
76
+
77
+ var userBabelConfig = modernConfig.tools.babel;
78
+ return applyUserBabelConfig(internalBabelConfig, userBabelConfig);
79
+ };
80
+ export var compileByBabel = /*#__PURE__*/function () {
81
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
82
+ var sourceDirs, distDir, tsconfigPath, results;
83
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
84
+ while (1) {
85
+ switch (_context2.prev = _context2.next) {
86
+ case 0:
87
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
88
+ _context2.next = 3;
89
+ return Promise.all(sourceDirs.map( /*#__PURE__*/function () {
90
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(sourceDir) {
91
+ var babelConfig, basename, targetDir;
92
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
93
+ while (1) {
94
+ switch (_context.prev = _context.next) {
95
+ case 0:
96
+ babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
97
+ tsconfigPath: tsconfigPath ? tsconfigPath : '',
98
+ syntax: 'es6+',
99
+ type: 'commonjs'
100
+ });
101
+ _context.next = 3;
102
+ return fs.pathExists(sourceDir);
103
+
104
+ case 3:
105
+ if (!_context.sent) {
106
+ _context.next = 8;
107
+ break;
108
+ }
109
+
110
+ basename = path.basename(sourceDir);
111
+ targetDir = path.join(distDir, basename);
112
+ _context.next = 8;
113
+ return fs.copy(sourceDir, targetDir, {
114
+ filter: function filter(src) {
115
+ return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
116
+ }
117
+ });
118
+
119
+ case 8:
120
+ return _context.abrupt("return", compiler({
121
+ rootDir: appDirectory,
122
+ distDir: distDir,
123
+ sourceDir: sourceDir,
124
+ extensions: FILE_EXTENSIONS
125
+ }, babelConfig));
126
+
127
+ case 9:
128
+ case "end":
129
+ return _context.stop();
130
+ }
131
+ }
132
+ }, _callee);
133
+ }));
134
+
135
+ return function (_x4) {
136
+ return _ref2.apply(this, arguments);
137
+ };
138
+ }()));
139
+
140
+ case 3:
141
+ results = _context2.sent;
142
+ results.forEach(function (result) {
143
+ if (result.code === 1) {
144
+ throw new Error(result.message);
145
+ }
146
+ });
147
+
148
+ case 5:
149
+ case "end":
150
+ return _context2.stop();
151
+ }
152
+ }
153
+ }, _callee2);
154
+ }));
155
+
156
+ return function compileByBabel(_x, _x2, _x3) {
157
+ return _ref.apply(this, arguments);
158
+ };
159
+ }();
@@ -0,0 +1,177 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
3
+ import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
4
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
+ import path from 'path';
6
+ import { logger, getAlias, fs } from '@modern-js/utils';
7
+ import ts from 'typescript';
8
+ import { TypescriptLoader } from "./typescript-loader";
9
+ import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
10
+
11
+ var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
12
+ var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
13
+ var _ref = parsedCmd,
14
+ options = _ref.options,
15
+ fileNames = _ref.fileNames,
16
+ projectReferences = _ref.projectReferences;
17
+ return {
18
+ options: options,
19
+ fileNames: fileNames,
20
+ projectReferences: projectReferences
21
+ };
22
+ };
23
+
24
+ var copyFiles = /*#__PURE__*/function () {
25
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(from, to, tsconfigPath) {
26
+ var basename, targetDir;
27
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
28
+ while (1) {
29
+ switch (_context.prev = _context.next) {
30
+ case 0:
31
+ _context.next = 2;
32
+ return fs.pathExists(from);
33
+
34
+ case 2:
35
+ if (!_context.sent) {
36
+ _context.next = 7;
37
+ break;
38
+ }
39
+
40
+ basename = path.basename(from);
41
+ targetDir = path.join(to, basename);
42
+ _context.next = 7;
43
+ return fs.copy(from, targetDir, {
44
+ filter: function filter(src) {
45
+ return !['.ts'].includes(path.extname(src)) && src !== tsconfigPath;
46
+ }
47
+ });
48
+
49
+ case 7:
50
+ case "end":
51
+ return _context.stop();
52
+ }
53
+ }
54
+ }, _callee);
55
+ }));
56
+
57
+ return function copyFiles(_x, _x2, _x3) {
58
+ return _ref2.apply(this, arguments);
59
+ };
60
+ }();
61
+
62
+ export var compileByTs = /*#__PURE__*/function () {
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;
65
+
66
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
67
+ while (1) {
68
+ switch (_context2.prev = _context2.next) {
69
+ case 0:
70
+ logger.info("Running ts compile...");
71
+ sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
72
+
73
+ if (tsconfigPath) {
74
+ _context2.next = 4;
75
+ break;
76
+ }
77
+
78
+ return _context2.abrupt("return");
79
+
80
+ case 4:
81
+ ts = new TypescriptLoader({
82
+ appDirectory: appDirectory
83
+ }).load();
84
+ createProgram = ts.createIncrementalProgram || ts.createProgram;
85
+ formatHost = getFormatHost(ts);
86
+ alias = modernConfig.source.alias;
87
+ aliasOption = getAlias(alias || {}, {
88
+ appDirectory: appDirectory,
89
+ tsconfigPath: tsconfigPath
90
+ });
91
+ _aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
92
+ _readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
93
+ rootNames = fileNames.filter(function (fileName) {
94
+ return fileName.endsWith('.d.ts') || sourceDirs.some(function (sourceDir) {
95
+ return fileName.includes(sourceDir);
96
+ });
97
+ });
98
+ program = createProgram.call(ts, {
99
+ rootNames: rootNames,
100
+ projectReferences: projectReferences,
101
+ options: _objectSpread({
102
+ rootDir: appDirectory,
103
+ outDir: distDir
104
+ }, options)
105
+ });
106
+ tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
107
+ emitResult = program.emit(undefined, undefined, undefined, undefined, {
108
+ before: [tsconfigPathsPlugin]
109
+ });
110
+ allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
111
+
112
+ if (allDiagnostics.length > 0) {
113
+ logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
114
+
115
+ process.exit(1);
116
+ }
117
+
118
+ _iterator = _createForOfIteratorHelper(sourceDirs);
119
+ _context2.prev = 18;
120
+
121
+ _iterator.s();
122
+
123
+ case 20:
124
+ if ((_step = _iterator.n()).done) {
125
+ _context2.next = 26;
126
+ break;
127
+ }
128
+
129
+ source = _step.value;
130
+ _context2.next = 24;
131
+ return copyFiles(source, distDir, tsconfigPath);
132
+
133
+ case 24:
134
+ _context2.next = 20;
135
+ break;
136
+
137
+ case 26:
138
+ _context2.next = 31;
139
+ break;
140
+
141
+ case 28:
142
+ _context2.prev = 28;
143
+ _context2.t0 = _context2["catch"](18);
144
+
145
+ _iterator.e(_context2.t0);
146
+
147
+ case 31:
148
+ _context2.prev = 31;
149
+
150
+ _iterator.f();
151
+
152
+ return _context2.finish(31);
153
+
154
+ case 34:
155
+ case "end":
156
+ return _context2.stop();
157
+ }
158
+ }
159
+ }, _callee2, null, [[18, 28, 31, 34]]);
160
+ }));
161
+
162
+ return function compileByTs(_x4, _x5, _x6) {
163
+ return _ref3.apply(this, arguments);
164
+ };
165
+ }();
166
+
167
+ var getFormatHost = function getFormatHost(ts) {
168
+ return {
169
+ getCanonicalFileName: function getCanonicalFileName(path) {
170
+ return path;
171
+ },
172
+ getCurrentDirectory: ts.sys.getCurrentDirectory,
173
+ getNewLine: function getNewLine() {
174
+ return ts.sys.newLine;
175
+ }
176
+ };
177
+ };