@modern-js/server-utils 1.2.12-alpha.0 → 1.15.1-beta.0
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.
- package/CHANGELOG.md +9 -7
- package/dist/js/modern/common/index.js +26 -0
- package/dist/js/modern/{babel.js → compilers/babel/index.js} +40 -0
- package/dist/js/modern/compilers/typescript/index.js +109 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +138 -0
- package/dist/js/modern/compilers/typescript/typescript-loader.js +32 -0
- package/dist/js/modern/index.js +2 -1
- package/dist/js/node/common/index.js +52 -0
- package/dist/js/node/{babel.js → compilers/babel/index.js} +54 -3
- package/dist/js/node/compilers/typescript/index.js +133 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +152 -0
- package/dist/js/node/compilers/typescript/typescript-loader.js +41 -0
- package/dist/js/node/index.js +14 -2
- package/dist/js/treeshaking/common/index.js +82 -0
- package/dist/js/treeshaking/compilers/babel/index.js +160 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +189 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +155 -0
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +39 -0
- package/dist/js/treeshaking/index.js +2 -1
- package/dist/types/common/index.d.ts +14 -0
- package/dist/types/{babel.d.ts → compilers/babel/index.d.ts} +3 -1
- package/dist/types/compilers/typescript/index.d.ts +2 -0
- package/dist/types/compilers/typescript/tsconfig-paths-plugin.d.ts +2 -0
- package/dist/types/compilers/typescript/typescript-loader.d.ts +11 -0
- package/dist/types/index.d.ts +2 -1
- package/package.json +15 -9
- package/dist/js/treeshaking/babel.js +0 -74
|
@@ -0,0 +1,152 @@
|
|
|
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
|
+
if (_path.default.isAbsolute(filePath)) {
|
|
24
|
+
return filePath;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return _path.default.resolve(baseUrl, filePath);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
31
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
32
|
+
if (isRegExpKey(key)) {
|
|
33
|
+
const regexp = new RegExp(key);
|
|
34
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
35
|
+
o.push([regexp, aliasPath]);
|
|
36
|
+
} else {
|
|
37
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
38
|
+
o.push([key, aliasPath]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return o;
|
|
42
|
+
}, []);
|
|
43
|
+
return requestedModule => {
|
|
44
|
+
for (const [key, value] of aliasPairs) {
|
|
45
|
+
if (key instanceof RegExp) {
|
|
46
|
+
if (key.test(requestedModule)) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (requestedModule === key) {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
59
|
+
const tsPaths = {};
|
|
60
|
+
const alias = {};
|
|
61
|
+
Object.keys(paths).forEach(key => {
|
|
62
|
+
if (Array.isArray(paths[key])) {
|
|
63
|
+
tsPaths[key] = paths[key];
|
|
64
|
+
} else {
|
|
65
|
+
alias[key] = paths[key];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
69
|
+
const matchTsPath = (0, _tsconfigPaths.createMatchPath)(baseUrl, tsPaths, ['main']);
|
|
70
|
+
|
|
71
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
72
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
73
|
+
|
|
74
|
+
if (result) {
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return matchAliasPath(requestedModule);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
if (Object.keys(paths).length === 0) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return ctx => {
|
|
86
|
+
return sf => {
|
|
87
|
+
const visitNode = node => {
|
|
88
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
89
|
+
try {
|
|
90
|
+
const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
|
|
91
|
+
|
|
92
|
+
if (!importPathWithQuotes) {
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
97
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
98
|
+
|
|
99
|
+
if (!result) {
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
104
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
105
|
+
|
|
106
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
107
|
+
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
108
|
+
} else {
|
|
109
|
+
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
110
|
+
}
|
|
111
|
+
} catch (_unused) {
|
|
112
|
+
return node;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
126
|
+
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
127
|
+
|
|
128
|
+
if (!result) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (os.platform() === 'win32') {
|
|
133
|
+
result = result.replace(/\\/g, '/');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!_path.default.isAbsolute(result)) {
|
|
137
|
+
try {
|
|
138
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
139
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
140
|
+
const packagePath = require.resolve(text, {
|
|
141
|
+
paths: [process.cwd(), ...module.paths]
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
if (packagePath) {
|
|
145
|
+
return text;
|
|
146
|
+
}
|
|
147
|
+
} catch (_unused2) {}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const resolvedPath = _path.posix.relative((0, _path.dirname)(sf.fileName), result) || './';
|
|
151
|
+
return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
|
|
152
|
+
}
|
|
@@ -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;
|
package/dist/js/node/index.js
CHANGED
|
@@ -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,82 @@
|
|
|
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 recursive from 'recursive-readdir';
|
|
6
|
+
import { compileByTs } from "../compilers/typescript";
|
|
7
|
+
import { compileByBabel } from "../compilers/babel";
|
|
8
|
+
export var FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
9
|
+
export var TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
10
|
+
export var compile = /*#__PURE__*/function () {
|
|
11
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(appDirectory, modernConfig, compileOptions) {
|
|
12
|
+
var tsConifgPath, isTsProject;
|
|
13
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
14
|
+
while (1) {
|
|
15
|
+
switch (_context.prev = _context.next) {
|
|
16
|
+
case 0:
|
|
17
|
+
tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
|
|
18
|
+
_context.next = 3;
|
|
19
|
+
return fs.pathExists(tsConifgPath);
|
|
20
|
+
|
|
21
|
+
case 3:
|
|
22
|
+
isTsProject = _context.sent;
|
|
23
|
+
|
|
24
|
+
if (!isTsProject) {
|
|
25
|
+
_context.next = 9;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_context.next = 7;
|
|
30
|
+
return compileByTs(appDirectory, modernConfig, compileOptions);
|
|
31
|
+
|
|
32
|
+
case 7:
|
|
33
|
+
_context.next = 11;
|
|
34
|
+
break;
|
|
35
|
+
|
|
36
|
+
case 9:
|
|
37
|
+
_context.next = 11;
|
|
38
|
+
return compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
39
|
+
|
|
40
|
+
case 11:
|
|
41
|
+
case "end":
|
|
42
|
+
return _context.stop();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}, _callee);
|
|
46
|
+
}));
|
|
47
|
+
|
|
48
|
+
return function compile(_x, _x2, _x3) {
|
|
49
|
+
return _ref.apply(this, arguments);
|
|
50
|
+
};
|
|
51
|
+
}();
|
|
52
|
+
export var recursiveReadTsFiles = /*#__PURE__*/function () {
|
|
53
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(dirname) {
|
|
54
|
+
var ignoreFunc, files;
|
|
55
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
56
|
+
while (1) {
|
|
57
|
+
switch (_context2.prev = _context2.next) {
|
|
58
|
+
case 0:
|
|
59
|
+
ignoreFunc = function ignoreFunc(filename) {
|
|
60
|
+
var extname = path.extname(filename);
|
|
61
|
+
return !['.ts', '.d.ts'].includes(extname);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
_context2.next = 3;
|
|
65
|
+
return recursive(dirname, [ignoreFunc]);
|
|
66
|
+
|
|
67
|
+
case 3:
|
|
68
|
+
files = _context2.sent;
|
|
69
|
+
return _context2.abrupt("return", files);
|
|
70
|
+
|
|
71
|
+
case 5:
|
|
72
|
+
case "end":
|
|
73
|
+
return _context2.stop();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, _callee2);
|
|
77
|
+
}));
|
|
78
|
+
|
|
79
|
+
return function recursiveReadTsFiles(_x4) {
|
|
80
|
+
return _ref2.apply(this, arguments);
|
|
81
|
+
};
|
|
82
|
+
}();
|
|
@@ -0,0 +1,160 @@
|
|
|
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 patterns, results;
|
|
83
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
|
84
|
+
while (1) {
|
|
85
|
+
switch (_context2.prev = _context2.next) {
|
|
86
|
+
case 0:
|
|
87
|
+
patterns = compileOptions.patterns;
|
|
88
|
+
_context2.next = 3;
|
|
89
|
+
return Promise.all(patterns.map( /*#__PURE__*/function () {
|
|
90
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(pattern) {
|
|
91
|
+
var from, to, tsconfigPath, babelConfig, basename, targetDir;
|
|
92
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
93
|
+
while (1) {
|
|
94
|
+
switch (_context.prev = _context.next) {
|
|
95
|
+
case 0:
|
|
96
|
+
from = pattern.from, to = pattern.to, tsconfigPath = pattern.tsconfigPath;
|
|
97
|
+
babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
98
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
99
|
+
syntax: 'es6+',
|
|
100
|
+
type: 'commonjs'
|
|
101
|
+
});
|
|
102
|
+
_context.next = 4;
|
|
103
|
+
return fs.pathExists(from);
|
|
104
|
+
|
|
105
|
+
case 4:
|
|
106
|
+
if (!_context.sent) {
|
|
107
|
+
_context.next = 9;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
basename = path.basename(from);
|
|
112
|
+
targetDir = path.join(to, basename);
|
|
113
|
+
_context.next = 9;
|
|
114
|
+
return fs.copy(from, targetDir, {
|
|
115
|
+
filter: function filter(src) {
|
|
116
|
+
return !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
case 9:
|
|
121
|
+
return _context.abrupt("return", compiler({
|
|
122
|
+
rootDir: appDirectory,
|
|
123
|
+
distDir: to,
|
|
124
|
+
sourceDir: from,
|
|
125
|
+
extensions: FILE_EXTENSIONS
|
|
126
|
+
}, babelConfig));
|
|
127
|
+
|
|
128
|
+
case 10:
|
|
129
|
+
case "end":
|
|
130
|
+
return _context.stop();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}, _callee);
|
|
134
|
+
}));
|
|
135
|
+
|
|
136
|
+
return function (_x4) {
|
|
137
|
+
return _ref2.apply(this, arguments);
|
|
138
|
+
};
|
|
139
|
+
}()));
|
|
140
|
+
|
|
141
|
+
case 3:
|
|
142
|
+
results = _context2.sent;
|
|
143
|
+
results.forEach(function (result) {
|
|
144
|
+
if (result.code === 1) {
|
|
145
|
+
throw new Error(result.message);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
case 5:
|
|
150
|
+
case "end":
|
|
151
|
+
return _context2.stop();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}, _callee2);
|
|
155
|
+
}));
|
|
156
|
+
|
|
157
|
+
return function compileByBabel(_x, _x2, _x3) {
|
|
158
|
+
return _ref.apply(this, arguments);
|
|
159
|
+
};
|
|
160
|
+
}();
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
|
+
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
|
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
|
|
4
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
5
|
+
// from to tsconfigpath
|
|
6
|
+
// root dir
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import { logger, getAlias, fs } from '@modern-js/utils';
|
|
9
|
+
import { TS_CONFIG_FILENAME } from "../../common";
|
|
10
|
+
import { TypescriptLoader } from "./typescript-loader";
|
|
11
|
+
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
12
|
+
import ts from 'typescript';
|
|
13
|
+
|
|
14
|
+
var readTsConfigByFile = function readTsConfigByFile(tsConfigFile) {
|
|
15
|
+
var parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
|
|
16
|
+
var _ref = parsedCmd,
|
|
17
|
+
options = _ref.options,
|
|
18
|
+
fileNames = _ref.fileNames,
|
|
19
|
+
projectReferences = _ref.projectReferences;
|
|
20
|
+
return {
|
|
21
|
+
options: options,
|
|
22
|
+
fileNames: fileNames,
|
|
23
|
+
projectReferences: projectReferences
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
var copyFiles = /*#__PURE__*/function () {
|
|
28
|
+
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(from, to, tsconfigPath) {
|
|
29
|
+
var basename, targetDir;
|
|
30
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
31
|
+
while (1) {
|
|
32
|
+
switch (_context.prev = _context.next) {
|
|
33
|
+
case 0:
|
|
34
|
+
_context.next = 2;
|
|
35
|
+
return fs.pathExists(from);
|
|
36
|
+
|
|
37
|
+
case 2:
|
|
38
|
+
if (!_context.sent) {
|
|
39
|
+
_context.next = 7;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
basename = path.basename(from);
|
|
44
|
+
targetDir = path.join(to, basename);
|
|
45
|
+
_context.next = 7;
|
|
46
|
+
return fs.copy(from, targetDir, {
|
|
47
|
+
filter: function filter(src) {
|
|
48
|
+
return !['.ts'].includes(path.extname(src)) && src !== tsconfigPath;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
case 7:
|
|
53
|
+
case "end":
|
|
54
|
+
return _context.stop();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}, _callee);
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
return function copyFiles(_x, _x2, _x3) {
|
|
61
|
+
return _ref2.apply(this, arguments);
|
|
62
|
+
};
|
|
63
|
+
}(); // TODO: 分离特殊的业务逻辑,通用化
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
export var compileByTs = /*#__PURE__*/function () {
|
|
67
|
+
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(appDirectory, modernConfig, compileOptions) {
|
|
68
|
+
var rootTsconfigPath, patterns, ts, createProgram, formatHost, _iterator, _step, _loop;
|
|
69
|
+
|
|
70
|
+
return _regeneratorRuntime().wrap(function _callee2$(_context3) {
|
|
71
|
+
while (1) {
|
|
72
|
+
switch (_context3.prev = _context3.next) {
|
|
73
|
+
case 0:
|
|
74
|
+
logger.info("Running ts compile...");
|
|
75
|
+
rootTsconfigPath = path.join(appDirectory, TS_CONFIG_FILENAME);
|
|
76
|
+
patterns = compileOptions.patterns;
|
|
77
|
+
ts = new TypescriptLoader({
|
|
78
|
+
appDirectory: appDirectory
|
|
79
|
+
}).load();
|
|
80
|
+
createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
81
|
+
formatHost = getFormatHost(ts);
|
|
82
|
+
_iterator = _createForOfIteratorHelper(patterns);
|
|
83
|
+
_context3.prev = 7;
|
|
84
|
+
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() {
|
|
85
|
+
var pattern, _pattern$tsconfigPath, tsconfigPath, from, to, basename, targetDir, alias, aliasOption, _aliasOption$paths, paths, _aliasOption$absolute, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics;
|
|
86
|
+
|
|
87
|
+
return _regeneratorRuntime().wrap(function _loop$(_context2) {
|
|
88
|
+
while (1) {
|
|
89
|
+
switch (_context2.prev = _context2.next) {
|
|
90
|
+
case 0:
|
|
91
|
+
pattern = _step.value;
|
|
92
|
+
_pattern$tsconfigPath = pattern.tsconfigPath, tsconfigPath = _pattern$tsconfigPath === void 0 ? rootTsconfigPath : _pattern$tsconfigPath, from = pattern.from, to = pattern.to;
|
|
93
|
+
basename = path.basename(from);
|
|
94
|
+
targetDir = path.join(to, basename);
|
|
95
|
+
logger.info("Compile ".concat(from));
|
|
96
|
+
alias = modernConfig.source.alias;
|
|
97
|
+
aliasOption = getAlias(alias || {}, {
|
|
98
|
+
appDirectory: appDirectory,
|
|
99
|
+
tsconfigPath: tsconfigPath
|
|
100
|
+
});
|
|
101
|
+
_aliasOption$paths = aliasOption.paths, paths = _aliasOption$paths === void 0 ? {} : _aliasOption$paths, _aliasOption$absolute = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption$absolute === void 0 ? './' : _aliasOption$absolute;
|
|
102
|
+
_readTsConfigByFile = readTsConfigByFile(tsconfigPath), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
|
|
103
|
+
rootNames = fileNames.filter(function (fileName) {
|
|
104
|
+
return fileName.endsWith('.d.ts') || fileName.includes(from);
|
|
105
|
+
});
|
|
106
|
+
program = createProgram.call(ts, {
|
|
107
|
+
rootNames: rootNames,
|
|
108
|
+
projectReferences: projectReferences,
|
|
109
|
+
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
110
|
+
outDir: targetDir
|
|
111
|
+
})
|
|
112
|
+
});
|
|
113
|
+
tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
114
|
+
emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
115
|
+
before: [tsconfigPathsPlugin]
|
|
116
|
+
});
|
|
117
|
+
allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
118
|
+
|
|
119
|
+
if (allDiagnostics.length > 0) {
|
|
120
|
+
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
_context2.next = 17;
|
|
125
|
+
return copyFiles(from, to, tsconfigPath);
|
|
126
|
+
|
|
127
|
+
case 17:
|
|
128
|
+
case "end":
|
|
129
|
+
return _context2.stop();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}, _loop);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
_iterator.s();
|
|
136
|
+
|
|
137
|
+
case 10:
|
|
138
|
+
if ((_step = _iterator.n()).done) {
|
|
139
|
+
_context3.next = 14;
|
|
140
|
+
break;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return _context3.delegateYield(_loop(), "t0", 12);
|
|
144
|
+
|
|
145
|
+
case 12:
|
|
146
|
+
_context3.next = 10;
|
|
147
|
+
break;
|
|
148
|
+
|
|
149
|
+
case 14:
|
|
150
|
+
_context3.next = 19;
|
|
151
|
+
break;
|
|
152
|
+
|
|
153
|
+
case 16:
|
|
154
|
+
_context3.prev = 16;
|
|
155
|
+
_context3.t1 = _context3["catch"](7);
|
|
156
|
+
|
|
157
|
+
_iterator.e(_context3.t1);
|
|
158
|
+
|
|
159
|
+
case 19:
|
|
160
|
+
_context3.prev = 19;
|
|
161
|
+
|
|
162
|
+
_iterator.f();
|
|
163
|
+
|
|
164
|
+
return _context3.finish(19);
|
|
165
|
+
|
|
166
|
+
case 22:
|
|
167
|
+
case "end":
|
|
168
|
+
return _context3.stop();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}, _callee2, null, [[7, 16, 19, 22]]);
|
|
172
|
+
}));
|
|
173
|
+
|
|
174
|
+
return function compileByTs(_x4, _x5, _x6) {
|
|
175
|
+
return _ref3.apply(this, arguments);
|
|
176
|
+
};
|
|
177
|
+
}();
|
|
178
|
+
|
|
179
|
+
var getFormatHost = function getFormatHost(ts) {
|
|
180
|
+
return {
|
|
181
|
+
getCanonicalFileName: function getCanonicalFileName(path) {
|
|
182
|
+
return path;
|
|
183
|
+
},
|
|
184
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
185
|
+
getNewLine: function getNewLine() {
|
|
186
|
+
return ts.sys.newLine;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
};
|