@modern-js/server-utils 1.15.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/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 +9 -3
- package/dist/js/treeshaking/babel.js +0 -74
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import { fs } from '@modern-js/utils';
|
|
3
|
+
import recursive from 'recursive-readdir';
|
|
4
|
+
import { compileByTs } from "../compilers/typescript";
|
|
5
|
+
import { compileByBabel } from "../compilers/babel";
|
|
6
|
+
export const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
7
|
+
export const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
8
|
+
export const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
9
|
+
const tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
|
|
10
|
+
const isTsProject = await fs.pathExists(tsConifgPath);
|
|
11
|
+
|
|
12
|
+
if (isTsProject) {
|
|
13
|
+
await compileByTs(appDirectory, modernConfig, compileOptions);
|
|
14
|
+
} else {
|
|
15
|
+
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
export const recursiveReadTsFiles = async dirname => {
|
|
19
|
+
const ignoreFunc = filename => {
|
|
20
|
+
const extname = path.extname(filename);
|
|
21
|
+
return !['.ts', '.d.ts'].includes(extname);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const files = await recursive(dirname, [ignoreFunc]);
|
|
25
|
+
return files;
|
|
26
|
+
};
|
|
@@ -4,8 +4,11 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
4
4
|
|
|
5
5
|
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; }
|
|
6
6
|
|
|
7
|
+
import * as path from 'path';
|
|
7
8
|
import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
|
|
8
9
|
import { fs, json5, getAlias, applyOptionsChain } from '@modern-js/utils';
|
|
10
|
+
import { compiler } from '@modern-js/babel-compiler';
|
|
11
|
+
import { FILE_EXTENSIONS } from "../../common";
|
|
9
12
|
export * from '@babel/core';
|
|
10
13
|
export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
11
14
|
// 如果不存在,则返回 noExistReturn
|
|
@@ -80,4 +83,41 @@ export const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME:
|
|
|
80
83
|
|
|
81
84
|
const userBabelConfig = modernConfig.tools.babel;
|
|
82
85
|
return applyUserBabelConfig(internalBabelConfig, userBabelConfig);
|
|
86
|
+
};
|
|
87
|
+
export const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
|
|
88
|
+
const {
|
|
89
|
+
patterns
|
|
90
|
+
} = compileOptions;
|
|
91
|
+
const results = await Promise.all(patterns.map(async pattern => {
|
|
92
|
+
const {
|
|
93
|
+
from,
|
|
94
|
+
to,
|
|
95
|
+
tsconfigPath
|
|
96
|
+
} = pattern;
|
|
97
|
+
const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
98
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
99
|
+
syntax: 'es6+',
|
|
100
|
+
type: 'commonjs'
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (await fs.pathExists(from)) {
|
|
104
|
+
const basename = path.basename(from);
|
|
105
|
+
const targetDir = path.join(to, basename);
|
|
106
|
+
await fs.copy(from, targetDir, {
|
|
107
|
+
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return compiler({
|
|
112
|
+
rootDir: appDirectory,
|
|
113
|
+
distDir: to,
|
|
114
|
+
sourceDir: from,
|
|
115
|
+
extensions: FILE_EXTENSIONS
|
|
116
|
+
}, babelConfig);
|
|
117
|
+
}));
|
|
118
|
+
results.forEach(result => {
|
|
119
|
+
if (result.code === 1) {
|
|
120
|
+
throw new Error(result.message);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
83
123
|
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
|
|
3
|
+
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; }
|
|
4
|
+
|
|
5
|
+
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; }
|
|
6
|
+
|
|
7
|
+
// from to tsconfigpath
|
|
8
|
+
// root dir
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { logger, getAlias, fs } from '@modern-js/utils';
|
|
11
|
+
import { TS_CONFIG_FILENAME } from "../../common";
|
|
12
|
+
import { TypescriptLoader } from "./typescript-loader";
|
|
13
|
+
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
14
|
+
import ts from 'typescript';
|
|
15
|
+
|
|
16
|
+
const readTsConfigByFile = tsConfigFile => {
|
|
17
|
+
const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
|
|
18
|
+
const {
|
|
19
|
+
options,
|
|
20
|
+
fileNames,
|
|
21
|
+
projectReferences
|
|
22
|
+
} = parsedCmd;
|
|
23
|
+
return {
|
|
24
|
+
options,
|
|
25
|
+
fileNames,
|
|
26
|
+
projectReferences
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const copyFiles = async (from, to, tsconfigPath) => {
|
|
31
|
+
if (await fs.pathExists(from)) {
|
|
32
|
+
const basename = path.basename(from);
|
|
33
|
+
const targetDir = path.join(to, basename);
|
|
34
|
+
await fs.copy(from, targetDir, {
|
|
35
|
+
filter: src => !['.ts'].includes(path.extname(src)) && src !== tsconfigPath
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}; // TODO: 分离特殊的业务逻辑,通用化
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
export const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
|
|
42
|
+
logger.info(`Running ts compile...`);
|
|
43
|
+
const rootTsconfigPath = path.join(appDirectory, TS_CONFIG_FILENAME);
|
|
44
|
+
const {
|
|
45
|
+
patterns
|
|
46
|
+
} = compileOptions;
|
|
47
|
+
const ts = new TypescriptLoader({
|
|
48
|
+
appDirectory
|
|
49
|
+
}).load();
|
|
50
|
+
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
51
|
+
const formatHost = getFormatHost(ts);
|
|
52
|
+
|
|
53
|
+
for (const pattern of patterns) {
|
|
54
|
+
const {
|
|
55
|
+
tsconfigPath = rootTsconfigPath,
|
|
56
|
+
from,
|
|
57
|
+
to
|
|
58
|
+
} = pattern;
|
|
59
|
+
const basename = path.basename(from);
|
|
60
|
+
const targetDir = path.join(to, basename);
|
|
61
|
+
logger.info(`Compile ${from}`);
|
|
62
|
+
const {
|
|
63
|
+
alias
|
|
64
|
+
} = modernConfig.source;
|
|
65
|
+
const aliasOption = getAlias(alias || {}, {
|
|
66
|
+
appDirectory,
|
|
67
|
+
tsconfigPath
|
|
68
|
+
});
|
|
69
|
+
const {
|
|
70
|
+
paths = {},
|
|
71
|
+
absoluteBaseUrl = './'
|
|
72
|
+
} = aliasOption;
|
|
73
|
+
const {
|
|
74
|
+
options,
|
|
75
|
+
fileNames,
|
|
76
|
+
projectReferences
|
|
77
|
+
} = readTsConfigByFile(tsconfigPath);
|
|
78
|
+
const rootNames = fileNames.filter(fileName => {
|
|
79
|
+
return fileName.endsWith('.d.ts') || fileName.includes(from);
|
|
80
|
+
});
|
|
81
|
+
let program = createProgram.call(ts, {
|
|
82
|
+
rootNames: rootNames,
|
|
83
|
+
projectReferences,
|
|
84
|
+
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
85
|
+
outDir: targetDir
|
|
86
|
+
})
|
|
87
|
+
});
|
|
88
|
+
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
89
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
90
|
+
before: [tsconfigPathsPlugin]
|
|
91
|
+
});
|
|
92
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
93
|
+
|
|
94
|
+
if (allDiagnostics.length > 0) {
|
|
95
|
+
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
await copyFiles(from, to, tsconfigPath);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const getFormatHost = ts => {
|
|
104
|
+
return {
|
|
105
|
+
getCanonicalFileName: path => path,
|
|
106
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
107
|
+
getNewLine: () => ts.sys.newLine
|
|
108
|
+
};
|
|
109
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as os from 'os';
|
|
2
|
+
import path, { dirname, posix } from 'path';
|
|
3
|
+
import { createMatchPath } from 'tsconfig-paths';
|
|
4
|
+
|
|
5
|
+
const isRegExpKey = str => {
|
|
6
|
+
return str.startsWith('^') || str.endsWith('$');
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
10
|
+
if (path.isAbsolute(filePath)) {
|
|
11
|
+
return filePath;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return path.resolve(baseUrl, filePath);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
18
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
19
|
+
if (isRegExpKey(key)) {
|
|
20
|
+
const regexp = new RegExp(key);
|
|
21
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
22
|
+
o.push([regexp, aliasPath]);
|
|
23
|
+
} else {
|
|
24
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
25
|
+
o.push([key, aliasPath]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return o;
|
|
29
|
+
}, []);
|
|
30
|
+
return requestedModule => {
|
|
31
|
+
for (const [key, value] of aliasPairs) {
|
|
32
|
+
if (key instanceof RegExp) {
|
|
33
|
+
if (key.test(requestedModule)) {
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (requestedModule === key) {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
46
|
+
const tsPaths = {};
|
|
47
|
+
const alias = {};
|
|
48
|
+
Object.keys(paths).forEach(key => {
|
|
49
|
+
if (Array.isArray(paths[key])) {
|
|
50
|
+
tsPaths[key] = paths[key];
|
|
51
|
+
} else {
|
|
52
|
+
alias[key] = paths[key];
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
56
|
+
const matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
|
|
57
|
+
|
|
58
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
59
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
60
|
+
|
|
61
|
+
if (result) {
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return matchAliasPath(requestedModule);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
if (Object.keys(paths).length === 0) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return ctx => {
|
|
73
|
+
return sf => {
|
|
74
|
+
const visitNode = node => {
|
|
75
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
76
|
+
try {
|
|
77
|
+
const importPathWithQuotes = node.moduleSpecifier && node.moduleSpecifier.getText();
|
|
78
|
+
|
|
79
|
+
if (!importPathWithQuotes) {
|
|
80
|
+
return node;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
84
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
85
|
+
|
|
86
|
+
if (!result) {
|
|
87
|
+
return node;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
91
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
92
|
+
|
|
93
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
94
|
+
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
95
|
+
} else {
|
|
96
|
+
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
97
|
+
}
|
|
98
|
+
} catch (_unused) {
|
|
99
|
+
return node;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
110
|
+
|
|
111
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
112
|
+
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
113
|
+
|
|
114
|
+
if (!result) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (os.platform() === 'win32') {
|
|
119
|
+
result = result.replace(/\\/g, '/');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (!path.isAbsolute(result)) {
|
|
123
|
+
try {
|
|
124
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
125
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
126
|
+
const packagePath = require.resolve(text, {
|
|
127
|
+
paths: [process.cwd(), ...module.paths]
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
if (packagePath) {
|
|
131
|
+
return text;
|
|
132
|
+
}
|
|
133
|
+
} catch (_unused2) {}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
|
|
137
|
+
return resolvedPath[0] === '.' ? resolvedPath : './' + resolvedPath;
|
|
138
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
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; }
|
|
2
|
+
|
|
3
|
+
export class TypescriptLoader {
|
|
4
|
+
constructor({
|
|
5
|
+
appDirectory
|
|
6
|
+
}) {
|
|
7
|
+
_defineProperty(this, "tsBinary", void 0);
|
|
8
|
+
|
|
9
|
+
_defineProperty(this, "appDirectory", void 0);
|
|
10
|
+
|
|
11
|
+
this.appDirectory = appDirectory;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
load() {
|
|
15
|
+
if (this.tsBinary) {
|
|
16
|
+
return this.tsBinary;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const tsPath = require.resolve('typescript', {
|
|
21
|
+
paths: [this.appDirectory || process.cwd()]
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const ts = require(tsPath);
|
|
25
|
+
|
|
26
|
+
return ts;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
}
|
package/dist/js/modern/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from "./babel";
|
|
1
|
+
export * from "./compilers/babel";
|
|
2
|
+
export { compile } from "./common";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.recursiveReadTsFiles = exports.compile = exports.TS_CONFIG_FILENAME = exports.FILE_EXTENSIONS = void 0;
|
|
7
|
+
|
|
8
|
+
var path = _interopRequireWildcard(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _recursiveReaddir = _interopRequireDefault(require("recursive-readdir"));
|
|
13
|
+
|
|
14
|
+
var _typescript = require("../compilers/typescript");
|
|
15
|
+
|
|
16
|
+
var _babel = require("../compilers/babel");
|
|
17
|
+
|
|
18
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
+
|
|
20
|
+
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); }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
25
|
+
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
26
|
+
const TS_CONFIG_FILENAME = 'tsconfig.json';
|
|
27
|
+
exports.TS_CONFIG_FILENAME = TS_CONFIG_FILENAME;
|
|
28
|
+
|
|
29
|
+
const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
30
|
+
const tsConifgPath = path.join(appDirectory, TS_CONFIG_FILENAME);
|
|
31
|
+
const isTsProject = await _utils.fs.pathExists(tsConifgPath);
|
|
32
|
+
|
|
33
|
+
if (isTsProject) {
|
|
34
|
+
await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
|
|
35
|
+
} else {
|
|
36
|
+
await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
exports.compile = compile;
|
|
41
|
+
|
|
42
|
+
const recursiveReadTsFiles = async dirname => {
|
|
43
|
+
const ignoreFunc = filename => {
|
|
44
|
+
const extname = path.extname(filename);
|
|
45
|
+
return !['.ts', '.d.ts'].includes(extname);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const files = await (0, _recursiveReaddir.default)(dirname, [ignoreFunc]);
|
|
49
|
+
return files;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.recursiveReadTsFiles = recursiveReadTsFiles;
|
|
@@ -7,14 +7,21 @@ var _exportNames = {
|
|
|
7
7
|
readTsConfig: true,
|
|
8
8
|
existTsConfigFile: true,
|
|
9
9
|
getBabelConfig: true,
|
|
10
|
-
resolveBabelConfig: true
|
|
10
|
+
resolveBabelConfig: true,
|
|
11
|
+
compileByBabel: true
|
|
11
12
|
};
|
|
12
|
-
exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile = void 0;
|
|
13
|
+
exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile = exports.compileByBabel = void 0;
|
|
14
|
+
|
|
15
|
+
var path = _interopRequireWildcard(require("path"));
|
|
13
16
|
|
|
14
17
|
var _babelPresetLib = require("@modern-js/babel-preset-lib");
|
|
15
18
|
|
|
16
19
|
var _utils = require("@modern-js/utils");
|
|
17
20
|
|
|
21
|
+
var _babelCompiler = require("@modern-js/babel-compiler");
|
|
22
|
+
|
|
23
|
+
var _common = require("../../common");
|
|
24
|
+
|
|
18
25
|
var _core = require("@babel/core");
|
|
19
26
|
|
|
20
27
|
Object.keys(_core).forEach(function (key) {
|
|
@@ -29,6 +36,10 @@ Object.keys(_core).forEach(function (key) {
|
|
|
29
36
|
});
|
|
30
37
|
});
|
|
31
38
|
|
|
39
|
+
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); }
|
|
40
|
+
|
|
41
|
+
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; }
|
|
42
|
+
|
|
32
43
|
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; }
|
|
33
44
|
|
|
34
45
|
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; }
|
|
@@ -120,4 +131,44 @@ const resolveBabelConfig = (appDirectory, modernConfig, option // FIXME: babel t
|
|
|
120
131
|
return (0, _babelPresetLib.applyUserBabelConfig)(internalBabelConfig, userBabelConfig);
|
|
121
132
|
};
|
|
122
133
|
|
|
123
|
-
exports.resolveBabelConfig = resolveBabelConfig;
|
|
134
|
+
exports.resolveBabelConfig = resolveBabelConfig;
|
|
135
|
+
|
|
136
|
+
const compileByBabel = async (appDirectory, modernConfig, compileOptions) => {
|
|
137
|
+
const {
|
|
138
|
+
patterns
|
|
139
|
+
} = compileOptions;
|
|
140
|
+
const results = await Promise.all(patterns.map(async pattern => {
|
|
141
|
+
const {
|
|
142
|
+
from,
|
|
143
|
+
to,
|
|
144
|
+
tsconfigPath
|
|
145
|
+
} = pattern;
|
|
146
|
+
const babelConfig = resolveBabelConfig(appDirectory, modernConfig, {
|
|
147
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
148
|
+
syntax: 'es6+',
|
|
149
|
+
type: 'commonjs'
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (await _utils.fs.pathExists(from)) {
|
|
153
|
+
const basename = path.basename(from);
|
|
154
|
+
const targetDir = path.join(to, basename);
|
|
155
|
+
await _utils.fs.copy(from, targetDir, {
|
|
156
|
+
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return (0, _babelCompiler.compiler)({
|
|
161
|
+
rootDir: appDirectory,
|
|
162
|
+
distDir: to,
|
|
163
|
+
sourceDir: from,
|
|
164
|
+
extensions: _common.FILE_EXTENSIONS
|
|
165
|
+
}, babelConfig);
|
|
166
|
+
}));
|
|
167
|
+
results.forEach(result => {
|
|
168
|
+
if (result.code === 1) {
|
|
169
|
+
throw new Error(result.message);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
exports.compileByBabel = compileByBabel;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.compileByTs = void 0;
|
|
7
|
+
|
|
8
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("@modern-js/utils");
|
|
11
|
+
|
|
12
|
+
var _common = require("../../common");
|
|
13
|
+
|
|
14
|
+
var _typescriptLoader = require("./typescript-loader");
|
|
15
|
+
|
|
16
|
+
var _tsconfigPathsPlugin = require("./tsconfig-paths-plugin");
|
|
17
|
+
|
|
18
|
+
var _typescript = _interopRequireDefault(require("typescript"));
|
|
19
|
+
|
|
20
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
|
+
|
|
22
|
+
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; }
|
|
23
|
+
|
|
24
|
+
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; }
|
|
25
|
+
|
|
26
|
+
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; }
|
|
27
|
+
|
|
28
|
+
const readTsConfigByFile = tsConfigFile => {
|
|
29
|
+
const parsedCmd = _typescript.default.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, _typescript.default.sys);
|
|
30
|
+
|
|
31
|
+
const {
|
|
32
|
+
options,
|
|
33
|
+
fileNames,
|
|
34
|
+
projectReferences
|
|
35
|
+
} = parsedCmd;
|
|
36
|
+
return {
|
|
37
|
+
options,
|
|
38
|
+
fileNames,
|
|
39
|
+
projectReferences
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const copyFiles = async (from, to, tsconfigPath) => {
|
|
44
|
+
if (await _utils.fs.pathExists(from)) {
|
|
45
|
+
const basename = _path.default.basename(from);
|
|
46
|
+
|
|
47
|
+
const targetDir = _path.default.join(to, basename);
|
|
48
|
+
|
|
49
|
+
await _utils.fs.copy(from, targetDir, {
|
|
50
|
+
filter: src => !['.ts'].includes(_path.default.extname(src)) && src !== tsconfigPath
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}; // TODO: 分离特殊的业务逻辑,通用化
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const compileByTs = async (appDirectory, modernConfig, compileOptions) => {
|
|
57
|
+
_utils.logger.info(`Running ts compile...`);
|
|
58
|
+
|
|
59
|
+
const rootTsconfigPath = _path.default.join(appDirectory, _common.TS_CONFIG_FILENAME);
|
|
60
|
+
|
|
61
|
+
const {
|
|
62
|
+
patterns
|
|
63
|
+
} = compileOptions;
|
|
64
|
+
const ts = new _typescriptLoader.TypescriptLoader({
|
|
65
|
+
appDirectory
|
|
66
|
+
}).load();
|
|
67
|
+
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
68
|
+
const formatHost = getFormatHost(ts);
|
|
69
|
+
|
|
70
|
+
for (const pattern of patterns) {
|
|
71
|
+
const {
|
|
72
|
+
tsconfigPath = rootTsconfigPath,
|
|
73
|
+
from,
|
|
74
|
+
to
|
|
75
|
+
} = pattern;
|
|
76
|
+
|
|
77
|
+
const basename = _path.default.basename(from);
|
|
78
|
+
|
|
79
|
+
const targetDir = _path.default.join(to, basename);
|
|
80
|
+
|
|
81
|
+
_utils.logger.info(`Compile ${from}`);
|
|
82
|
+
|
|
83
|
+
const {
|
|
84
|
+
alias
|
|
85
|
+
} = modernConfig.source;
|
|
86
|
+
const aliasOption = (0, _utils.getAlias)(alias || {}, {
|
|
87
|
+
appDirectory,
|
|
88
|
+
tsconfigPath
|
|
89
|
+
});
|
|
90
|
+
const {
|
|
91
|
+
paths = {},
|
|
92
|
+
absoluteBaseUrl = './'
|
|
93
|
+
} = aliasOption;
|
|
94
|
+
const {
|
|
95
|
+
options,
|
|
96
|
+
fileNames,
|
|
97
|
+
projectReferences
|
|
98
|
+
} = readTsConfigByFile(tsconfigPath);
|
|
99
|
+
const rootNames = fileNames.filter(fileName => {
|
|
100
|
+
return fileName.endsWith('.d.ts') || fileName.includes(from);
|
|
101
|
+
});
|
|
102
|
+
let program = createProgram.call(ts, {
|
|
103
|
+
rootNames: rootNames,
|
|
104
|
+
projectReferences,
|
|
105
|
+
options: _objectSpread(_objectSpread({}, options), {}, {
|
|
106
|
+
outDir: targetDir
|
|
107
|
+
})
|
|
108
|
+
});
|
|
109
|
+
const tsconfigPathsPlugin = (0, _tsconfigPathsPlugin.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
|
|
110
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
111
|
+
before: [tsconfigPathsPlugin]
|
|
112
|
+
});
|
|
113
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
114
|
+
|
|
115
|
+
if (allDiagnostics.length > 0) {
|
|
116
|
+
_utils.logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
117
|
+
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
await copyFiles(from, to, tsconfigPath);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
exports.compileByTs = compileByTs;
|
|
126
|
+
|
|
127
|
+
const getFormatHost = ts => {
|
|
128
|
+
return {
|
|
129
|
+
getCanonicalFileName: path => path,
|
|
130
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
131
|
+
getNewLine: () => ts.sys.newLine
|
|
132
|
+
};
|
|
133
|
+
};
|