@modern-js/server-utils 2.60.0 → 2.60.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/dist/cjs/compilers/typescript/index.js +1 -1
- package/dist/common/index.js +65 -0
- package/dist/compilers/babel/index.js +97 -0
- package/dist/compilers/babel/preset/alias.js +64 -0
- package/dist/compilers/babel/preset/index.js +46 -0
- package/dist/compilers/babel/preset/types.js +2 -0
- package/dist/compilers/typescript/index.js +91 -0
- package/dist/compilers/typescript/tsconfigPathsPlugin.js +198 -0
- package/dist/compilers/typescript/typescriptLoader.js +24 -0
- package/dist/esm/compilers/typescript/index.js +1 -1
- package/dist/esm-node/compilers/typescript/index.js +1 -1
- package/dist/index.js +21 -0
- package/dist/utils.js +41 -0
- package/package.json +4 -4
|
@@ -52,7 +52,7 @@ const copyFiles = async (from, to, appDirectory, tsconfigPath) => {
|
|
|
52
52
|
filter: (src) => ![
|
|
53
53
|
".ts",
|
|
54
54
|
".js"
|
|
55
|
-
].includes(import_path.default.extname(src)) && src
|
|
55
|
+
].includes(import_path.default.extname(src)) && src.endsWith("tsconfig.json")
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.compile = exports.FILE_EXTENSIONS = void 0;
|
|
36
|
+
const path = __importStar(require("path"));
|
|
37
|
+
const utils_1 = require("@modern-js/utils");
|
|
38
|
+
exports.FILE_EXTENSIONS = ['.js', '.ts', '.mjs', '.ejs'];
|
|
39
|
+
const validateAbsolutePath = (filename, message) => {
|
|
40
|
+
if (!path.isAbsolute(filename)) {
|
|
41
|
+
throw new Error(message);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
45
|
+
filenames.forEach(filename => validateAbsolutePath(filename, messageFunc(filename)));
|
|
46
|
+
};
|
|
47
|
+
const compile = (appDirectory, modernConfig, compileOptions) => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
+
var _a;
|
|
49
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
50
|
+
validateAbsolutePaths(sourceDirs, dir => `source dir ${dir} is not an absolute path.`);
|
|
51
|
+
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
52
|
+
const compiler = (_a = modernConfig === null || modernConfig === void 0 ? void 0 : modernConfig.server) === null || _a === void 0 ? void 0 : _a.compiler;
|
|
53
|
+
const isTsProject = tsconfigPath && (yield utils_1.fs.pathExists(tsconfigPath));
|
|
54
|
+
if (!isTsProject || compiler === 'babel') {
|
|
55
|
+
// eslint-disable-next-line node/file-extension-in-import
|
|
56
|
+
const { compileByBabel } = yield import('../compilers/babel/index.js');
|
|
57
|
+
yield compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// eslint-disable-next-line node/file-extension-in-import
|
|
61
|
+
const { compileByTs } = yield import('../compilers/typescript/index.js');
|
|
62
|
+
yield compileByTs(appDirectory, modernConfig, compileOptions);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
exports.compile = compile;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.compileByBabel = exports.resolveBabelConfig = exports.existTsConfigFile = exports.readTsConfig = void 0;
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const utils_1 = require("@modern-js/utils");
|
|
41
|
+
const babel_compiler_1 = require("@modern-js/babel-compiler");
|
|
42
|
+
const common_1 = require("../../common");
|
|
43
|
+
const preset_1 = require("./preset");
|
|
44
|
+
__exportStar(require("@babel/core"), exports);
|
|
45
|
+
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
46
|
+
// 如果不存在,则返回 noExistReturn
|
|
47
|
+
if (!utils_1.fs.existsSync(tsconfigPath)) {
|
|
48
|
+
return noExistReturn;
|
|
49
|
+
}
|
|
50
|
+
const content = utils_1.fs.readFileSync(tsconfigPath, 'utf-8');
|
|
51
|
+
return utils_1.json5.parse(content);
|
|
52
|
+
};
|
|
53
|
+
exports.readTsConfig = readTsConfig;
|
|
54
|
+
const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
55
|
+
const tsconfig = (0, exports.readTsConfig)(tsconfigAbsolutePath);
|
|
56
|
+
return Boolean(tsconfig);
|
|
57
|
+
};
|
|
58
|
+
exports.existTsConfigFile = existTsConfigFile;
|
|
59
|
+
const resolveBabelConfig = (appDirectory, config, option) => {
|
|
60
|
+
const { alias, babelConfig } = config;
|
|
61
|
+
// alias config
|
|
62
|
+
const aliasConfig = (0, utils_1.getAliasConfig)(alias, Object.assign({ appDirectory }, option));
|
|
63
|
+
// babel config
|
|
64
|
+
const defaultBabelConfig = (0, preset_1.getBabelConfig)({
|
|
65
|
+
appDirectory,
|
|
66
|
+
alias: aliasConfig,
|
|
67
|
+
});
|
|
68
|
+
return (0, preset_1.applyUserBabelConfig)(defaultBabelConfig, babelConfig);
|
|
69
|
+
};
|
|
70
|
+
exports.resolveBabelConfig = resolveBabelConfig;
|
|
71
|
+
const compileByBabel = (appDirectory, config, compileOptions) => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
73
|
+
const results = yield Promise.all(sourceDirs.map((sourceDir) => __awaiter(void 0, void 0, void 0, function* () {
|
|
74
|
+
const babelConfig = (0, exports.resolveBabelConfig)(appDirectory, config, {
|
|
75
|
+
tsconfigPath: tsconfigPath ? tsconfigPath : '',
|
|
76
|
+
});
|
|
77
|
+
if (yield utils_1.fs.pathExists(sourceDir)) {
|
|
78
|
+
const basename = path.basename(sourceDir);
|
|
79
|
+
const targetDir = path.join(distDir, basename);
|
|
80
|
+
yield utils_1.fs.copy(sourceDir, targetDir, {
|
|
81
|
+
filter: src => !['.ts', '.js'].includes(path.extname(src)) && src !== tsconfigPath,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
return (0, babel_compiler_1.compiler)({
|
|
85
|
+
rootDir: appDirectory,
|
|
86
|
+
distDir,
|
|
87
|
+
sourceDir,
|
|
88
|
+
extensions: common_1.FILE_EXTENSIONS,
|
|
89
|
+
}, babelConfig);
|
|
90
|
+
})));
|
|
91
|
+
results.forEach(result => {
|
|
92
|
+
if (result.code === 1) {
|
|
93
|
+
throw new Error(result.message);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
exports.compileByBabel = compileByBabel;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.aliasPlugin = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const tsconfig_paths_1 = require("@modern-js/utils/tsconfig-paths");
|
|
9
|
+
const utils_1 = require("@modern-js/utils");
|
|
10
|
+
const { resolvePath } = require('@modern-js/babel-plugin-module-resolver');
|
|
11
|
+
const defaultPaths = { '@': ['./src'] };
|
|
12
|
+
const aliasPlugin = (alias) => {
|
|
13
|
+
const { absoluteBaseUrl, isTsPath, isTsProject = false } = alias;
|
|
14
|
+
const mergedPaths = isTsPath
|
|
15
|
+
? alias.paths || {}
|
|
16
|
+
: Object.assign(Object.assign({}, defaultPaths), (alias.paths || {}));
|
|
17
|
+
let tsPaths = {};
|
|
18
|
+
if (isTsProject) {
|
|
19
|
+
tsPaths = (0, utils_1.getUserAlias)(mergedPaths);
|
|
20
|
+
}
|
|
21
|
+
tsPaths = Object.keys(tsPaths).reduce((o, key) => {
|
|
22
|
+
if (typeof tsPaths[key] === 'string') {
|
|
23
|
+
return Object.assign(Object.assign({}, o), { [`${key}`]: [tsPaths[key]] });
|
|
24
|
+
}
|
|
25
|
+
return Object.assign(Object.assign({}, o), { [`${key}`]: tsPaths[key] });
|
|
26
|
+
}, {});
|
|
27
|
+
const resolvePathFn = (sourcePath, currentFile, opts) => {
|
|
28
|
+
// fix by: https://github.com/tleunen/babel-plugin-module-resolver/pull/409/files
|
|
29
|
+
if (sourcePath === '.' || sourcePath === './') {
|
|
30
|
+
return sourcePath;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
*以下是匹配到tsconfig的paths的情况进行进一步匹配和转换
|
|
34
|
+
*/
|
|
35
|
+
const matchPath = (0, tsconfig_paths_1.createMatchPath)(absoluteBaseUrl, tsPaths, ['index']);
|
|
36
|
+
const result = matchPath(sourcePath, undefined, undefined, [
|
|
37
|
+
'.js',
|
|
38
|
+
'.jsx',
|
|
39
|
+
'.ts',
|
|
40
|
+
'.tsx',
|
|
41
|
+
]);
|
|
42
|
+
if (result) {
|
|
43
|
+
const relativePath = path_1.default.relative(path_1.default.dirname(currentFile), path_1.default.dirname(result));
|
|
44
|
+
const fileName = path_1.default.basename(result);
|
|
45
|
+
// 如果是同级文件,则返回的是 ''
|
|
46
|
+
const filePath = path_1.default
|
|
47
|
+
.normalize(`${relativePath.length === 0 ? '.' : relativePath}/${fileName}`)
|
|
48
|
+
.replace(/\\/, '/');
|
|
49
|
+
return filePath.startsWith('.') ? filePath : `./${filePath}`;
|
|
50
|
+
}
|
|
51
|
+
return resolvePath(sourcePath, currentFile, opts);
|
|
52
|
+
};
|
|
53
|
+
const typescriptExts = ['.ts', '.tsx', '.js', '.jsx', '.es', '.es6', '.mjs'];
|
|
54
|
+
return [
|
|
55
|
+
require.resolve('@modern-js/babel-plugin-module-resolver'),
|
|
56
|
+
{
|
|
57
|
+
root: absoluteBaseUrl,
|
|
58
|
+
alias: mergedPaths,
|
|
59
|
+
resolvePath: isTsPath ? resolvePathFn : undefined,
|
|
60
|
+
extensions: isTsProject ? typescriptExts : undefined,
|
|
61
|
+
},
|
|
62
|
+
];
|
|
63
|
+
};
|
|
64
|
+
exports.aliasPlugin = aliasPlugin;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.applyUserBabelConfig = exports.getBabelConfig = void 0;
|
|
18
|
+
const node_1 = require("@rsbuild/babel-preset/node");
|
|
19
|
+
const alias_1 = require("./alias");
|
|
20
|
+
const getBabelConfig = (libPresetOption) => {
|
|
21
|
+
var _a, _b, _c;
|
|
22
|
+
const config = (0, node_1.getBabelConfigForNode)({
|
|
23
|
+
presetEnv: {
|
|
24
|
+
loose: true,
|
|
25
|
+
modules: 'commonjs',
|
|
26
|
+
},
|
|
27
|
+
pluginDecorators: {
|
|
28
|
+
version: 'legacy',
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
(_a = config.presets) === null || _a === void 0 ? void 0 : _a.push([
|
|
32
|
+
require.resolve('@babel/preset-react'),
|
|
33
|
+
{
|
|
34
|
+
runtime: 'automatic',
|
|
35
|
+
},
|
|
36
|
+
]);
|
|
37
|
+
if (libPresetOption.alias) {
|
|
38
|
+
(_b = config.plugins) === null || _b === void 0 ? void 0 : _b.push((0, alias_1.aliasPlugin)(libPresetOption.alias));
|
|
39
|
+
}
|
|
40
|
+
(_c = config.plugins) === null || _c === void 0 ? void 0 : _c.push(require.resolve('babel-plugin-transform-typescript-metadata'));
|
|
41
|
+
return config;
|
|
42
|
+
};
|
|
43
|
+
exports.getBabelConfig = getBabelConfig;
|
|
44
|
+
__exportStar(require("./types"), exports);
|
|
45
|
+
var utils_1 = require("@modern-js/utils");
|
|
46
|
+
Object.defineProperty(exports, "applyUserBabelConfig", { enumerable: true, get: function () { return utils_1.applyUserBabelConfig; } });
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.compileByTs = void 0;
|
|
16
|
+
const path_1 = __importDefault(require("path"));
|
|
17
|
+
const utils_1 = require("@modern-js/utils");
|
|
18
|
+
const typescriptLoader_1 = require("./typescriptLoader");
|
|
19
|
+
const tsconfigPathsPlugin_1 = require("./tsconfigPathsPlugin");
|
|
20
|
+
const readTsConfigByFile = (tsConfigFile, tsInstance) => {
|
|
21
|
+
const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, undefined, tsInstance.sys);
|
|
22
|
+
const { options, fileNames, projectReferences } = parsedCmd;
|
|
23
|
+
return { options, fileNames, projectReferences };
|
|
24
|
+
};
|
|
25
|
+
const copyFiles = (from, to, appDirectory, tsconfigPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
if (yield utils_1.fs.pathExists(from)) {
|
|
27
|
+
const relativePath = path_1.default.relative(appDirectory, from);
|
|
28
|
+
const targetDir = path_1.default.join(to, relativePath);
|
|
29
|
+
yield utils_1.fs.copy(from, targetDir, {
|
|
30
|
+
filter: src => !['.ts'].includes(path_1.default.extname(src)) && src !== tsconfigPath,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const compileByTs = (appDirectory, config, compileOptions) => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
+
utils_1.logger.info(`Running ts compile...`);
|
|
36
|
+
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
37
|
+
if (!tsconfigPath) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const ts = new typescriptLoader_1.TypescriptLoader({
|
|
41
|
+
appDirectory,
|
|
42
|
+
}).load();
|
|
43
|
+
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
44
|
+
const formatHost = getFormatHost(ts);
|
|
45
|
+
const { alias } = config;
|
|
46
|
+
const aliasOption = (0, utils_1.getAliasConfig)(alias, {
|
|
47
|
+
appDirectory,
|
|
48
|
+
tsconfigPath,
|
|
49
|
+
});
|
|
50
|
+
const { paths = {}, absoluteBaseUrl = './' } = aliasOption;
|
|
51
|
+
const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath, ts);
|
|
52
|
+
const sourcePosixPaths = sourceDirs.map(sourceDir => sourceDir.split(path_1.default.sep).join(path_1.default.posix.sep));
|
|
53
|
+
const rootNames = fileNames.filter(fileName => {
|
|
54
|
+
return (fileName.endsWith('.d.ts') ||
|
|
55
|
+
sourcePosixPaths.some(sourceDir => {
|
|
56
|
+
return fileName.includes(sourceDir);
|
|
57
|
+
}));
|
|
58
|
+
});
|
|
59
|
+
const program = createProgram.call(ts, {
|
|
60
|
+
rootNames,
|
|
61
|
+
projectReferences,
|
|
62
|
+
options: Object.assign({ rootDir: appDirectory, outDir: distDir }, options),
|
|
63
|
+
});
|
|
64
|
+
const tsconfigPathsPlugin = (0, tsconfigPathsPlugin_1.tsconfigPathsBeforeHookFactory)(ts, absoluteBaseUrl, paths);
|
|
65
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
66
|
+
before: [tsconfigPathsPlugin],
|
|
67
|
+
});
|
|
68
|
+
const allDiagnostics = ts
|
|
69
|
+
.getPreEmitDiagnostics(program)
|
|
70
|
+
.concat(emitResult.diagnostics);
|
|
71
|
+
const { noEmitOnError } = options;
|
|
72
|
+
if (allDiagnostics.length > 0) {
|
|
73
|
+
utils_1.logger.error(ts.formatDiagnosticsWithColorAndContext([...new Set(allDiagnostics)], formatHost));
|
|
74
|
+
if (typeof noEmitOnError === 'undefined' || noEmitOnError === true) {
|
|
75
|
+
// eslint-disable-next-line no-process-exit
|
|
76
|
+
process.exit(1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const source of sourceDirs) {
|
|
80
|
+
yield copyFiles(source, distDir, appDirectory, tsconfigPath);
|
|
81
|
+
}
|
|
82
|
+
utils_1.logger.info(`Ts compile succeed`);
|
|
83
|
+
});
|
|
84
|
+
exports.compileByTs = compileByTs;
|
|
85
|
+
const getFormatHost = (ts) => {
|
|
86
|
+
return {
|
|
87
|
+
getCanonicalFileName: (path) => path,
|
|
88
|
+
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
89
|
+
getNewLine: () => ts.sys.newLine,
|
|
90
|
+
};
|
|
91
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.tsconfigPathsBeforeHookFactory = void 0;
|
|
27
|
+
const os = __importStar(require("os"));
|
|
28
|
+
const path_1 = __importStar(require("path"));
|
|
29
|
+
const ts = __importStar(require("typescript"));
|
|
30
|
+
const tsconfig_paths_1 = require("@modern-js/utils/tsconfig-paths");
|
|
31
|
+
const isRegExpKey = (str) => {
|
|
32
|
+
return str.startsWith('^') || str.endsWith('$');
|
|
33
|
+
};
|
|
34
|
+
const resolveAliasPath = (baseUrl, filePath) => {
|
|
35
|
+
// exclude absolute path and alias
|
|
36
|
+
if (filePath.startsWith('.') || filePath.startsWith('..')) {
|
|
37
|
+
return path_1.default.resolve(baseUrl, filePath);
|
|
38
|
+
}
|
|
39
|
+
return filePath;
|
|
40
|
+
};
|
|
41
|
+
const createAliasMatcher = (baseUrl, alias) => {
|
|
42
|
+
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
43
|
+
if (isRegExpKey(key)) {
|
|
44
|
+
const regexp = new RegExp(key);
|
|
45
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
46
|
+
o.push([regexp, aliasPath]);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
50
|
+
o.push([key, aliasPath]);
|
|
51
|
+
}
|
|
52
|
+
return o;
|
|
53
|
+
}, []);
|
|
54
|
+
const cacheMap = new Map();
|
|
55
|
+
// eslint-disable-next-line consistent-return
|
|
56
|
+
return (requestedModule) => {
|
|
57
|
+
if (cacheMap.has(requestedModule)) {
|
|
58
|
+
return cacheMap.get(requestedModule);
|
|
59
|
+
}
|
|
60
|
+
for (const [key, value] of aliasPairs) {
|
|
61
|
+
if (key instanceof RegExp) {
|
|
62
|
+
if (key.test(requestedModule)) {
|
|
63
|
+
cacheMap.set(requestedModule, value);
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (requestedModule === key) {
|
|
68
|
+
cacheMap.set(requestedModule, value);
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const isDynamicImport = (tsBinary, node) => {
|
|
75
|
+
return (tsBinary.isCallExpression(node) &&
|
|
76
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword);
|
|
77
|
+
};
|
|
78
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
79
|
+
const tsPaths = {};
|
|
80
|
+
const alias = {};
|
|
81
|
+
Object.keys(paths).forEach(key => {
|
|
82
|
+
if (Array.isArray(paths[key])) {
|
|
83
|
+
tsPaths[key] = paths[key];
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
alias[key] = paths[key];
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
90
|
+
const matchTsPath = (0, tsconfig_paths_1.createMatchPath)(baseUrl, tsPaths, ['main']);
|
|
91
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
92
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
93
|
+
if (result) {
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
return matchAliasPath(requestedModule);
|
|
97
|
+
};
|
|
98
|
+
if (Object.keys(paths).length === 0) {
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
return (ctx) => {
|
|
102
|
+
return (sf) => {
|
|
103
|
+
const visitNode = (node) => {
|
|
104
|
+
var _a;
|
|
105
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
106
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
107
|
+
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
108
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
109
|
+
if (!result) {
|
|
110
|
+
return node;
|
|
111
|
+
}
|
|
112
|
+
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
|
|
113
|
+
tsBinary.factory.createStringLiteral(result),
|
|
114
|
+
]));
|
|
115
|
+
}
|
|
116
|
+
if (tsBinary.isImportDeclaration(node) ||
|
|
117
|
+
(tsBinary.isExportDeclaration(node) && node.moduleSpecifier)) {
|
|
118
|
+
try {
|
|
119
|
+
const importPathWithQuotes = (_a = node === null || node === void 0 ? void 0 : node.moduleSpecifier) === null || _a === void 0 ? void 0 : _a.getText();
|
|
120
|
+
if (!importPathWithQuotes) {
|
|
121
|
+
return node;
|
|
122
|
+
}
|
|
123
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
124
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
125
|
+
if (!result) {
|
|
126
|
+
return node;
|
|
127
|
+
}
|
|
128
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
129
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
130
|
+
let newNode;
|
|
131
|
+
if (tsBinary.isImportDeclaration(node)) {
|
|
132
|
+
newNode = tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
newNode = tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
136
|
+
}
|
|
137
|
+
newNode.flags = node.flags;
|
|
138
|
+
return newNode;
|
|
139
|
+
}
|
|
140
|
+
catch (_b) {
|
|
141
|
+
return node;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
145
|
+
};
|
|
146
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
exports.tsconfigPathsBeforeHookFactory = tsconfigPathsBeforeHookFactory;
|
|
151
|
+
// fork from https://github.com/nestjs/nest-cli/blob/HEAD/lib/compiler/hooks/tsconfig-paths.hook.ts
|
|
152
|
+
// license at https://github.com/nestjs/nest/blob/master/LICENSE
|
|
153
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
154
|
+
let result = matcher(text, undefined, undefined, [
|
|
155
|
+
'.ts',
|
|
156
|
+
'.tsx',
|
|
157
|
+
'.js',
|
|
158
|
+
'.jsx',
|
|
159
|
+
]);
|
|
160
|
+
if (!result) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (os.platform() === 'win32') {
|
|
164
|
+
result = result.replace(/\\/g, '/');
|
|
165
|
+
}
|
|
166
|
+
if (!path_1.default.isAbsolute(result)) {
|
|
167
|
+
// handle alias to alias
|
|
168
|
+
if (!result.startsWith('.') && !result.startsWith('..')) {
|
|
169
|
+
try {
|
|
170
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
171
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
172
|
+
const packagePath = require.resolve(result, {
|
|
173
|
+
paths: [process.cwd(), ...module.paths],
|
|
174
|
+
});
|
|
175
|
+
if (packagePath) {
|
|
176
|
+
// eslint-disable-next-line consistent-return
|
|
177
|
+
return result;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch (_a) { }
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
// Installed packages (node modules) should take precedence over root files with the same name.
|
|
184
|
+
// Ref: https://github.com/nestjs/nest-cli/issues/838
|
|
185
|
+
const packagePath = require.resolve(text, {
|
|
186
|
+
paths: [process.cwd(), ...module.paths],
|
|
187
|
+
});
|
|
188
|
+
if (packagePath) {
|
|
189
|
+
// eslint-disable-next-line consistent-return
|
|
190
|
+
return text;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
catch (_b) { }
|
|
194
|
+
}
|
|
195
|
+
const resolvedPath = path_1.posix.relative((0, path_1.dirname)(sf.fileName), result) || './';
|
|
196
|
+
// eslint-disable-next-line consistent-return
|
|
197
|
+
return resolvedPath[0] === '.' ? resolvedPath : `./${resolvedPath}`;
|
|
198
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypescriptLoader = void 0;
|
|
4
|
+
class TypescriptLoader {
|
|
5
|
+
constructor({ appDirectory }) {
|
|
6
|
+
this.appDirectory = appDirectory;
|
|
7
|
+
}
|
|
8
|
+
load() {
|
|
9
|
+
if (this.tsBinary) {
|
|
10
|
+
return this.tsBinary;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
const tsPath = require.resolve('typescript', {
|
|
14
|
+
paths: [this.appDirectory || process.cwd()],
|
|
15
|
+
});
|
|
16
|
+
const ts = require(tsPath);
|
|
17
|
+
return ts;
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.TypescriptLoader = TypescriptLoader;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.compile = void 0;
|
|
18
|
+
__exportStar(require("./compilers/babel"), exports);
|
|
19
|
+
var common_1 = require("./common");
|
|
20
|
+
Object.defineProperty(exports, "compile", { enumerable: true, get: function () { return common_1.compile; } });
|
|
21
|
+
__exportStar(require("./utils"), exports);
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.requireExistModuleAsync = exports.compatRequireAsync = void 0;
|
|
13
|
+
const utils_1 = require("@modern-js/utils");
|
|
14
|
+
const compatRequireAsync = (filePath, interop = true) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
if (filePath.endsWith('.json')) {
|
|
16
|
+
return require(filePath);
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
const mod = require(filePath);
|
|
20
|
+
const rtnESMDefault = interop && (mod === null || mod === void 0 ? void 0 : mod.__esModule);
|
|
21
|
+
return rtnESMDefault ? mod.default : mod;
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error.code === 'ERR_REQUIRE_ESM') {
|
|
25
|
+
return yield import(filePath);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
exports.compatRequireAsync = compatRequireAsync;
|
|
33
|
+
const requireExistModuleAsync = (filename, opt) => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const final = Object.assign({ extensions: ['.ts', '.js'], interop: true }, opt);
|
|
35
|
+
const exist = (0, utils_1.findExists)(final.extensions.map(ext => `${filename}${ext}`));
|
|
36
|
+
if (!exist) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return yield (0, exports.compatRequireAsync)(exist, final.interop);
|
|
40
|
+
});
|
|
41
|
+
exports.requireExistModuleAsync = requireExistModuleAsync;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "2.60.0",
|
|
18
|
+
"version": "2.60.1-alpha.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"@swc/helpers": "0.5.13",
|
|
40
40
|
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
|
41
41
|
"@modern-js/babel-compiler": "2.60.0",
|
|
42
|
+
"@modern-js/utils": "2.60.0",
|
|
42
43
|
"@modern-js/babel-preset": "2.60.0",
|
|
43
|
-
"@modern-js/babel-plugin-module-resolver": "2.60.0"
|
|
44
|
-
"@modern-js/utils": "2.60.0"
|
|
44
|
+
"@modern-js/babel-plugin-module-resolver": "2.60.0"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/babel__core": "^7.20.5",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
"jest": "^29",
|
|
51
51
|
"ts-jest": "^29.1.0",
|
|
52
52
|
"typescript": "^5",
|
|
53
|
-
"@modern-js/server-core": "2.60.0",
|
|
54
53
|
"@scripts/build": "2.60.0",
|
|
54
|
+
"@modern-js/server-core": "2.60.0",
|
|
55
55
|
"@scripts/jest-config": "2.60.0"
|
|
56
56
|
},
|
|
57
57
|
"sideEffects": false,
|