@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
|
@@ -1,202 +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 _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
-
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { fs, getAliasConfig, logger } from "@modern-js/utils";
|
|
7
|
-
import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
|
|
8
|
-
import { TypescriptLoader } from "./typescriptLoader";
|
|
9
|
-
var readTsConfigByFile = function(tsConfigFile, tsInstance) {
|
|
10
|
-
var parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
|
|
11
|
-
var options = parsedCmd.options, fileNames = parsedCmd.fileNames, projectReferences = parsedCmd.projectReferences;
|
|
12
|
-
return {
|
|
13
|
-
options,
|
|
14
|
-
fileNames,
|
|
15
|
-
projectReferences
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
var copyFiles = function() {
|
|
19
|
-
var _ref = _async_to_generator(function(from, to, appDirectory) {
|
|
20
|
-
var relativePath, targetDir;
|
|
21
|
-
return _ts_generator(this, function(_state) {
|
|
22
|
-
switch (_state.label) {
|
|
23
|
-
case 0:
|
|
24
|
-
return [
|
|
25
|
-
4,
|
|
26
|
-
fs.pathExists(from)
|
|
27
|
-
];
|
|
28
|
-
case 1:
|
|
29
|
-
if (!_state.sent())
|
|
30
|
-
return [
|
|
31
|
-
3,
|
|
32
|
-
3
|
|
33
|
-
];
|
|
34
|
-
relativePath = path.relative(appDirectory, from);
|
|
35
|
-
targetDir = path.join(to, relativePath);
|
|
36
|
-
return [
|
|
37
|
-
4,
|
|
38
|
-
fs.copy(from, targetDir, {
|
|
39
|
-
filter: function(src) {
|
|
40
|
-
return ![
|
|
41
|
-
".ts",
|
|
42
|
-
".js"
|
|
43
|
-
].includes(path.extname(src)) && !src.endsWith("tsconfig.json");
|
|
44
|
-
}
|
|
45
|
-
})
|
|
46
|
-
];
|
|
47
|
-
case 2:
|
|
48
|
-
_state.sent();
|
|
49
|
-
_state.label = 3;
|
|
50
|
-
case 3:
|
|
51
|
-
return [
|
|
52
|
-
2
|
|
53
|
-
];
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
return function copyFiles2(from, to, appDirectory) {
|
|
58
|
-
return _ref.apply(this, arguments);
|
|
59
|
-
};
|
|
60
|
-
}();
|
|
61
|
-
var compileByTs = function() {
|
|
62
|
-
var _ref = _async_to_generator(function(appDirectory, config, compileOptions) {
|
|
63
|
-
var sourceDirs, distDir, tsconfigPath, ts, createProgram, formatHost, alias, aliasOption, _aliasOption_paths, paths, _aliasOption_absoluteBaseUrl, absoluteBaseUrl, _readTsConfigByFile, options, fileNames, projectReferences, sourcePosixPaths, rootNames, program, tsconfigPathsPlugin, emitResult, allDiagnostics, noEmitOnError, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, err;
|
|
64
|
-
return _ts_generator(this, function(_state) {
|
|
65
|
-
switch (_state.label) {
|
|
66
|
-
case 0:
|
|
67
|
-
logger.info("Running ts compile...");
|
|
68
|
-
sourceDirs = compileOptions.sourceDirs, distDir = compileOptions.distDir, tsconfigPath = compileOptions.tsconfigPath;
|
|
69
|
-
if (!tsconfigPath) {
|
|
70
|
-
return [
|
|
71
|
-
2
|
|
72
|
-
];
|
|
73
|
-
}
|
|
74
|
-
ts = new TypescriptLoader({
|
|
75
|
-
appDirectory
|
|
76
|
-
}).load();
|
|
77
|
-
createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
78
|
-
formatHost = getFormatHost(ts);
|
|
79
|
-
alias = config.alias;
|
|
80
|
-
aliasOption = getAliasConfig(alias, {
|
|
81
|
-
appDirectory,
|
|
82
|
-
tsconfigPath
|
|
83
|
-
});
|
|
84
|
-
_aliasOption_paths = aliasOption.paths, paths = _aliasOption_paths === void 0 ? {} : _aliasOption_paths, _aliasOption_absoluteBaseUrl = aliasOption.absoluteBaseUrl, absoluteBaseUrl = _aliasOption_absoluteBaseUrl === void 0 ? "./" : _aliasOption_absoluteBaseUrl;
|
|
85
|
-
_readTsConfigByFile = readTsConfigByFile(tsconfigPath, ts), options = _readTsConfigByFile.options, fileNames = _readTsConfigByFile.fileNames, projectReferences = _readTsConfigByFile.projectReferences;
|
|
86
|
-
sourcePosixPaths = sourceDirs.map(function(sourceDir) {
|
|
87
|
-
return sourceDir.split(path.sep).join(path.posix.sep);
|
|
88
|
-
});
|
|
89
|
-
rootNames = fileNames.filter(function(fileName) {
|
|
90
|
-
return fileName.endsWith(".d.ts") || sourcePosixPaths.some(function(sourceDir) {
|
|
91
|
-
return fileName.includes(sourceDir);
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
program = createProgram.call(ts, {
|
|
95
|
-
rootNames,
|
|
96
|
-
projectReferences,
|
|
97
|
-
options: _object_spread({
|
|
98
|
-
rootDir: appDirectory,
|
|
99
|
-
outDir: distDir
|
|
100
|
-
}, options)
|
|
101
|
-
});
|
|
102
|
-
tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
103
|
-
emitResult = program.emit(void 0, void 0, void 0, void 0, {
|
|
104
|
-
before: [
|
|
105
|
-
tsconfigPathsPlugin
|
|
106
|
-
]
|
|
107
|
-
});
|
|
108
|
-
allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
109
|
-
noEmitOnError = options.noEmitOnError;
|
|
110
|
-
if (allDiagnostics.length > 0) {
|
|
111
|
-
logger.error(ts.formatDiagnosticsWithColorAndContext(_to_consumable_array(new Set(allDiagnostics)), formatHost));
|
|
112
|
-
if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
|
|
113
|
-
if (compileOptions.throwErrorInsteadOfExit) {
|
|
114
|
-
logger.error("TypeScript compilation failed");
|
|
115
|
-
} else {
|
|
116
|
-
process.exit(1);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
121
|
-
_state.label = 1;
|
|
122
|
-
case 1:
|
|
123
|
-
_state.trys.push([
|
|
124
|
-
1,
|
|
125
|
-
6,
|
|
126
|
-
7,
|
|
127
|
-
8
|
|
128
|
-
]);
|
|
129
|
-
_iterator = sourceDirs[Symbol.iterator]();
|
|
130
|
-
_state.label = 2;
|
|
131
|
-
case 2:
|
|
132
|
-
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done))
|
|
133
|
-
return [
|
|
134
|
-
3,
|
|
135
|
-
5
|
|
136
|
-
];
|
|
137
|
-
source = _step.value;
|
|
138
|
-
return [
|
|
139
|
-
4,
|
|
140
|
-
copyFiles(source, distDir, appDirectory)
|
|
141
|
-
];
|
|
142
|
-
case 3:
|
|
143
|
-
_state.sent();
|
|
144
|
-
_state.label = 4;
|
|
145
|
-
case 4:
|
|
146
|
-
_iteratorNormalCompletion = true;
|
|
147
|
-
return [
|
|
148
|
-
3,
|
|
149
|
-
2
|
|
150
|
-
];
|
|
151
|
-
case 5:
|
|
152
|
-
return [
|
|
153
|
-
3,
|
|
154
|
-
8
|
|
155
|
-
];
|
|
156
|
-
case 6:
|
|
157
|
-
err = _state.sent();
|
|
158
|
-
_didIteratorError = true;
|
|
159
|
-
_iteratorError = err;
|
|
160
|
-
return [
|
|
161
|
-
3,
|
|
162
|
-
8
|
|
163
|
-
];
|
|
164
|
-
case 7:
|
|
165
|
-
try {
|
|
166
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
167
|
-
_iterator.return();
|
|
168
|
-
}
|
|
169
|
-
} finally {
|
|
170
|
-
if (_didIteratorError) {
|
|
171
|
-
throw _iteratorError;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
return [
|
|
175
|
-
7
|
|
176
|
-
];
|
|
177
|
-
case 8:
|
|
178
|
-
logger.info("Ts compile succeed");
|
|
179
|
-
return [
|
|
180
|
-
2
|
|
181
|
-
];
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
return function compileByTs2(appDirectory, config, compileOptions) {
|
|
186
|
-
return _ref.apply(this, arguments);
|
|
187
|
-
};
|
|
188
|
-
}();
|
|
189
|
-
var getFormatHost = function(ts) {
|
|
190
|
-
return {
|
|
191
|
-
getCanonicalFileName: function(path2) {
|
|
192
|
-
return path2;
|
|
193
|
-
},
|
|
194
|
-
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
195
|
-
getNewLine: function() {
|
|
196
|
-
return ts.sys.newLine;
|
|
197
|
-
}
|
|
198
|
-
};
|
|
199
|
-
};
|
|
200
|
-
export {
|
|
201
|
-
compileByTs
|
|
202
|
-
};
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { _ as _instanceof } from "@swc/helpers/_/_instanceof";
|
|
2
|
-
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
|
|
3
|
-
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
-
import * as os from "os";
|
|
5
|
-
import path, { dirname, posix } from "path";
|
|
6
|
-
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
7
|
-
import * as ts from "typescript";
|
|
8
|
-
var isRegExpKey = function(str) {
|
|
9
|
-
return str.startsWith("^") || str.endsWith("$");
|
|
10
|
-
};
|
|
11
|
-
var resolveAliasPath = function(baseUrl, filePath) {
|
|
12
|
-
if (filePath.startsWith(".") || filePath.startsWith("..")) {
|
|
13
|
-
return path.resolve(baseUrl, filePath);
|
|
14
|
-
}
|
|
15
|
-
return filePath;
|
|
16
|
-
};
|
|
17
|
-
var createAliasMatcher = function(baseUrl, alias) {
|
|
18
|
-
var aliasPairs = Object.keys(alias).reduce(function(o, key) {
|
|
19
|
-
if (isRegExpKey(key)) {
|
|
20
|
-
var regexp = new RegExp(key);
|
|
21
|
-
var aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
22
|
-
o.push([
|
|
23
|
-
regexp,
|
|
24
|
-
aliasPath
|
|
25
|
-
]);
|
|
26
|
-
} else {
|
|
27
|
-
var aliasPath1 = resolveAliasPath(baseUrl, alias[key]);
|
|
28
|
-
o.push([
|
|
29
|
-
key,
|
|
30
|
-
aliasPath1
|
|
31
|
-
]);
|
|
32
|
-
}
|
|
33
|
-
return o;
|
|
34
|
-
}, []);
|
|
35
|
-
var cacheMap = /* @__PURE__ */ new Map();
|
|
36
|
-
return function(requestedModule) {
|
|
37
|
-
if (cacheMap.has(requestedModule)) {
|
|
38
|
-
return cacheMap.get(requestedModule);
|
|
39
|
-
}
|
|
40
|
-
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
|
|
41
|
-
try {
|
|
42
|
-
for (var _iterator = aliasPairs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
43
|
-
var _step_value = _sliced_to_array(_step.value, 2), key = _step_value[0], value = _step_value[1];
|
|
44
|
-
if (_instanceof(key, RegExp)) {
|
|
45
|
-
if (key.test(requestedModule)) {
|
|
46
|
-
cacheMap.set(requestedModule, value);
|
|
47
|
-
return value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
if (requestedModule === key) {
|
|
51
|
-
cacheMap.set(requestedModule, value);
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
} catch (err) {
|
|
56
|
-
_didIteratorError = true;
|
|
57
|
-
_iteratorError = err;
|
|
58
|
-
} finally {
|
|
59
|
-
try {
|
|
60
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
61
|
-
_iterator.return();
|
|
62
|
-
}
|
|
63
|
-
} finally {
|
|
64
|
-
if (_didIteratorError) {
|
|
65
|
-
throw _iteratorError;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
var isDynamicImport = function(tsBinary, node) {
|
|
72
|
-
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
73
|
-
};
|
|
74
|
-
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
75
|
-
var tsPaths = {};
|
|
76
|
-
var alias = {};
|
|
77
|
-
Object.keys(paths).forEach(function(key) {
|
|
78
|
-
if (Array.isArray(paths[key])) {
|
|
79
|
-
tsPaths[key] = paths[key];
|
|
80
|
-
} else {
|
|
81
|
-
alias[key] = paths[key];
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
var matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
85
|
-
var matchTsPath = createMatchPath(baseUrl, tsPaths, [
|
|
86
|
-
"main"
|
|
87
|
-
]);
|
|
88
|
-
var matchPath = function(requestedModule, readJSONSync, fileExists, extensions) {
|
|
89
|
-
var result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
90
|
-
if (result) {
|
|
91
|
-
return result;
|
|
92
|
-
}
|
|
93
|
-
return matchAliasPath(requestedModule);
|
|
94
|
-
};
|
|
95
|
-
if (Object.keys(paths).length === 0) {
|
|
96
|
-
return void 0;
|
|
97
|
-
}
|
|
98
|
-
return function(ctx) {
|
|
99
|
-
return function(sf) {
|
|
100
|
-
var visitNode = function(node) {
|
|
101
|
-
if (isDynamicImport(tsBinary, node)) {
|
|
102
|
-
var importPathWithQuotes = node.arguments[0].getText(sf);
|
|
103
|
-
var text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
104
|
-
var result = getNotAliasedPath(sf, matchPath, text);
|
|
105
|
-
if (!result) {
|
|
106
|
-
return node;
|
|
107
|
-
}
|
|
108
|
-
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
|
|
109
|
-
tsBinary.factory.createStringLiteral(result)
|
|
110
|
-
]));
|
|
111
|
-
}
|
|
112
|
-
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
113
|
-
try {
|
|
114
|
-
var _node_moduleSpecifier;
|
|
115
|
-
var importPathWithQuotes1 = node === null || node === void 0 ? void 0 : (_node_moduleSpecifier = node.moduleSpecifier) === null || _node_moduleSpecifier === void 0 ? void 0 : _node_moduleSpecifier.getText();
|
|
116
|
-
if (!importPathWithQuotes1) {
|
|
117
|
-
return node;
|
|
118
|
-
}
|
|
119
|
-
var text1 = importPathWithQuotes1.substring(1, importPathWithQuotes1.length - 1);
|
|
120
|
-
var result1 = getNotAliasedPath(sf, matchPath, text1);
|
|
121
|
-
if (!result1) {
|
|
122
|
-
return node;
|
|
123
|
-
}
|
|
124
|
-
var moduleSpecifier = tsBinary.factory.createStringLiteral(result1);
|
|
125
|
-
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
126
|
-
var newNode;
|
|
127
|
-
if (tsBinary.isImportDeclaration(node)) {
|
|
128
|
-
newNode = tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
129
|
-
} else {
|
|
130
|
-
newNode = tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
131
|
-
}
|
|
132
|
-
newNode.flags = node.flags;
|
|
133
|
-
return newNode;
|
|
134
|
-
} catch (e) {
|
|
135
|
-
return node;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
139
|
-
};
|
|
140
|
-
return tsBinary.visitNode(sf, visitNode);
|
|
141
|
-
};
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
function getNotAliasedPath(sf, matcher, text) {
|
|
145
|
-
var result = matcher(text, void 0, void 0, [
|
|
146
|
-
".ts",
|
|
147
|
-
".tsx",
|
|
148
|
-
".js",
|
|
149
|
-
".jsx"
|
|
150
|
-
]);
|
|
151
|
-
if (!result) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
if (os.platform() === "win32") {
|
|
155
|
-
result = result.replace(/\\/g, "/");
|
|
156
|
-
}
|
|
157
|
-
if (!path.isAbsolute(result)) {
|
|
158
|
-
if (!result.startsWith(".") && !result.startsWith("..")) {
|
|
159
|
-
try {
|
|
160
|
-
var packagePath = require.resolve(result, {
|
|
161
|
-
paths: [
|
|
162
|
-
process.cwd()
|
|
163
|
-
].concat(_to_consumable_array(module.paths))
|
|
164
|
-
});
|
|
165
|
-
if (packagePath) {
|
|
166
|
-
return result;
|
|
167
|
-
}
|
|
168
|
-
} catch (e) {
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
try {
|
|
172
|
-
var packagePath1 = require.resolve(text, {
|
|
173
|
-
paths: [
|
|
174
|
-
process.cwd()
|
|
175
|
-
].concat(_to_consumable_array(module.paths))
|
|
176
|
-
});
|
|
177
|
-
if (packagePath1) {
|
|
178
|
-
return text;
|
|
179
|
-
}
|
|
180
|
-
} catch (e) {
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
var resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
|
|
184
|
-
return resolvedPath[0] === "." ? resolvedPath : "./".concat(resolvedPath);
|
|
185
|
-
}
|
|
186
|
-
export {
|
|
187
|
-
tsconfigPathsBeforeHookFactory
|
|
188
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
2
|
-
var TypescriptLoader = /* @__PURE__ */ function() {
|
|
3
|
-
"use strict";
|
|
4
|
-
function TypescriptLoader2(param) {
|
|
5
|
-
var appDirectory = param.appDirectory;
|
|
6
|
-
_class_call_check(this, TypescriptLoader2);
|
|
7
|
-
this.appDirectory = appDirectory;
|
|
8
|
-
}
|
|
9
|
-
var _proto = TypescriptLoader2.prototype;
|
|
10
|
-
_proto.load = function load() {
|
|
11
|
-
if (this.tsBinary) {
|
|
12
|
-
return this.tsBinary;
|
|
13
|
-
}
|
|
14
|
-
try {
|
|
15
|
-
var tsPath = require.resolve("typescript", {
|
|
16
|
-
paths: [
|
|
17
|
-
this.appDirectory || process.cwd()
|
|
18
|
-
]
|
|
19
|
-
});
|
|
20
|
-
var ts = require(tsPath);
|
|
21
|
-
return ts;
|
|
22
|
-
} catch (error) {
|
|
23
|
-
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
return TypescriptLoader2;
|
|
27
|
-
}();
|
|
28
|
-
export {
|
|
29
|
-
TypescriptLoader
|
|
30
|
-
};
|
package/dist/esm/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import { fs } from "@modern-js/utils";
|
|
3
|
-
const FILE_EXTENSIONS = [
|
|
4
|
-
".js",
|
|
5
|
-
".ts",
|
|
6
|
-
".mjs",
|
|
7
|
-
".ejs"
|
|
8
|
-
];
|
|
9
|
-
const validateAbsolutePath = (filename, message) => {
|
|
10
|
-
if (!path.isAbsolute(filename)) {
|
|
11
|
-
throw new Error(message);
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
const validateAbsolutePaths = (filenames, messageFunc) => {
|
|
15
|
-
filenames.forEach((filename) => validateAbsolutePath(filename, messageFunc(filename)));
|
|
16
|
-
};
|
|
17
|
-
const compile = async (appDirectory, modernConfig, compileOptions) => {
|
|
18
|
-
var _modernConfig_server;
|
|
19
|
-
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
20
|
-
validateAbsolutePaths(sourceDirs, (dir) => `source dir ${dir} is not an absolute path.`);
|
|
21
|
-
validateAbsolutePath(distDir, `dist dir ${distDir} is not an absolute path.`);
|
|
22
|
-
const compiler = modernConfig === null || modernConfig === void 0 ? void 0 : (_modernConfig_server = modernConfig.server) === null || _modernConfig_server === void 0 ? void 0 : _modernConfig_server.compiler;
|
|
23
|
-
const isTsProject = tsconfigPath && await fs.pathExists(tsconfigPath);
|
|
24
|
-
if (!isTsProject || compiler === "babel") {
|
|
25
|
-
const { compileByBabel } = await import("../compilers/babel");
|
|
26
|
-
await compileByBabel(appDirectory, modernConfig, compileOptions);
|
|
27
|
-
} else {
|
|
28
|
-
const { compileByTs } = await import("../compilers/typescript");
|
|
29
|
-
await compileByTs(appDirectory, modernConfig, compileOptions);
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
export {
|
|
33
|
-
FILE_EXTENSIONS,
|
|
34
|
-
compile
|
|
35
|
-
};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import { compiler } from "@modern-js/babel-compiler";
|
|
3
|
-
import { fs, getAliasConfig, json5 } from "@modern-js/utils";
|
|
4
|
-
import { FILE_EXTENSIONS } from "../../common";
|
|
5
|
-
import { applyUserBabelConfig, getBabelConfig } from "./preset";
|
|
6
|
-
export * from "@babel/core";
|
|
7
|
-
const readTsConfig = (tsconfigPath, noExistReturn = null) => {
|
|
8
|
-
if (!fs.existsSync(tsconfigPath)) {
|
|
9
|
-
return noExistReturn;
|
|
10
|
-
}
|
|
11
|
-
const content = fs.readFileSync(tsconfigPath, "utf-8");
|
|
12
|
-
return json5.parse(content);
|
|
13
|
-
};
|
|
14
|
-
const existTsConfigFile = (tsconfigAbsolutePath) => {
|
|
15
|
-
const tsconfig = readTsConfig(tsconfigAbsolutePath);
|
|
16
|
-
return Boolean(tsconfig);
|
|
17
|
-
};
|
|
18
|
-
const resolveBabelConfig = (appDirectory, config, option, isEsm) => {
|
|
19
|
-
const { alias, babelConfig } = config;
|
|
20
|
-
const aliasConfig = getAliasConfig(alias, {
|
|
21
|
-
appDirectory,
|
|
22
|
-
...option
|
|
23
|
-
});
|
|
24
|
-
const defaultBabelConfig = getBabelConfig({
|
|
25
|
-
appDirectory,
|
|
26
|
-
alias: aliasConfig,
|
|
27
|
-
isEsm
|
|
28
|
-
});
|
|
29
|
-
return applyUserBabelConfig(defaultBabelConfig, babelConfig);
|
|
30
|
-
};
|
|
31
|
-
const compileByBabel = async (appDirectory, config, compileOptions) => {
|
|
32
|
-
const { sourceDirs, distDir, tsconfigPath, moduleType } = compileOptions;
|
|
33
|
-
const isEsm = moduleType === "module";
|
|
34
|
-
const results = await Promise.all(sourceDirs.map(async (sourceDir) => {
|
|
35
|
-
const babelConfig = resolveBabelConfig(appDirectory, config, {
|
|
36
|
-
tsconfigPath: tsconfigPath ? tsconfigPath : ""
|
|
37
|
-
}, isEsm);
|
|
38
|
-
if (await fs.pathExists(sourceDir)) {
|
|
39
|
-
const basename = path.basename(sourceDir);
|
|
40
|
-
const targetDir = path.join(distDir, basename);
|
|
41
|
-
await fs.copy(sourceDir, targetDir, {
|
|
42
|
-
filter: (src) => ![
|
|
43
|
-
".ts",
|
|
44
|
-
".js"
|
|
45
|
-
].includes(path.extname(src)) && src !== tsconfigPath
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
return compiler({
|
|
49
|
-
rootDir: appDirectory,
|
|
50
|
-
distDir,
|
|
51
|
-
sourceDir,
|
|
52
|
-
extensions: FILE_EXTENSIONS
|
|
53
|
-
}, babelConfig);
|
|
54
|
-
}));
|
|
55
|
-
results.forEach((result) => {
|
|
56
|
-
if (result.code === 1) {
|
|
57
|
-
throw new Error(result.message);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
};
|
|
61
|
-
export {
|
|
62
|
-
compileByBabel,
|
|
63
|
-
existTsConfigFile,
|
|
64
|
-
readTsConfig,
|
|
65
|
-
resolveBabelConfig
|
|
66
|
-
};
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { getUserAlias } from "@modern-js/utils";
|
|
3
|
-
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
4
|
-
const { resolvePath } = require("@modern-js/babel-plugin-module-resolver");
|
|
5
|
-
const defaultPaths = {
|
|
6
|
-
"@": [
|
|
7
|
-
"./src"
|
|
8
|
-
]
|
|
9
|
-
};
|
|
10
|
-
const aliasPlugin = (alias) => {
|
|
11
|
-
const { absoluteBaseUrl, isTsPath, isTsProject = false } = alias;
|
|
12
|
-
const mergedPaths = isTsPath ? alias.paths || {} : {
|
|
13
|
-
...defaultPaths,
|
|
14
|
-
...alias.paths || {}
|
|
15
|
-
};
|
|
16
|
-
let tsPaths = {};
|
|
17
|
-
if (isTsProject) {
|
|
18
|
-
tsPaths = getUserAlias(mergedPaths);
|
|
19
|
-
}
|
|
20
|
-
tsPaths = Object.keys(tsPaths).reduce((o, key) => {
|
|
21
|
-
if (typeof tsPaths[key] === "string") {
|
|
22
|
-
return {
|
|
23
|
-
...o,
|
|
24
|
-
[`${key}`]: [
|
|
25
|
-
tsPaths[key]
|
|
26
|
-
]
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
return {
|
|
30
|
-
...o,
|
|
31
|
-
[`${key}`]: tsPaths[key]
|
|
32
|
-
};
|
|
33
|
-
}, {});
|
|
34
|
-
const resolvePathFn = (sourcePath, currentFile, opts) => {
|
|
35
|
-
if (sourcePath === "." || sourcePath === "./") {
|
|
36
|
-
return sourcePath;
|
|
37
|
-
}
|
|
38
|
-
const matchPath = createMatchPath(absoluteBaseUrl, tsPaths, [
|
|
39
|
-
"index"
|
|
40
|
-
]);
|
|
41
|
-
const result = matchPath(sourcePath, void 0, void 0, [
|
|
42
|
-
".js",
|
|
43
|
-
".jsx",
|
|
44
|
-
".ts",
|
|
45
|
-
".tsx"
|
|
46
|
-
]);
|
|
47
|
-
if (result) {
|
|
48
|
-
const relativePath = path.relative(path.dirname(currentFile), path.dirname(result));
|
|
49
|
-
const fileName = path.basename(result);
|
|
50
|
-
const filePath = path.normalize(`${relativePath.length === 0 ? "." : relativePath}/${fileName}`).replace(/\\/, "/");
|
|
51
|
-
return filePath.startsWith(".") ? filePath : `./${filePath}`;
|
|
52
|
-
}
|
|
53
|
-
return resolvePath(sourcePath, currentFile, opts);
|
|
54
|
-
};
|
|
55
|
-
const typescriptExts = [
|
|
56
|
-
".ts",
|
|
57
|
-
".tsx",
|
|
58
|
-
".js",
|
|
59
|
-
".jsx",
|
|
60
|
-
".es",
|
|
61
|
-
".es6",
|
|
62
|
-
".mjs"
|
|
63
|
-
];
|
|
64
|
-
return [
|
|
65
|
-
require.resolve("@modern-js/babel-plugin-module-resolver"),
|
|
66
|
-
{
|
|
67
|
-
root: absoluteBaseUrl,
|
|
68
|
-
alias: mergedPaths,
|
|
69
|
-
resolvePath: isTsPath ? resolvePathFn : void 0,
|
|
70
|
-
extensions: isTsProject ? typescriptExts : void 0
|
|
71
|
-
}
|
|
72
|
-
];
|
|
73
|
-
};
|
|
74
|
-
export {
|
|
75
|
-
aliasPlugin
|
|
76
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { getBabelConfigForNode } from "@modern-js/babel-preset/node";
|
|
2
|
-
import { aliasPlugin } from "./alias";
|
|
3
|
-
const getBabelConfig = (libPresetOption) => {
|
|
4
|
-
var _config_presets, _config_plugins;
|
|
5
|
-
const { isEsm } = libPresetOption;
|
|
6
|
-
const 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
|
-
};
|