@modern-js/server-utils 1.21.5 → 2.0.0-beta.1
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 +58 -16
- package/dist/js/modern/common/index.js +0 -5
- package/dist/js/modern/compilers/babel/index.js +10 -14
- package/dist/js/modern/compilers/typescript/index.js +4 -16
- package/dist/js/modern/compilers/typescript/tsconfig-paths-plugin.js +11 -38
- package/dist/js/modern/compilers/typescript/typescript-loader.js +0 -8
- package/dist/js/node/common/index.js +0 -13
- package/dist/js/node/compilers/babel/index.js +9 -33
- package/dist/js/node/compilers/typescript/index.js +3 -27
- package/dist/js/node/compilers/typescript/tsconfig-paths-plugin.js +11 -45
- package/dist/js/node/compilers/typescript/typescript-loader.js +0 -10
- package/dist/js/node/index.js +0 -3
- package/dist/js/treeshaking/common/index.js +0 -14
- package/dist/js/treeshaking/compilers/babel/index.js +12 -20
- package/dist/js/treeshaking/compilers/typescript/index.js +7 -34
- package/dist/js/treeshaking/compilers/typescript/tsconfig-paths-plugin.js +14 -45
- package/dist/js/treeshaking/compilers/typescript/typescript-loader.js +0 -9
- package/package.json +10 -33
package/CHANGELOG.md
CHANGED
|
@@ -1,31 +1,73 @@
|
|
|
1
1
|
# @modern-js/server-utils
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## 2.0.0-beta.1
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- dda38c9: chore: v2
|
|
4
8
|
|
|
5
9
|
### Patch Changes
|
|
6
10
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
- @modern-js/utils@1.21.5
|
|
11
|
+
- 9b915e0c10: fix: tsconfig-paths plugin's new node use old node flag
|
|
12
|
+
fix: tsconfig-paths 插件转换的新节点使用旧节点的 flag
|
|
13
|
+
- a8642da: fix(server-utils): incorrect babel-compiler version
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
fix(server-utils): 修复引用错误的 babel-compiler 版本的问题
|
|
13
16
|
|
|
14
|
-
|
|
17
|
+
- 14b712d: fix: use consistent alias type and default value across packages
|
|
18
|
+
|
|
19
|
+
fix: 在各个包中使用一致的 alias 类型定义和默认值
|
|
20
|
+
|
|
21
|
+
- Updated dependencies [92f0ead]
|
|
22
|
+
- Updated dependencies [edd1cfb1af]
|
|
23
|
+
- Updated dependencies [cc971eabfc]
|
|
24
|
+
- Updated dependencies [5b9049f]
|
|
25
|
+
- Updated dependencies [92004d1]
|
|
26
|
+
- Updated dependencies [b8bbe036c7]
|
|
27
|
+
- Updated dependencies [d5a31df781]
|
|
28
|
+
- Updated dependencies [dda38c9]
|
|
29
|
+
- Updated dependencies [3bbea92b2a]
|
|
30
|
+
- Updated dependencies [f179749]
|
|
31
|
+
- Updated dependencies [abf3421]
|
|
32
|
+
- Updated dependencies [543be9558e]
|
|
33
|
+
- Updated dependencies [14b712d]
|
|
34
|
+
- @modern-js/utils@2.0.0-beta.1
|
|
35
|
+
- @modern-js/plugin@2.0.0-beta.1
|
|
36
|
+
- @modern-js/babel-preset-lib@2.0.0-beta.1
|
|
37
|
+
- @modern-js/babel-compiler@2.0.0-beta.1
|
|
15
38
|
|
|
16
|
-
|
|
17
|
-
- @modern-js/babel-compiler@1.21.4
|
|
18
|
-
- @modern-js/plugin@1.21.4
|
|
19
|
-
- @modern-js/utils@1.21.4
|
|
39
|
+
## 2.0.0-beta.0
|
|
20
40
|
|
|
21
|
-
|
|
41
|
+
### Major Changes
|
|
42
|
+
|
|
43
|
+
- dda38c9: chore: v2
|
|
22
44
|
|
|
23
45
|
### Patch Changes
|
|
24
46
|
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
|
|
47
|
+
- 9b915e0c1: fix: tsconfig-paths plugin's new node use old node flag
|
|
48
|
+
fix: tsconfig-paths 插件转换的新节点使用旧节点的 flag
|
|
49
|
+
- a8642da: fix(server-utils): incorrect babel-compiler version
|
|
50
|
+
|
|
51
|
+
fix(server-utils): 修复引用错误的 babel-compiler 版本的问题
|
|
52
|
+
|
|
53
|
+
- 14b712d: fix: use consistent alias type and default value across packages
|
|
54
|
+
|
|
55
|
+
fix: 在各个包中使用一致的 alias 类型定义和默认值
|
|
56
|
+
|
|
57
|
+
- Updated dependencies [edd1cfb1a]
|
|
58
|
+
- Updated dependencies [cc971eabf]
|
|
59
|
+
- Updated dependencies [5b9049f]
|
|
60
|
+
- Updated dependencies [b8bbe036c]
|
|
61
|
+
- Updated dependencies [d5a31df78]
|
|
62
|
+
- Updated dependencies [dda38c9]
|
|
63
|
+
- Updated dependencies [3bbea92b2]
|
|
64
|
+
- Updated dependencies [abf3421]
|
|
65
|
+
- Updated dependencies [543be95]
|
|
66
|
+
- Updated dependencies [14b712d]
|
|
67
|
+
- @modern-js/utils@2.0.0-beta.0
|
|
68
|
+
- @modern-js/plugin@2.0.0-beta.0
|
|
69
|
+
- @modern-js/babel-preset-lib@2.0.0-beta.0
|
|
70
|
+
- @modern-js/babel-compiler@2.0.0-beta.0
|
|
29
71
|
|
|
30
72
|
## 1.21.2
|
|
31
73
|
|
|
@@ -3,20 +3,16 @@ import { fs } from '@modern-js/utils';
|
|
|
3
3
|
import { compileByTs } from "../compilers/typescript";
|
|
4
4
|
import { compileByBabel } from "../compilers/babel";
|
|
5
5
|
export const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
6
|
-
|
|
7
6
|
const validateAbsolutePath = (filename, message) => {
|
|
8
7
|
if (!path.isAbsolute(filename)) {
|
|
9
8
|
throw new Error(message);
|
|
10
9
|
}
|
|
11
10
|
};
|
|
12
|
-
|
|
13
11
|
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
14
12
|
filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
|
|
15
13
|
};
|
|
16
|
-
|
|
17
14
|
export const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
18
15
|
var _modernConfig$server;
|
|
19
|
-
|
|
20
16
|
const {
|
|
21
17
|
sourceDirs,
|
|
22
18
|
distDir,
|
|
@@ -26,7 +22,6 @@ export const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
|
26
22
|
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
27
23
|
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
|
|
28
24
|
const isTsProject = tsconfigPath && (await fs.pathExists(tsconfigPath));
|
|
29
|
-
|
|
30
25
|
if (!isTsProject || compiler === 'babel') {
|
|
31
26
|
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
32
27
|
} else {
|
|
@@ -1,12 +1,9 @@
|
|
|
1
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
2
|
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
3
|
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
4
|
import * as path from 'path';
|
|
8
5
|
import { getBabelChain, applyUserBabelConfig } from '@modern-js/babel-preset-lib';
|
|
9
|
-
import { fs, json5,
|
|
6
|
+
import { fs, json5, getAliasConfig } from '@modern-js/utils';
|
|
10
7
|
import { compiler } from '@modern-js/babel-compiler';
|
|
11
8
|
import { FILE_EXTENSIONS } from "../../common";
|
|
12
9
|
export * from '@babel/core';
|
|
@@ -15,7 +12,6 @@ export const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
|
15
12
|
if (!fs.existsSync(tsconfigPath)) {
|
|
16
13
|
return noExistReturn;
|
|
17
14
|
}
|
|
18
|
-
|
|
19
15
|
const content = fs.readFileSync(tsconfigPath, 'utf-8');
|
|
20
16
|
return json5.parse(content);
|
|
21
17
|
};
|
|
@@ -29,19 +25,22 @@ export const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
|
29
25
|
sourceType: 'unambiguous'
|
|
30
26
|
}, chain.toJSON());
|
|
31
27
|
};
|
|
32
|
-
export const resolveBabelConfig = (appDirectory, config, option
|
|
28
|
+
export const resolveBabelConfig = (appDirectory, config, option
|
|
29
|
+
// FIXME: babel type can't pass type checking
|
|
33
30
|
) => {
|
|
34
31
|
const {
|
|
35
32
|
envVars,
|
|
36
33
|
globalVars,
|
|
37
34
|
alias,
|
|
38
35
|
babelConfig
|
|
39
|
-
} = config;
|
|
36
|
+
} = config;
|
|
40
37
|
|
|
41
|
-
|
|
38
|
+
// alias config
|
|
39
|
+
const aliasConfig = getAliasConfig(alias, _objectSpread({
|
|
42
40
|
appDirectory
|
|
43
|
-
}, option));
|
|
41
|
+
}, option));
|
|
44
42
|
|
|
43
|
+
// babel config
|
|
45
44
|
const babelChain = getBabelChain({
|
|
46
45
|
appDirectory,
|
|
47
46
|
enableReactPreset: true,
|
|
@@ -60,14 +59,13 @@ export const resolveBabelConfig = (appDirectory, config, option // FIXME: babel
|
|
|
60
59
|
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
61
60
|
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
62
61
|
legacy: true
|
|
63
|
-
}]);
|
|
62
|
+
}]);
|
|
64
63
|
|
|
64
|
+
// resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
|
|
65
65
|
babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
|
|
66
66
|
loose: true
|
|
67
67
|
}]);
|
|
68
|
-
|
|
69
68
|
const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
|
|
70
|
-
|
|
71
69
|
return applyUserBabelConfig(internalBabelConfig, babelConfig);
|
|
72
70
|
};
|
|
73
71
|
export const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
@@ -82,7 +80,6 @@ export const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
|
82
80
|
syntax: 'es6+',
|
|
83
81
|
type: 'commonjs'
|
|
84
82
|
});
|
|
85
|
-
|
|
86
83
|
if (await fs.pathExists(sourceDir)) {
|
|
87
84
|
const basename = path.basename(sourceDir);
|
|
88
85
|
const targetDir = path.join(distDir, basename);
|
|
@@ -90,7 +87,6 @@ export const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
|
90
87
|
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
91
88
|
});
|
|
92
89
|
}
|
|
93
|
-
|
|
94
90
|
return compiler({
|
|
95
91
|
rootDir: appDirectory,
|
|
96
92
|
distDir,
|
|
@@ -1,15 +1,11 @@
|
|
|
1
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
2
|
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
3
|
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
4
|
import path from 'path';
|
|
8
|
-
import { logger,
|
|
5
|
+
import { logger, getAliasConfig, fs } from '@modern-js/utils';
|
|
9
6
|
import ts from 'typescript';
|
|
10
7
|
import { TypescriptLoader } from "./typescript-loader";
|
|
11
8
|
import { tsconfigPathsBeforeHookFactory } from "./tsconfig-paths-plugin";
|
|
12
|
-
|
|
13
9
|
const readTsConfigByFile = tsConfigFile => {
|
|
14
10
|
const parsedCmd = ts.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, ts.sys);
|
|
15
11
|
const {
|
|
@@ -23,7 +19,6 @@ const readTsConfigByFile = tsConfigFile => {
|
|
|
23
19
|
projectReferences
|
|
24
20
|
};
|
|
25
21
|
};
|
|
26
|
-
|
|
27
22
|
const copyFiles = async (from, to, tsconfigPath) => {
|
|
28
23
|
if (await fs.pathExists(from)) {
|
|
29
24
|
const basename = path.basename(from);
|
|
@@ -33,7 +28,6 @@ const copyFiles = async (from, to, tsconfigPath) => {
|
|
|
33
28
|
});
|
|
34
29
|
}
|
|
35
30
|
};
|
|
36
|
-
|
|
37
31
|
export const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
38
32
|
logger.info(`Running ts compile...`);
|
|
39
33
|
const {
|
|
@@ -41,11 +35,9 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
|
41
35
|
distDir,
|
|
42
36
|
tsconfigPath
|
|
43
37
|
} = compileOptions;
|
|
44
|
-
|
|
45
38
|
if (!tsconfigPath) {
|
|
46
39
|
return;
|
|
47
40
|
}
|
|
48
|
-
|
|
49
41
|
const ts = new TypescriptLoader({
|
|
50
42
|
appDirectory
|
|
51
43
|
}).load();
|
|
@@ -54,7 +46,7 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
|
54
46
|
const {
|
|
55
47
|
alias
|
|
56
48
|
} = config;
|
|
57
|
-
const aliasOption =
|
|
49
|
+
const aliasOption = getAliasConfig(alias, {
|
|
58
50
|
appDirectory,
|
|
59
51
|
tsconfigPath
|
|
60
52
|
});
|
|
@@ -86,20 +78,16 @@ export const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
|
86
78
|
before: [tsconfigPathsPlugin]
|
|
87
79
|
});
|
|
88
80
|
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
89
|
-
|
|
90
81
|
if (allDiagnostics.length > 0) {
|
|
91
|
-
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
92
|
-
|
|
82
|
+
logger.error(ts.formatDiagnosticsWithColorAndContext(allDiagnostics, formatHost));
|
|
83
|
+
// eslint-disable-next-line no-process-exit
|
|
93
84
|
process.exit(1);
|
|
94
85
|
}
|
|
95
|
-
|
|
96
86
|
for (const source of sourceDirs) {
|
|
97
87
|
await copyFiles(source, distDir, tsconfigPath);
|
|
98
88
|
}
|
|
99
|
-
|
|
100
89
|
logger.info(`Ts compile succeed`);
|
|
101
90
|
};
|
|
102
|
-
|
|
103
91
|
const getFormatHost = ts => {
|
|
104
92
|
return {
|
|
105
93
|
getCanonicalFileName: path => path,
|
|
@@ -2,20 +2,16 @@ import * as os from 'os';
|
|
|
2
2
|
import path, { dirname, posix } from 'path';
|
|
3
3
|
import * as ts from 'typescript';
|
|
4
4
|
import { createMatchPath } from 'tsconfig-paths';
|
|
5
|
-
|
|
6
5
|
const isRegExpKey = str => {
|
|
7
6
|
return str.startsWith('^') || str.endsWith('$');
|
|
8
7
|
};
|
|
9
|
-
|
|
10
8
|
const resolveAliasPath = (baseUrl, filePath) => {
|
|
11
9
|
// exclude absolute path and alias
|
|
12
10
|
if (filePath.startsWith('.') || filePath.startsWith('..')) {
|
|
13
11
|
return path.resolve(baseUrl, filePath);
|
|
14
12
|
}
|
|
15
|
-
|
|
16
13
|
return filePath;
|
|
17
14
|
};
|
|
18
|
-
|
|
19
15
|
const createAliasMatcher = (baseUrl, alias) => {
|
|
20
16
|
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
21
17
|
if (isRegExpKey(key)) {
|
|
@@ -26,16 +22,15 @@ const createAliasMatcher = (baseUrl, alias) => {
|
|
|
26
22
|
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
27
23
|
o.push([key, aliasPath]);
|
|
28
24
|
}
|
|
29
|
-
|
|
30
25
|
return o;
|
|
31
26
|
}, []);
|
|
32
|
-
const cacheMap = new Map();
|
|
27
|
+
const cacheMap = new Map();
|
|
33
28
|
|
|
29
|
+
// eslint-disable-next-line consistent-return
|
|
34
30
|
return requestedModule => {
|
|
35
31
|
if (cacheMap.has(requestedModule)) {
|
|
36
32
|
return cacheMap.get(requestedModule);
|
|
37
33
|
}
|
|
38
|
-
|
|
39
34
|
for (const [key, value] of aliasPairs) {
|
|
40
35
|
if (key instanceof RegExp) {
|
|
41
36
|
if (key.test(requestedModule)) {
|
|
@@ -43,7 +38,6 @@ const createAliasMatcher = (baseUrl, alias) => {
|
|
|
43
38
|
return value;
|
|
44
39
|
}
|
|
45
40
|
}
|
|
46
|
-
|
|
47
41
|
if (requestedModule === key) {
|
|
48
42
|
cacheMap.set(requestedModule, value);
|
|
49
43
|
return value;
|
|
@@ -51,11 +45,9 @@ const createAliasMatcher = (baseUrl, alias) => {
|
|
|
51
45
|
}
|
|
52
46
|
};
|
|
53
47
|
};
|
|
54
|
-
|
|
55
48
|
const isDynamicImport = (tsBinary, node) => {
|
|
56
49
|
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
57
50
|
};
|
|
58
|
-
|
|
59
51
|
export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
60
52
|
const tsPaths = {};
|
|
61
53
|
const alias = {};
|
|
@@ -68,21 +60,16 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
|
68
60
|
});
|
|
69
61
|
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
70
62
|
const matchTsPath = createMatchPath(baseUrl, tsPaths, ['main']);
|
|
71
|
-
|
|
72
63
|
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
73
64
|
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
74
|
-
|
|
75
65
|
if (result) {
|
|
76
66
|
return result;
|
|
77
67
|
}
|
|
78
|
-
|
|
79
68
|
return matchAliasPath(requestedModule);
|
|
80
69
|
};
|
|
81
|
-
|
|
82
70
|
if (Object.keys(paths).length === 0) {
|
|
83
71
|
return undefined;
|
|
84
72
|
}
|
|
85
|
-
|
|
86
73
|
return ctx => {
|
|
87
74
|
return sf => {
|
|
88
75
|
const visitNode = node => {
|
|
@@ -90,63 +77,53 @@ export function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
|
90
77
|
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
91
78
|
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
92
79
|
const result = getNotAliasedPath(sf, matchPath, text);
|
|
93
|
-
|
|
94
80
|
if (!result) {
|
|
95
81
|
return node;
|
|
96
82
|
}
|
|
97
|
-
|
|
98
83
|
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([tsBinary.factory.createStringLiteral(result)]));
|
|
99
84
|
}
|
|
100
|
-
|
|
101
85
|
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
102
86
|
try {
|
|
103
87
|
var _node$moduleSpecifier;
|
|
104
|
-
|
|
105
88
|
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
89
|
if (!importPathWithQuotes) {
|
|
108
90
|
return node;
|
|
109
91
|
}
|
|
110
|
-
|
|
111
92
|
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
112
93
|
const result = getNotAliasedPath(sf, matchPath, text);
|
|
113
|
-
|
|
114
94
|
if (!result) {
|
|
115
95
|
return node;
|
|
116
96
|
}
|
|
117
|
-
|
|
118
97
|
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
119
98
|
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
120
|
-
|
|
99
|
+
let newNode;
|
|
121
100
|
if (tsBinary.isImportDeclaration(node)) {
|
|
122
|
-
|
|
101
|
+
newNode = tsBinary.factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
123
102
|
} else {
|
|
124
|
-
|
|
103
|
+
newNode = tsBinary.factory.updateExportDeclaration(node, node.decorators, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
125
104
|
}
|
|
105
|
+
newNode.flags = node.flags;
|
|
106
|
+
return newNode;
|
|
126
107
|
} catch (_unused) {
|
|
127
108
|
return node;
|
|
128
109
|
}
|
|
129
110
|
}
|
|
130
|
-
|
|
131
111
|
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
132
112
|
};
|
|
133
|
-
|
|
134
113
|
return tsBinary.visitNode(sf, visitNode);
|
|
135
114
|
};
|
|
136
115
|
};
|
|
137
|
-
}
|
|
116
|
+
}
|
|
138
117
|
|
|
118
|
+
// fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
139
119
|
function getNotAliasedPath(sf, matcher, text) {
|
|
140
120
|
let result = matcher(text, undefined, undefined, ['.ts', '.tsx', '.js', '.jsx']);
|
|
141
|
-
|
|
142
121
|
if (!result) {
|
|
143
122
|
return;
|
|
144
123
|
}
|
|
145
|
-
|
|
146
124
|
if (os.platform() === 'win32') {
|
|
147
125
|
result = result.replace(/\\/g, '/');
|
|
148
126
|
}
|
|
149
|
-
|
|
150
127
|
if (!path.isAbsolute(result)) {
|
|
151
128
|
// handle alias to alias
|
|
152
129
|
if (!result.startsWith('.') && !result.startsWith('..')) {
|
|
@@ -156,29 +133,25 @@ function getNotAliasedPath(sf, matcher, text) {
|
|
|
156
133
|
const packagePath = require.resolve(result, {
|
|
157
134
|
paths: [process.cwd(), ...module.paths]
|
|
158
135
|
});
|
|
159
|
-
|
|
160
136
|
if (packagePath) {
|
|
161
137
|
// eslint-disable-next-line consistent-return
|
|
162
138
|
return result;
|
|
163
139
|
}
|
|
164
140
|
} catch (_unused2) {}
|
|
165
141
|
}
|
|
166
|
-
|
|
167
142
|
try {
|
|
168
143
|
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
169
144
|
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
170
145
|
const packagePath = require.resolve(text, {
|
|
171
146
|
paths: [process.cwd(), ...module.paths]
|
|
172
147
|
});
|
|
173
|
-
|
|
174
148
|
if (packagePath) {
|
|
175
149
|
// eslint-disable-next-line consistent-return
|
|
176
150
|
return text;
|
|
177
151
|
}
|
|
178
152
|
} catch (_unused3) {}
|
|
179
153
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
154
|
+
const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
|
|
155
|
+
// eslint-disable-next-line consistent-return
|
|
183
156
|
return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
|
|
184
157
|
}
|
|
@@ -1,32 +1,24 @@
|
|
|
1
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
2
|
export class TypescriptLoader {
|
|
4
3
|
constructor({
|
|
5
4
|
appDirectory
|
|
6
5
|
}) {
|
|
7
6
|
_defineProperty(this, "tsBinary", void 0);
|
|
8
|
-
|
|
9
7
|
_defineProperty(this, "appDirectory", void 0);
|
|
10
|
-
|
|
11
8
|
this.appDirectory = appDirectory;
|
|
12
9
|
}
|
|
13
|
-
|
|
14
10
|
load() {
|
|
15
11
|
if (this.tsBinary) {
|
|
16
12
|
return this.tsBinary;
|
|
17
13
|
}
|
|
18
|
-
|
|
19
14
|
try {
|
|
20
15
|
const tsPath = require.resolve('typescript', {
|
|
21
16
|
paths: [this.appDirectory || process.cwd()]
|
|
22
17
|
});
|
|
23
|
-
|
|
24
18
|
const ts = require(tsPath);
|
|
25
|
-
|
|
26
19
|
return ts;
|
|
27
20
|
} catch (error) {
|
|
28
21
|
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
29
22
|
}
|
|
30
23
|
}
|
|
31
|
-
|
|
32
24
|
}
|
|
@@ -4,35 +4,24 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.compile = exports.FILE_EXTENSIONS = void 0;
|
|
7
|
-
|
|
8
7
|
var path = _interopRequireWildcard(require("path"));
|
|
9
|
-
|
|
10
8
|
var _utils = require("@modern-js/utils");
|
|
11
|
-
|
|
12
9
|
var _typescript = require("../compilers/typescript");
|
|
13
|
-
|
|
14
10
|
var _babel = require("../compilers/babel");
|
|
15
|
-
|
|
16
11
|
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
12
|
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
13
|
const FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
21
14
|
exports.FILE_EXTENSIONS = FILE_EXTENSIONS;
|
|
22
|
-
|
|
23
15
|
const validateAbsolutePath = (filename, message) => {
|
|
24
16
|
if (!path.isAbsolute(filename)) {
|
|
25
17
|
throw new Error(message);
|
|
26
18
|
}
|
|
27
19
|
};
|
|
28
|
-
|
|
29
20
|
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
30
21
|
filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
|
|
31
22
|
};
|
|
32
|
-
|
|
33
23
|
const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
34
24
|
var _modernConfig$server;
|
|
35
|
-
|
|
36
25
|
const {
|
|
37
26
|
sourceDirs,
|
|
38
27
|
distDir,
|
|
@@ -42,12 +31,10 @@ const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
|
42
31
|
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
43
32
|
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig$server = modernConfig.server) === null || _modernConfig$server === void 0 ? void 0 : _modernConfig$server.compiler;
|
|
44
33
|
const isTsProject = tsconfigPath && (await _utils.fs.pathExists(tsconfigPath));
|
|
45
|
-
|
|
46
34
|
if (!isTsProject || compiler === 'babel') {
|
|
47
35
|
await (0, _babel.compileByBabel)(appDirectory, modernConfig, compileOptions);
|
|
48
36
|
} else {
|
|
49
37
|
await (0, _typescript.compileByTs)(appDirectory, modernConfig, compileOptions);
|
|
50
38
|
}
|
|
51
39
|
};
|
|
52
|
-
|
|
53
40
|
exports.compile = compile;
|
|
@@ -11,19 +11,12 @@ var _exportNames = {
|
|
|
11
11
|
compileByBabel: true
|
|
12
12
|
};
|
|
13
13
|
exports.resolveBabelConfig = exports.readTsConfig = exports.getBabelConfig = exports.existTsConfigFile = exports.compileByBabel = void 0;
|
|
14
|
-
|
|
15
14
|
var path = _interopRequireWildcard(require("path"));
|
|
16
|
-
|
|
17
15
|
var _babelPresetLib = require("@modern-js/babel-preset-lib");
|
|
18
|
-
|
|
19
16
|
var _utils = require("@modern-js/utils");
|
|
20
|
-
|
|
21
17
|
var _babelCompiler = require("@modern-js/babel-compiler");
|
|
22
|
-
|
|
23
18
|
var _common = require("../../common");
|
|
24
|
-
|
|
25
19
|
var _core = require("@babel/core");
|
|
26
|
-
|
|
27
20
|
Object.keys(_core).forEach(function (key) {
|
|
28
21
|
if (key === "default" || key === "__esModule") return;
|
|
29
22
|
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
@@ -35,59 +28,48 @@ Object.keys(_core).forEach(function (key) {
|
|
|
35
28
|
}
|
|
36
29
|
});
|
|
37
30
|
});
|
|
38
|
-
|
|
39
31
|
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
32
|
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
|
-
|
|
43
33
|
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; }
|
|
44
|
-
|
|
45
34
|
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; }
|
|
46
|
-
|
|
47
35
|
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; }
|
|
48
|
-
|
|
49
36
|
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
50
37
|
// 如果不存在,则返回 noExistReturn
|
|
51
38
|
if (!_utils.fs.existsSync(tsconfigPath)) {
|
|
52
39
|
return noExistReturn;
|
|
53
40
|
}
|
|
54
|
-
|
|
55
41
|
const content = _utils.fs.readFileSync(tsconfigPath, 'utf-8');
|
|
56
|
-
|
|
57
42
|
return _utils.json5.parse(content);
|
|
58
43
|
};
|
|
59
|
-
|
|
60
44
|
exports.readTsConfig = readTsConfig;
|
|
61
|
-
|
|
62
45
|
const existTsConfigFile = tsconfigAbsolutePath => {
|
|
63
46
|
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
64
47
|
return Boolean(tsconfig);
|
|
65
48
|
};
|
|
66
|
-
|
|
67
49
|
exports.existTsConfigFile = existTsConfigFile;
|
|
68
|
-
|
|
69
50
|
const getBabelConfig = (libPresetOption, syntaxOption) => {
|
|
70
51
|
const chain = (0, _babelPresetLib.getBabelChain)(libPresetOption, syntaxOption);
|
|
71
52
|
return _objectSpread({
|
|
72
53
|
sourceType: 'unambiguous'
|
|
73
54
|
}, chain.toJSON());
|
|
74
55
|
};
|
|
75
|
-
|
|
76
56
|
exports.getBabelConfig = getBabelConfig;
|
|
77
|
-
|
|
78
|
-
|
|
57
|
+
const resolveBabelConfig = (appDirectory, config, option
|
|
58
|
+
// FIXME: babel type can't pass type checking
|
|
79
59
|
) => {
|
|
80
60
|
const {
|
|
81
61
|
envVars,
|
|
82
62
|
globalVars,
|
|
83
63
|
alias,
|
|
84
64
|
babelConfig
|
|
85
|
-
} = config;
|
|
65
|
+
} = config;
|
|
86
66
|
|
|
87
|
-
|
|
67
|
+
// alias config
|
|
68
|
+
const aliasConfig = (0, _utils.getAliasConfig)(alias, _objectSpread({
|
|
88
69
|
appDirectory
|
|
89
|
-
}, option));
|
|
70
|
+
}, option));
|
|
90
71
|
|
|
72
|
+
// babel config
|
|
91
73
|
const babelChain = (0, _babelPresetLib.getBabelChain)({
|
|
92
74
|
appDirectory,
|
|
93
75
|
enableReactPreset: true,
|
|
@@ -106,19 +88,16 @@ const resolveBabelConfig = (appDirectory, config, option // FIXME: babel type ca
|
|
|
106
88
|
babelChain.plugin('babel-plugin-transform-typescript-metadata').use(require.resolve('babel-plugin-transform-typescript-metadata'), []);
|
|
107
89
|
babelChain.plugin('@babel/plugin-proposal-decorators').use(require.resolve('@babel/plugin-proposal-decorators'), [{
|
|
108
90
|
legacy: true
|
|
109
|
-
}]);
|
|
91
|
+
}]);
|
|
110
92
|
|
|
93
|
+
// resolve "Definitely assigned fields cannot be initialized here, but only in the constructor."
|
|
111
94
|
babelChain.plugin('@babel/plugin-proposal-class-properties').use(require.resolve('@babel/plugin-proposal-class-properties'), [{
|
|
112
95
|
loose: true
|
|
113
96
|
}]);
|
|
114
|
-
|
|
115
97
|
const internalBabelConfig = _objectSpread({}, babelChain.toJSON());
|
|
116
|
-
|
|
117
98
|
return (0, _babelPresetLib.applyUserBabelConfig)(internalBabelConfig, babelConfig);
|
|
118
99
|
};
|
|
119
|
-
|
|
120
100
|
exports.resolveBabelConfig = resolveBabelConfig;
|
|
121
|
-
|
|
122
101
|
const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
123
102
|
const {
|
|
124
103
|
sourceDirs,
|
|
@@ -131,7 +110,6 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
|
131
110
|
syntax: 'es6+',
|
|
132
111
|
type: 'commonjs'
|
|
133
112
|
});
|
|
134
|
-
|
|
135
113
|
if (await _utils.fs.pathExists(sourceDir)) {
|
|
136
114
|
const basename = path.basename(sourceDir);
|
|
137
115
|
const targetDir = path.join(distDir, basename);
|
|
@@ -139,7 +117,6 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
|
139
117
|
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath
|
|
140
118
|
});
|
|
141
119
|
}
|
|
142
|
-
|
|
143
120
|
return (0, _babelCompiler.compiler)({
|
|
144
121
|
rootDir: appDirectory,
|
|
145
122
|
distDir,
|
|
@@ -153,5 +130,4 @@ const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
|
153
130
|
}
|
|
154
131
|
});
|
|
155
132
|
};
|
|
156
|
-
|
|
157
133
|
exports.compileByBabel = compileByBabel;
|