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