@modern-js/server-utils 1.17.0 → 1.18.1-alpha.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 +32 -0
- package/dist/js/modern/common/index.js +35 -0
- package/dist/js/modern/{babel.js → compilers/babel/index.js} +37 -0
- package/dist/js/modern/compilers/typescript/index.js +107 -0
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +184 -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 +53 -0
- package/dist/js/node/{babel.js → compilers/babel/index.js} +51 -3
- package/dist/js/node/compilers/typescript/index.js +127 -0
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +199 -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 +79 -0
- package/dist/js/treeshaking/compilers/babel/index.js +159 -0
- package/dist/js/treeshaking/compilers/typescript/index.js +180 -0
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +202 -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 +11 -8
- package/dist/js/treeshaking/babel.js +0 -74
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
# @modern-js/server-utils
|
|
2
2
|
|
|
3
|
+
## 1.18.1-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- fb02c81: fix: alias should take precedence over package name
|
|
8
|
+
fix: 编译时,别名的优先级应该高于包名
|
|
9
|
+
- Updated dependencies [c1a4d9b]
|
|
10
|
+
- Updated dependencies [9fcfbd4]
|
|
11
|
+
- Updated dependencies [6c2c745]
|
|
12
|
+
- @modern-js/plugin@1.18.1-alpha.0
|
|
13
|
+
- @modern-js/utils@1.18.1-alpha.0
|
|
14
|
+
- @modern-js/babel-preset-lib@1.18.1-alpha.0
|
|
15
|
+
- @modern-js/babel-compiler@1.18.1-alpha.0
|
|
16
|
+
|
|
17
|
+
## 1.18.0
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 2b7406d: feat: use typescript instead of babel as typescript compiler in server
|
|
22
|
+
feat: 服务端,增加 typescript 作为 typescipt 编译器
|
|
23
|
+
- 0a4d622: fix: compile ts with dynamic import should replace to correct alias name
|
|
24
|
+
fix: 编译 ts 时,动态导入的路径应该替换为正确的别名
|
|
25
|
+
- 60a2e3a: feat: add compiler option for server
|
|
26
|
+
feat: 为 server 添加编译选项
|
|
27
|
+
- Updated dependencies [8280920]
|
|
28
|
+
- Updated dependencies [5227370]
|
|
29
|
+
- Updated dependencies [7928bae]
|
|
30
|
+
- @modern-js/utils@1.18.0
|
|
31
|
+
- @modern-js/babel-preset-lib@1.18.0
|
|
32
|
+
- @modern-js/babel-compiler@1.18.0
|
|
33
|
+
- @modern-js/plugin@1.18.0
|
|
34
|
+
|
|
3
35
|
## 1.17.0
|
|
4
36
|
|
|
5
37
|
### Patch Changes
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
var _modernConfig$server;
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
sourceDirs,
|
|
22
|
+
distDir,
|
|
23
|
+
tsconfigPath
|
|
24
|
+
} = compileOptions;
|
|
25
|
+
validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
|
|
26
|
+
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
27
|
+
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
|
|
28
|
+
const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
|
|
29
|
+
|
|
30
|
+
if (!isTsProject || compiler === 'babel') {
|
|
31
|
+
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
32
|
+
} else {
|
|
33
|
+
await compileByTs(appDirectory, modernConfig, compileOptions);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -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,107 @@
|
|
|
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 sourcePosixPaths = sourceDirs.map(sourceDir => sourceDir.split(path.sep).join(path.posix.sep));
|
|
71
|
+
const rootNames = fileNames.filter(fileName => {
|
|
72
|
+
return fileName.endsWith('.d.ts') || sourcePosixPaths.some(sourceDir => {
|
|
73
|
+
return fileName.includes(sourceDir);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
const program = createProgram.call(ts, {
|
|
77
|
+
rootNames,
|
|
78
|
+
projectReferences,
|
|
79
|
+
options: _objectSpread({
|
|
80
|
+
rootDir: appDirectory,
|
|
81
|
+
outDir: distDir
|
|
82
|
+
}, options)
|
|
83
|
+
});
|
|
84
|
+
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
85
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
86
|
+
before: [tsconfigPathsPlugin]
|
|
87
|
+
});
|
|
88
|
+
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
89
|
+
|
|
90
|
+
if (allDiagnostics.length > 0) {
|
|
91
|
+
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost)); // eslint-disable-next-line no-process-exit
|
|
92
|
+
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const source of sourceDirs) {
|
|
97
|
+
await copyFiles(source, distDir, tsconfigPath);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const getFormatHost = ts => {
|
|
102
|
+
return {
|
|
103
|
+
getCanonicalFileName: path => path,
|
|
104
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
105
|
+
getNewLine: () => ts.sys.newLine
|
|
106
|
+
};
|
|
107
|
+
};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import * as os from 'os';
|
|
2
|
+
import path, { dirname, posix } from 'path';
|
|
3
|
+
import * as ts from 'typescript';
|
|
4
|
+
import { createMatchPath } from 'tsconfig-paths';
|
|
5
|
+
|
|
6
|
+
const isRegExpKey = str => {
|
|
7
|
+
return str.startsWith('^') || str.endsWith('$');
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
11
|
+
// exclude absolute path and alias
|
|
12
|
+
if (filePath.startsWith('.') || filePath.startsWith('..')) {
|
|
13
|
+
return path.resolve(baseUrl, filePath);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return filePath;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
20
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
21
|
+
if (isRegExpKey(key)) {
|
|
22
|
+
const regexp = new RegExp(key);
|
|
23
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
24
|
+
o.push([regexp, aliasPath]);
|
|
25
|
+
} else {
|
|
26
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
27
|
+
o.push([key, aliasPath]);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return o;
|
|
31
|
+
}, []);
|
|
32
|
+
const cacheMap = new Map(); // eslint-disable-next-line consistent-return
|
|
33
|
+
|
|
34
|
+
return requestedModule => {
|
|
35
|
+
if (cacheMap.has(requestedModule)) {
|
|
36
|
+
return cacheMap.get(requestedModule);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
for (const [key, value] of aliasPairs) {
|
|
40
|
+
if (key instanceof RegExp) {
|
|
41
|
+
if (key.test(requestedModule)) {
|
|
42
|
+
cacheMap.set(requestedModule, value);
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (requestedModule === key) {
|
|
48
|
+
cacheMap.set(requestedModule, value);
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const isDynamicImport = (tsBinary, node) => {
|
|
56
|
+
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
60
|
+
const tsPaths = {};
|
|
61
|
+
const alias = {};
|
|
62
|
+
Object.keys(paths).forEach(key => {
|
|
63
|
+
if (Array.isArray(paths[key])) {
|
|
64
|
+
tsPaths[key] = paths[key];
|
|
65
|
+
} else {
|
|
66
|
+
alias[key] = paths[key];
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
70
|
+
const matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
|
|
71
|
+
|
|
72
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
73
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
74
|
+
|
|
75
|
+
if (result) {
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return matchAliasPath(requestedModule);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
if (Object.keys(paths).length === 0) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return ctx => {
|
|
87
|
+
return sf => {
|
|
88
|
+
const visitNode = node => {
|
|
89
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
90
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
91
|
+
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
92
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
93
|
+
|
|
94
|
+
if (!result) {
|
|
95
|
+
return node;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
102
|
+
try {
|
|
103
|
+
var _node$moduleSpecifier;
|
|
104
|
+
|
|
105
|
+
const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node$moduleSpecifier = node.moduleSpecifier) === null || _node$moduleSpecifier === void 0 ? void 0 : _node$moduleSpecifier.getText();
|
|
106
|
+
|
|
107
|
+
if (!importPathWithQuotes) {
|
|
108
|
+
return node;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
112
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
113
|
+
|
|
114
|
+
if (!result) {
|
|
115
|
+
return node;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
119
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
120
|
+
|
|
121
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
122
|
+
return tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
123
|
+
} else {
|
|
124
|
+
return tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
125
|
+
}
|
|
126
|
+
} catch (_unused) {
|
|
127
|
+
return node;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
} // fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
138
|
+
|
|
139
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
140
|
+
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
141
|
+
|
|
142
|
+
if (!result) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (os.platform() === 'win32') {
|
|
147
|
+
result = result.replace(/\\/g, '/');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!path.isAbsolute(result)) {
|
|
151
|
+
// handle alias to alias
|
|
152
|
+
if (!result.startsWith('.') && !result.startsWith('..')) {
|
|
153
|
+
try {
|
|
154
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
155
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
156
|
+
const packagePath = require.resolve(result, {
|
|
157
|
+
paths: [process.cwd(), ...module.paths]
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
if (packagePath) {
|
|
161
|
+
// eslint-disable-next-line consistent-return
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
} catch (_unused2) {}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
169
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
170
|
+
const packagePath = require.resolve(text, {
|
|
171
|
+
paths: [process.cwd(), ...module.paths]
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (packagePath) {
|
|
175
|
+
// eslint-disable-next-line consistent-return
|
|
176
|
+
return text;
|
|
177
|
+
}
|
|
178
|
+
} catch (_unused3) {}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const resolvedPath = posix.relative(dirname(sf.fileName), result) || './'; // eslint-disable-next-line consistent-return
|
|
182
|
+
|
|
183
|
+
return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
|
|
184
|
+
}
|
|
@@ -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,53 @@
|
|
|
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
|
+
var _modernConfig$server;
|
|
35
|
+
|
|
36
|
+
const {
|
|
37
|
+
sourceDirs,
|
|
38
|
+
distDir,
|
|
39
|
+
tsconfigPath
|
|
40
|
+
} = compileOptions;
|
|
41
|
+
validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
|
|
42
|
+
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
43
|
+
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
|
|
44
|
+
const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
|
|
45
|
+
|
|
46
|
+
if (!isTsProject || compiler === 'babel') {
|
|
47
|
+
await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
|
|
48
|
+
} else {
|
|
49
|
+
await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
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;
|