@modern-js/server-utils 2.69.4 → 3.0.0-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/common/index.js +58 -62
- package/dist/cjs/compilers/babel/index.js +145 -101
- package/dist/cjs/compilers/babel/preset/alias.js +123 -105
- package/dist/cjs/compilers/babel/preset/index.js +102 -68
- package/dist/cjs/compilers/babel/preset/types.js +17 -15
- package/dist/cjs/compilers/typescript/index.js +112 -119
- package/dist/cjs/compilers/typescript/tsconfigPathsPlugin.js +157 -191
- package/dist/cjs/compilers/typescript/typescriptLoader.js +47 -41
- package/dist/cjs/index.js +68 -28
- package/dist/esm/common/index.mjs +29 -0
- package/dist/esm/compilers/babel/index.mjs +57 -0
- package/dist/esm/compilers/babel/preset/alias.mjs +84 -0
- package/dist/esm/compilers/babel/preset/index.mjs +29 -0
- package/dist/esm/compilers/typescript/index.mjs +76 -0
- package/dist/esm/compilers/typescript/tsconfigPathsPlugin.mjs +126 -0
- package/dist/esm/compilers/typescript/typescriptLoader.mjs +20 -0
- package/dist/{esm-node/index.js → esm/index.mjs} +2 -4
- package/dist/esm-node/common/index.mjs +29 -0
- package/dist/esm-node/compilers/babel/index.mjs +57 -0
- package/dist/esm-node/compilers/babel/preset/alias.mjs +84 -0
- package/dist/esm-node/compilers/babel/preset/index.mjs +29 -0
- package/dist/esm-node/compilers/typescript/index.mjs +76 -0
- package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.mjs +126 -0
- package/dist/esm-node/compilers/typescript/typescriptLoader.mjs +20 -0
- package/dist/esm-node/index.mjs +3 -0
- package/package.json +33 -25
- package/rslib.config.mts +4 -0
- package/dist/esm/common/index.js +0 -97
- package/dist/esm/compilers/babel/index.js +0 -119
- package/dist/esm/compilers/babel/preset/alias.js +0 -70
- package/dist/esm/compilers/babel/preset/index.js +0 -36
- package/dist/esm/compilers/typescript/index.js +0 -202
- package/dist/esm/compilers/typescript/tsconfigPathsPlugin.js +0 -188
- package/dist/esm/compilers/typescript/typescriptLoader.js +0 -30
- package/dist/esm/index.js +0 -5
- package/dist/esm-node/common/index.js +0 -35
- package/dist/esm-node/compilers/babel/index.js +0 -66
- package/dist/esm-node/compilers/babel/preset/alias.js +0 -76
- package/dist/esm-node/compilers/babel/preset/index.js +0 -36
- package/dist/esm-node/compilers/typescript/index.js +0 -93
- package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.js +0 -170
- package/dist/esm-node/compilers/typescript/typescriptLoader.js +0 -24
- /package/dist/esm/compilers/babel/preset/{types.js → types.mjs} +0 -0
- /package/dist/esm-node/compilers/babel/preset/{types.js → types.mjs} +0 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { platform } from "os";
|
|
2
|
+
import path, { dirname, posix } from "path";
|
|
3
|
+
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
4
|
+
import { SyntaxKind } from "typescript";
|
|
5
|
+
const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
|
|
6
|
+
const resolveAliasPath = (baseUrl, filePath)=>{
|
|
7
|
+
if (filePath.startsWith('.') || filePath.startsWith('..')) return path.resolve(baseUrl, filePath);
|
|
8
|
+
return filePath;
|
|
9
|
+
};
|
|
10
|
+
const createAliasMatcher = (baseUrl, alias)=>{
|
|
11
|
+
const aliasPairs = Object.keys(alias).reduce((o, key)=>{
|
|
12
|
+
if (isRegExpKey(key)) {
|
|
13
|
+
const regexp = new RegExp(key);
|
|
14
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
15
|
+
o.push([
|
|
16
|
+
regexp,
|
|
17
|
+
aliasPath
|
|
18
|
+
]);
|
|
19
|
+
} else {
|
|
20
|
+
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
21
|
+
o.push([
|
|
22
|
+
key,
|
|
23
|
+
aliasPath
|
|
24
|
+
]);
|
|
25
|
+
}
|
|
26
|
+
return o;
|
|
27
|
+
}, []);
|
|
28
|
+
const cacheMap = new Map();
|
|
29
|
+
return (requestedModule)=>{
|
|
30
|
+
if (cacheMap.has(requestedModule)) return cacheMap.get(requestedModule);
|
|
31
|
+
for (const [key, value] of aliasPairs){
|
|
32
|
+
if (key instanceof RegExp) {
|
|
33
|
+
if (key.test(requestedModule)) {
|
|
34
|
+
cacheMap.set(requestedModule, value);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (requestedModule === key) {
|
|
39
|
+
cacheMap.set(requestedModule, value);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
const isDynamicImport = (tsBinary, node)=>tsBinary.isCallExpression(node) && node.expression.kind === SyntaxKind.ImportKeyword;
|
|
46
|
+
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
47
|
+
const tsPaths = {};
|
|
48
|
+
const alias = {};
|
|
49
|
+
Object.keys(paths).forEach((key)=>{
|
|
50
|
+
if (Array.isArray(paths[key])) tsPaths[key] = paths[key];
|
|
51
|
+
else alias[key] = paths[key];
|
|
52
|
+
});
|
|
53
|
+
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
54
|
+
const matchTsPath = createMatchPath(baseUrl, tsPaths, [
|
|
55
|
+
'main'
|
|
56
|
+
]);
|
|
57
|
+
const matchPath = (requestedModule, readJSONSync, fileExists, extensions)=>{
|
|
58
|
+
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
59
|
+
if (result) return result;
|
|
60
|
+
return matchAliasPath(requestedModule);
|
|
61
|
+
};
|
|
62
|
+
if (0 === Object.keys(paths).length) return;
|
|
63
|
+
return (ctx)=>(sf)=>{
|
|
64
|
+
const visitNode = (node)=>{
|
|
65
|
+
if (isDynamicImport(tsBinary, node)) {
|
|
66
|
+
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
67
|
+
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
68
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
69
|
+
if (!result) return node;
|
|
70
|
+
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
|
|
71
|
+
tsBinary.factory.createStringLiteral(result)
|
|
72
|
+
]));
|
|
73
|
+
}
|
|
74
|
+
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) try {
|
|
75
|
+
const importPathWithQuotes = node?.moduleSpecifier?.getText();
|
|
76
|
+
if (!importPathWithQuotes) return node;
|
|
77
|
+
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
78
|
+
const result = getNotAliasedPath(sf, matchPath, text);
|
|
79
|
+
if (!result) return node;
|
|
80
|
+
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
81
|
+
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
82
|
+
let newNode;
|
|
83
|
+
newNode = tsBinary.isImportDeclaration(node) ? tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause) : tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
84
|
+
newNode.flags = node.flags;
|
|
85
|
+
return newNode;
|
|
86
|
+
} catch {
|
|
87
|
+
return node;
|
|
88
|
+
}
|
|
89
|
+
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
90
|
+
};
|
|
91
|
+
return tsBinary.visitNode(sf, visitNode);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function getNotAliasedPath(sf, matcher, text) {
|
|
95
|
+
let result = matcher(text, void 0, void 0, [
|
|
96
|
+
'.ts',
|
|
97
|
+
'.tsx',
|
|
98
|
+
'.js',
|
|
99
|
+
'.jsx'
|
|
100
|
+
]);
|
|
101
|
+
if (!result) return;
|
|
102
|
+
if ('win32' === platform()) result = result.replace(/\\/g, '/');
|
|
103
|
+
if (!path.isAbsolute(result)) {
|
|
104
|
+
if (!result.startsWith('.') && !result.startsWith('..')) try {
|
|
105
|
+
const packagePath = require.resolve(result, {
|
|
106
|
+
paths: [
|
|
107
|
+
process.cwd(),
|
|
108
|
+
...module.paths
|
|
109
|
+
]
|
|
110
|
+
});
|
|
111
|
+
if (packagePath) return result;
|
|
112
|
+
} catch {}
|
|
113
|
+
try {
|
|
114
|
+
const packagePath = require.resolve(text, {
|
|
115
|
+
paths: [
|
|
116
|
+
process.cwd(),
|
|
117
|
+
...module.paths
|
|
118
|
+
]
|
|
119
|
+
});
|
|
120
|
+
if (packagePath) return text;
|
|
121
|
+
} catch {}
|
|
122
|
+
}
|
|
123
|
+
const resolvedPath = posix.relative(dirname(sf.fileName), result) || './';
|
|
124
|
+
return '.' === resolvedPath[0] ? resolvedPath : `./${resolvedPath}`;
|
|
125
|
+
}
|
|
126
|
+
export { tsconfigPathsBeforeHookFactory };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class TypescriptLoader {
|
|
2
|
+
load() {
|
|
3
|
+
if (this.tsBinary) return this.tsBinary;
|
|
4
|
+
try {
|
|
5
|
+
const tsPath = require.resolve("typescript", {
|
|
6
|
+
paths: [
|
|
7
|
+
this.appDirectory || process.cwd()
|
|
8
|
+
]
|
|
9
|
+
});
|
|
10
|
+
const ts = require(tsPath);
|
|
11
|
+
return ts;
|
|
12
|
+
} catch (error) {
|
|
13
|
+
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
constructor({ appDirectory }){
|
|
17
|
+
this.appDirectory = appDirectory;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export { TypescriptLoader };
|
package/package.json
CHANGED
|
@@ -15,44 +15,53 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "
|
|
18
|
+
"version": "3.0.0-alpha.0",
|
|
19
19
|
"jsnext:source": "./src/index.ts",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"main": "./dist/cjs/index.js",
|
|
22
|
-
"module": "./dist/esm/index.
|
|
22
|
+
"module": "./dist/esm/index.mjs",
|
|
23
23
|
"exports": {
|
|
24
24
|
".": {
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"jsnext:source": "./src/index.ts",
|
|
25
27
|
"node": {
|
|
26
|
-
"
|
|
27
|
-
"import": "./dist/esm-node/index.js",
|
|
28
|
+
"import": "./dist/esm-node/index.mjs",
|
|
28
29
|
"require": "./dist/cjs/index.js"
|
|
29
30
|
},
|
|
30
|
-
"default": "./dist/esm/index.
|
|
31
|
+
"default": "./dist/esm/index.mjs"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"typesVersions": {
|
|
35
|
+
"*": {
|
|
36
|
+
".": [
|
|
37
|
+
"./dist/types/index.d.ts"
|
|
38
|
+
]
|
|
31
39
|
}
|
|
32
40
|
},
|
|
33
41
|
"dependencies": {
|
|
34
|
-
"@babel/core": "^7.
|
|
35
|
-
"@babel/plugin-proposal-decorators": "^7.
|
|
36
|
-
"@babel/preset-env": "^7.
|
|
37
|
-
"@babel/preset-react": "^7.
|
|
38
|
-
"@babel/preset-typescript": "^7.
|
|
42
|
+
"@babel/core": "^7.28.5",
|
|
43
|
+
"@babel/plugin-proposal-decorators": "^7.28.0",
|
|
44
|
+
"@babel/preset-env": "^7.28.5",
|
|
45
|
+
"@babel/preset-react": "^7.28.5",
|
|
46
|
+
"@babel/preset-typescript": "^7.28.5",
|
|
39
47
|
"@swc/helpers": "^0.5.17",
|
|
40
|
-
"babel-plugin-transform-typescript-metadata": "^0.
|
|
41
|
-
"@modern-js/babel-compiler": "
|
|
42
|
-
"@modern-js/babel-plugin-module-resolver": "
|
|
43
|
-
"@modern-js/babel-preset": "
|
|
44
|
-
"@modern-js/utils": "
|
|
48
|
+
"babel-plugin-transform-typescript-metadata": "^0.4.0",
|
|
49
|
+
"@modern-js/babel-compiler": "3.0.0-alpha.0",
|
|
50
|
+
"@modern-js/babel-plugin-module-resolver": "3.0.0-alpha.0",
|
|
51
|
+
"@modern-js/babel-preset": "3.0.0-alpha.0",
|
|
52
|
+
"@modern-js/utils": "3.0.0-alpha.0"
|
|
45
53
|
},
|
|
46
54
|
"devDependencies": {
|
|
55
|
+
"@rslib/core": "0.18.5",
|
|
47
56
|
"@types/babel__core": "^7.20.5",
|
|
48
|
-
"@types/jest": "^29",
|
|
49
|
-
"@types/node": "^
|
|
50
|
-
"jest": "^29",
|
|
51
|
-
"ts-jest": "^29.
|
|
57
|
+
"@types/jest": "^29.5.14",
|
|
58
|
+
"@types/node": "^20",
|
|
59
|
+
"jest": "^29.7.0",
|
|
60
|
+
"ts-jest": "^29.4.6",
|
|
52
61
|
"typescript": "^5",
|
|
53
|
-
"@modern-js/server-core": "
|
|
54
|
-
"@scripts/
|
|
55
|
-
"@
|
|
62
|
+
"@modern-js/server-core": "3.0.0-alpha.0",
|
|
63
|
+
"@scripts/jest-config": "2.66.0",
|
|
64
|
+
"@modern-js/rslib": "2.68.10"
|
|
56
65
|
},
|
|
57
66
|
"sideEffects": false,
|
|
58
67
|
"publishConfig": {
|
|
@@ -60,9 +69,8 @@
|
|
|
60
69
|
"access": "public"
|
|
61
70
|
},
|
|
62
71
|
"scripts": {
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"build": "modern-lib build",
|
|
72
|
+
"dev": "rslib build --watch",
|
|
73
|
+
"build": "rslib build",
|
|
66
74
|
"test": "jest --passWithNoTests"
|
|
67
75
|
}
|
|
68
76
|
}
|
package/rslib.config.mts
ADDED
package/dist/esm/common/index.js
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
3
|
-
import * as path from "path";
|
|
4
|
-
import { fs } from "@modern-js/utils";
|
|
5
|
-
var FILE_EXTENSIONS = [
|
|
6
|
-
".js",
|
|
7
|
-
".ts",
|
|
8
|
-
".mjs",
|
|
9
|
-
".ejs"
|
|
10
|
-
];
|
|
11
|
-
var validateAbsolutePath = function(filename, message) {
|
|
12
|
-
if (!path.isAbsolute(filename)) {
|
|
13
|
-
throw new Error(message);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
var validateAbsolutePaths = function(filenames, messageFunc) {
|
|
17
|
-
filenames.forEach(function(filename) {
|
|
18
|
-
return validateAbsolutePath(filename, messageFunc(filename));
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
var compile = function() {
|
|
22
|
-
var _ref = _async_to_generator(function(appDirectory, modernConfig, compileOptions) {
|
|
23
|
-
var _modernConfig_server, sourceDirs, distDir, tsconfigPath, compiler, isTsProject, _tmp, compileByBabel, compileByTs;
|
|
24
|
-
return _ts_generator(this, function(_state) {
|
|
25
|
-
switch (_state.label) {
|
|
26
|
-
case 0:
|
|
27
|
-
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
|
|
28
|
-
validateAbsolutePaths(sourceDirs, function(dir) {
|
|
29
|
-
return "source dir ".concat(dir, " is not an absolute path.");
|
|
30
|
-
});
|
|
31
|
-
validateAbsolutePath(distDir, "dist dir ".concat(distDir, " is not an absolute path."));
|
|
32
|
-
compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
|
|
33
|
-
_tmp = tsconfigPath;
|
|
34
|
-
if (!_tmp)
|
|
35
|
-
return [
|
|
36
|
-
3,
|
|
37
|
-
2
|
|
38
|
-
];
|
|
39
|
-
return [
|
|
40
|
-
4,
|
|
41
|
-
fs.pathExists(tsconfigPath)
|
|
42
|
-
];
|
|
43
|
-
case 1:
|
|
44
|
-
_tmp = _state.sent();
|
|
45
|
-
_state.label = 2;
|
|
46
|
-
case 2:
|
|
47
|
-
isTsProject = _tmp;
|
|
48
|
-
if (!(!isTsProject || compiler === "babel"))
|
|
49
|
-
return [
|
|
50
|
-
3,
|
|
51
|
-
5
|
|
52
|
-
];
|
|
53
|
-
return [
|
|
54
|
-
4,
|
|
55
|
-
import("../compilers/babel")
|
|
56
|
-
];
|
|
57
|
-
case 3:
|
|
58
|
-
compileByBabel = _state.sent().compileByBabel;
|
|
59
|
-
return [
|
|
60
|
-
4,
|
|
61
|
-
compileByBabel(appDirectory, modernConfig, compileOptions)
|
|
62
|
-
];
|
|
63
|
-
case 4:
|
|
64
|
-
_state.sent();
|
|
65
|
-
return [
|
|
66
|
-
3,
|
|
67
|
-
8
|
|
68
|
-
];
|
|
69
|
-
case 5:
|
|
70
|
-
return [
|
|
71
|
-
4,
|
|
72
|
-
import("../compilers/typescript")
|
|
73
|
-
];
|
|
74
|
-
case 6:
|
|
75
|
-
compileByTs = _state.sent().compileByTs;
|
|
76
|
-
return [
|
|
77
|
-
4,
|
|
78
|
-
compileByTs(appDirectory, modernConfig, compileOptions)
|
|
79
|
-
];
|
|
80
|
-
case 7:
|
|
81
|
-
_state.sent();
|
|
82
|
-
_state.label = 8;
|
|
83
|
-
case 8:
|
|
84
|
-
return [
|
|
85
|
-
2
|
|
86
|
-
];
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
return function compile2(appDirectory, modernConfig, compileOptions) {
|
|
91
|
-
return _ref.apply(this, arguments);
|
|
92
|
-
};
|
|
93
|
-
}();
|
|
94
|
-
export {
|
|
95
|
-
FILE_EXTENSIONS,
|
|
96
|
-
compile
|
|
97
|
-
};
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
4
|
-
import * as path from "path";
|
|
5
|
-
import { compiler } from "@modern-js/babel-compiler";
|
|
6
|
-
import { fs, getAliasConfig, json5 } from "@modern-js/utils";
|
|
7
|
-
import { FILE_EXTENSIONS } from "../../common";
|
|
8
|
-
import { applyUserBabelConfig, getBabelConfig } from "./preset";
|
|
9
|
-
export * from "@babel/core";
|
|
10
|
-
var readTsConfig = function(tsconfigPath) {
|
|
11
|
-
var noExistReturn = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : null;
|
|
12
|
-
if (!fs.existsSync(tsconfigPath)) {
|
|
13
|
-
return noExistReturn;
|
|
14
|
-
}
|
|
15
|
-
var content = fs.readFileSync(tsconfigPath, "utf-8");
|
|
16
|
-
return json5.parse(content);
|
|
17
|
-
};
|
|
18
|
-
var existTsConfigFile = function(tsconfigAbsolutePath) {
|
|
19
|
-
var tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
20
|
-
return Boolean(tsconfig);
|
|
21
|
-
};
|
|
22
|
-
var resolveBabelConfig = function(appDirectory, config, option, isEsm) {
|
|
23
|
-
var alias = config.alias, babelConfig = config.babelConfig;
|
|
24
|
-
var aliasConfig = getAliasConfig(alias, _object_spread({
|
|
25
|
-
appDirectory
|
|
26
|
-
}, option));
|
|
27
|
-
var defaultBabelConfig = getBabelConfig({
|
|
28
|
-
appDirectory,
|
|
29
|
-
alias: aliasConfig,
|
|
30
|
-
isEsm
|
|
31
|
-
});
|
|
32
|
-
return applyUserBabelConfig(defaultBabelConfig, babelConfig);
|
|
33
|
-
};
|
|
34
|
-
var compileByBabel = function() {
|
|
35
|
-
var _ref = _async_to_generator(function(appDirectory, config, compileOptions) {
|
|
36
|
-
var sourceDirs, distDir, tsconfigPath, moduleType, isEsm, results;
|
|
37
|
-
return _ts_generator(this, function(_state) {
|
|
38
|
-
switch (_state.label) {
|
|
39
|
-
case 0:
|
|
40
|
-
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath, moduleType = compileOptions.moduleType;
|
|
41
|
-
isEsm = moduleType === "module";
|
|
42
|
-
return [
|
|
43
|
-
4,
|
|
44
|
-
Promise.all(sourceDirs.map(function() {
|
|
45
|
-
var _ref2 = _async_to_generator(function(sourceDir) {
|
|
46
|
-
var babelConfig, basename, targetDir;
|
|
47
|
-
return _ts_generator(this, function(_state2) {
|
|
48
|
-
switch (_state2.label) {
|
|
49
|
-
case 0:
|
|
50
|
-
babelConfig = resolveBabelConfig(appDirectory, config, {
|
|
51
|
-
tsconfigPath: tsconfigPath ? tsconfigPath : ""
|
|
52
|
-
}, isEsm);
|
|
53
|
-
return [
|
|
54
|
-
4,
|
|
55
|
-
fs.pathExists(sourceDir)
|
|
56
|
-
];
|
|
57
|
-
case 1:
|
|
58
|
-
if (!_state2.sent())
|
|
59
|
-
return [
|
|
60
|
-
3,
|
|
61
|
-
3
|
|
62
|
-
];
|
|
63
|
-
basename = path.basename(sourceDir);
|
|
64
|
-
targetDir = path.join(distDir, basename);
|
|
65
|
-
return [
|
|
66
|
-
4,
|
|
67
|
-
fs.copy(sourceDir, targetDir, {
|
|
68
|
-
filter: function(src) {
|
|
69
|
-
return ![
|
|
70
|
-
".ts",
|
|
71
|
-
".js"
|
|
72
|
-
].includes(path.extname(src)) && src !== tsconfigPath;
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
];
|
|
76
|
-
case 2:
|
|
77
|
-
_state2.sent();
|
|
78
|
-
_state2.label = 3;
|
|
79
|
-
case 3:
|
|
80
|
-
return [
|
|
81
|
-
2,
|
|
82
|
-
compiler({
|
|
83
|
-
rootDir: appDirectory,
|
|
84
|
-
distDir,
|
|
85
|
-
sourceDir,
|
|
86
|
-
extensions: FILE_EXTENSIONS
|
|
87
|
-
}, babelConfig)
|
|
88
|
-
];
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
return function(sourceDir) {
|
|
93
|
-
return _ref2.apply(this, arguments);
|
|
94
|
-
};
|
|
95
|
-
}()))
|
|
96
|
-
];
|
|
97
|
-
case 1:
|
|
98
|
-
results = _state.sent();
|
|
99
|
-
results.forEach(function(result) {
|
|
100
|
-
if (result.code === 1) {
|
|
101
|
-
throw new Error(result.message);
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
return [
|
|
105
|
-
2
|
|
106
|
-
];
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
return function compileByBabel2(appDirectory, config, compileOptions) {
|
|
111
|
-
return _ref.apply(this, arguments);
|
|
112
|
-
};
|
|
113
|
-
}();
|
|
114
|
-
export {
|
|
115
|
-
compileByBabel,
|
|
116
|
-
existTsConfigFile,
|
|
117
|
-
readTsConfig,
|
|
118
|
-
resolveBabelConfig
|
|
119
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
|
-
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
|
-
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
|
-
import path from "path";
|
|
5
|
-
import { getUserAlias } from "@modern-js/utils";
|
|
6
|
-
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
7
|
-
var resolvePath = require("@modern-js/babel-plugin-module-resolver").resolvePath;
|
|
8
|
-
var defaultPaths = {
|
|
9
|
-
"@": [
|
|
10
|
-
"./src"
|
|
11
|
-
]
|
|
12
|
-
};
|
|
13
|
-
var aliasPlugin = function(alias) {
|
|
14
|
-
var absoluteBaseUrl = alias.absoluteBaseUrl, isTsPath = alias.isTsPath, _alias_isTsProject = alias.isTsProject, isTsProject = _alias_isTsProject === void 0 ? false : _alias_isTsProject;
|
|
15
|
-
var mergedPaths = isTsPath ? alias.paths || {} : _object_spread({}, defaultPaths, alias.paths || {});
|
|
16
|
-
var tsPaths = {};
|
|
17
|
-
if (isTsProject) {
|
|
18
|
-
tsPaths = getUserAlias(mergedPaths);
|
|
19
|
-
}
|
|
20
|
-
tsPaths = Object.keys(tsPaths).reduce(function(o, key) {
|
|
21
|
-
if (typeof tsPaths[key] === "string") {
|
|
22
|
-
return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), [
|
|
23
|
-
tsPaths[key]
|
|
24
|
-
]));
|
|
25
|
-
}
|
|
26
|
-
return _object_spread_props(_object_spread({}, o), _define_property({}, "".concat(key), tsPaths[key]));
|
|
27
|
-
}, {});
|
|
28
|
-
var resolvePathFn = function(sourcePath, currentFile, opts) {
|
|
29
|
-
if (sourcePath === "." || sourcePath === "./") {
|
|
30
|
-
return sourcePath;
|
|
31
|
-
}
|
|
32
|
-
var matchPath = createMatchPath(absoluteBaseUrl, tsPaths, [
|
|
33
|
-
"index"
|
|
34
|
-
]);
|
|
35
|
-
var result = matchPath(sourcePath, void 0, void 0, [
|
|
36
|
-
".js",
|
|
37
|
-
".jsx",
|
|
38
|
-
".ts",
|
|
39
|
-
".tsx"
|
|
40
|
-
]);
|
|
41
|
-
if (result) {
|
|
42
|
-
var relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
|
|
43
|
-
var fileName = path.basename(result);
|
|
44
|
-
var filePath = path.normalize("".concat(relativePath.length === 0 ? "." : relativePath, "/").concat(fileName)).replace(/\\/, "/");
|
|
45
|
-
return filePath.startsWith(".") ? filePath : "./".concat(filePath);
|
|
46
|
-
}
|
|
47
|
-
return resolvePath(sourcePath, currentFile, opts);
|
|
48
|
-
};
|
|
49
|
-
var typescriptExts = [
|
|
50
|
-
".ts",
|
|
51
|
-
".tsx",
|
|
52
|
-
".js",
|
|
53
|
-
".jsx",
|
|
54
|
-
".es",
|
|
55
|
-
".es6",
|
|
56
|
-
".mjs"
|
|
57
|
-
];
|
|
58
|
-
return [
|
|
59
|
-
require.resolve("@modern-js/babel-plugin-module-resolver"),
|
|
60
|
-
{
|
|
61
|
-
root: absoluteBaseUrl,
|
|
62
|
-
alias: mergedPaths,
|
|
63
|
-
resolvePath: isTsPath ? resolvePathFn : void 0,
|
|
64
|
-
extensions: isTsProject ? typescriptExts : void 0
|
|
65
|
-
}
|
|
66
|
-
];
|
|
67
|
-
};
|
|
68
|
-
export {
|
|
69
|
-
aliasPlugin
|
|
70
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getBabelConfigForNode } from "@modern-js/babel-preset/node";
|
|
2
|
-
import { aliasPlugin } from "./alias";
|
|
3
|
-
var getBabelConfig = function(libPresetOption) {
|
|
4
|
-
var _config_presets, _config_plugins;
|
|
5
|
-
var isEsm = libPresetOption.isEsm;
|
|
6
|
-
var config = getBabelConfigForNode({
|
|
7
|
-
presetEnv: {
|
|
8
|
-
loose: true,
|
|
9
|
-
modules: isEsm ? false : "commonjs",
|
|
10
|
-
targets: [
|
|
11
|
-
"node >= 14"
|
|
12
|
-
]
|
|
13
|
-
},
|
|
14
|
-
pluginDecorators: {
|
|
15
|
-
version: "legacy"
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
(_config_presets = config.presets) === null || _config_presets === void 0 ? void 0 : _config_presets.push([
|
|
19
|
-
require.resolve("@babel/preset-react"),
|
|
20
|
-
{
|
|
21
|
-
runtime: "automatic"
|
|
22
|
-
}
|
|
23
|
-
]);
|
|
24
|
-
if (libPresetOption.alias) {
|
|
25
|
-
var _config_plugins1;
|
|
26
|
-
(_config_plugins1 = config.plugins) === null || _config_plugins1 === void 0 ? void 0 : _config_plugins1.push(aliasPlugin(libPresetOption.alias));
|
|
27
|
-
}
|
|
28
|
-
(_config_plugins = config.plugins) === null || _config_plugins === void 0 ? void 0 : _config_plugins.push(require.resolve("babel-plugin-transform-typescript-metadata"));
|
|
29
|
-
return config;
|
|
30
|
-
};
|
|
31
|
-
export * from "./types";
|
|
32
|
-
import { applyUserBabelConfig } from "@modern-js/utils";
|
|
33
|
-
export {
|
|
34
|
-
applyUserBabelConfig,
|
|
35
|
-
getBabelConfig
|
|
36
|
-
};
|