@modern-js/server-utils 2.69.5 → 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 +32 -24
- 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,93 +0,0 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import { fs, getAliasConfig, logger } from "@modern-js/utils";
|
|
3
|
-
import { tsconfigPathsBeforeHookFactory } from "./tsconfigPathsPlugin";
|
|
4
|
-
import { TypescriptLoader } from "./typescriptLoader";
|
|
5
|
-
const readTsConfigByFile = (tsConfigFile, tsInstance) => {
|
|
6
|
-
const parsedCmd = tsInstance.getParsedCommandLineOfConfigFile(tsConfigFile, void 0, tsInstance.sys);
|
|
7
|
-
const { options, fileNames, projectReferences } = parsedCmd;
|
|
8
|
-
return {
|
|
9
|
-
options,
|
|
10
|
-
fileNames,
|
|
11
|
-
projectReferences
|
|
12
|
-
};
|
|
13
|
-
};
|
|
14
|
-
const copyFiles = async (from, to, appDirectory) => {
|
|
15
|
-
if (await fs.pathExists(from)) {
|
|
16
|
-
const relativePath = path.relative(appDirectory, from);
|
|
17
|
-
const targetDir = path.join(to, relativePath);
|
|
18
|
-
await fs.copy(from, targetDir, {
|
|
19
|
-
filter: (src) => ![
|
|
20
|
-
".ts",
|
|
21
|
-
".js"
|
|
22
|
-
].includes(path.extname(src)) && !src.endsWith("tsconfig.json")
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
const compileByTs = async (appDirectory, config, compileOptions) => {
|
|
27
|
-
logger.info(`Running ts compile...`);
|
|
28
|
-
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
29
|
-
if (!tsconfigPath) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const ts = new TypescriptLoader({
|
|
33
|
-
appDirectory
|
|
34
|
-
}).load();
|
|
35
|
-
const createProgram = ts.createIncrementalProgram || ts.createProgram;
|
|
36
|
-
const formatHost = getFormatHost(ts);
|
|
37
|
-
const { alias } = config;
|
|
38
|
-
const aliasOption = getAliasConfig(alias, {
|
|
39
|
-
appDirectory,
|
|
40
|
-
tsconfigPath
|
|
41
|
-
});
|
|
42
|
-
const { paths = {}, absoluteBaseUrl = "./" } = aliasOption;
|
|
43
|
-
const { options, fileNames, projectReferences } = readTsConfigByFile(tsconfigPath, ts);
|
|
44
|
-
const sourcePosixPaths = sourceDirs.map((sourceDir) => sourceDir.split(path.sep).join(path.posix.sep));
|
|
45
|
-
const rootNames = fileNames.filter((fileName) => {
|
|
46
|
-
return fileName.endsWith(".d.ts") || sourcePosixPaths.some((sourceDir) => {
|
|
47
|
-
return fileName.includes(sourceDir);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
const program = createProgram.call(ts, {
|
|
51
|
-
rootNames,
|
|
52
|
-
projectReferences,
|
|
53
|
-
options: {
|
|
54
|
-
rootDir: appDirectory,
|
|
55
|
-
outDir: distDir,
|
|
56
|
-
...options
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
const tsconfigPathsPlugin = tsconfigPathsBeforeHookFactory(ts, absoluteBaseUrl, paths);
|
|
60
|
-
const emitResult = program.emit(void 0, void 0, void 0, void 0, {
|
|
61
|
-
before: [
|
|
62
|
-
tsconfigPathsPlugin
|
|
63
|
-
]
|
|
64
|
-
});
|
|
65
|
-
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
66
|
-
const { noEmitOnError } = options;
|
|
67
|
-
if (allDiagnostics.length > 0) {
|
|
68
|
-
logger.error(ts.formatDiagnosticsWithColorAndContext([
|
|
69
|
-
...new Set(allDiagnostics)
|
|
70
|
-
], formatHost));
|
|
71
|
-
if (typeof noEmitOnError === "undefined" || noEmitOnError === true) {
|
|
72
|
-
if (compileOptions.throwErrorInsteadOfExit) {
|
|
73
|
-
logger.error("TypeScript compilation failed");
|
|
74
|
-
} else {
|
|
75
|
-
process.exit(1);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
for (const source of sourceDirs) {
|
|
80
|
-
await copyFiles(source, distDir, appDirectory);
|
|
81
|
-
}
|
|
82
|
-
logger.info(`Ts compile succeed`);
|
|
83
|
-
};
|
|
84
|
-
const getFormatHost = (ts) => {
|
|
85
|
-
return {
|
|
86
|
-
getCanonicalFileName: (path2) => path2,
|
|
87
|
-
getCurrentDirectory: ts.sys.getCurrentDirectory,
|
|
88
|
-
getNewLine: () => ts.sys.newLine
|
|
89
|
-
};
|
|
90
|
-
};
|
|
91
|
-
export {
|
|
92
|
-
compileByTs
|
|
93
|
-
};
|
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import * as os from "os";
|
|
2
|
-
import path, { dirname, posix } from "path";
|
|
3
|
-
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
4
|
-
import * as ts from "typescript";
|
|
5
|
-
const isRegExpKey = (str) => {
|
|
6
|
-
return str.startsWith("^") || str.endsWith("$");
|
|
7
|
-
};
|
|
8
|
-
const resolveAliasPath = (baseUrl, filePath) => {
|
|
9
|
-
if (filePath.startsWith(".") || filePath.startsWith("..")) {
|
|
10
|
-
return path.resolve(baseUrl, filePath);
|
|
11
|
-
}
|
|
12
|
-
return filePath;
|
|
13
|
-
};
|
|
14
|
-
const createAliasMatcher = (baseUrl, alias) => {
|
|
15
|
-
const aliasPairs = Object.keys(alias).reduce((o, key) => {
|
|
16
|
-
if (isRegExpKey(key)) {
|
|
17
|
-
const regexp = new RegExp(key);
|
|
18
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
19
|
-
o.push([
|
|
20
|
-
regexp,
|
|
21
|
-
aliasPath
|
|
22
|
-
]);
|
|
23
|
-
} else {
|
|
24
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
25
|
-
o.push([
|
|
26
|
-
key,
|
|
27
|
-
aliasPath
|
|
28
|
-
]);
|
|
29
|
-
}
|
|
30
|
-
return o;
|
|
31
|
-
}, []);
|
|
32
|
-
const cacheMap = /* @__PURE__ */ new Map();
|
|
33
|
-
return (requestedModule) => {
|
|
34
|
-
if (cacheMap.has(requestedModule)) {
|
|
35
|
-
return cacheMap.get(requestedModule);
|
|
36
|
-
}
|
|
37
|
-
for (const [key, value] of aliasPairs) {
|
|
38
|
-
if (key instanceof RegExp) {
|
|
39
|
-
if (key.test(requestedModule)) {
|
|
40
|
-
cacheMap.set(requestedModule, value);
|
|
41
|
-
return value;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (requestedModule === key) {
|
|
45
|
-
cacheMap.set(requestedModule, value);
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
const isDynamicImport = (tsBinary, node) => {
|
|
52
|
-
return tsBinary.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
53
|
-
};
|
|
54
|
-
function tsconfigPathsBeforeHookFactory(tsBinary, baseUrl, paths) {
|
|
55
|
-
const tsPaths = {};
|
|
56
|
-
const alias = {};
|
|
57
|
-
Object.keys(paths).forEach((key) => {
|
|
58
|
-
if (Array.isArray(paths[key])) {
|
|
59
|
-
tsPaths[key] = paths[key];
|
|
60
|
-
} else {
|
|
61
|
-
alias[key] = paths[key];
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
65
|
-
const matchTsPath = createMatchPath(baseUrl, tsPaths, [
|
|
66
|
-
"main"
|
|
67
|
-
]);
|
|
68
|
-
const matchPath = (requestedModule, readJSONSync, fileExists, extensions) => {
|
|
69
|
-
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
70
|
-
if (result) {
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
return matchAliasPath(requestedModule);
|
|
74
|
-
};
|
|
75
|
-
if (Object.keys(paths).length === 0) {
|
|
76
|
-
return void 0;
|
|
77
|
-
}
|
|
78
|
-
return (ctx) => {
|
|
79
|
-
return (sf) => {
|
|
80
|
-
const visitNode = (node) => {
|
|
81
|
-
if (isDynamicImport(tsBinary, node)) {
|
|
82
|
-
const importPathWithQuotes = node.arguments[0].getText(sf);
|
|
83
|
-
const text = importPathWithQuotes.slice(1, importPathWithQuotes.length - 1);
|
|
84
|
-
const result = getNotAliasedPath(sf, matchPath, text);
|
|
85
|
-
if (!result) {
|
|
86
|
-
return node;
|
|
87
|
-
}
|
|
88
|
-
return tsBinary.factory.updateCallExpression(node, node.expression, node.typeArguments, tsBinary.factory.createNodeArray([
|
|
89
|
-
tsBinary.factory.createStringLiteral(result)
|
|
90
|
-
]));
|
|
91
|
-
}
|
|
92
|
-
if (tsBinary.isImportDeclaration(node) || tsBinary.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
93
|
-
try {
|
|
94
|
-
var _node_moduleSpecifier;
|
|
95
|
-
const importPathWithQuotes = node === null || node === void 0 ? void 0 : (_node_moduleSpecifier = node.moduleSpecifier) === null || _node_moduleSpecifier === void 0 ? void 0 : _node_moduleSpecifier.getText();
|
|
96
|
-
if (!importPathWithQuotes) {
|
|
97
|
-
return node;
|
|
98
|
-
}
|
|
99
|
-
const text = importPathWithQuotes.substring(1, importPathWithQuotes.length - 1);
|
|
100
|
-
const result = getNotAliasedPath(sf, matchPath, text);
|
|
101
|
-
if (!result) {
|
|
102
|
-
return node;
|
|
103
|
-
}
|
|
104
|
-
const moduleSpecifier = tsBinary.factory.createStringLiteral(result);
|
|
105
|
-
moduleSpecifier.parent = node.moduleSpecifier.parent;
|
|
106
|
-
let newNode;
|
|
107
|
-
if (tsBinary.isImportDeclaration(node)) {
|
|
108
|
-
newNode = tsBinary.factory.updateImportDeclaration(node, node.modifiers, node.importClause, moduleSpecifier, node.assertClause);
|
|
109
|
-
} else {
|
|
110
|
-
newNode = tsBinary.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, moduleSpecifier, node.assertClause);
|
|
111
|
-
}
|
|
112
|
-
newNode.flags = node.flags;
|
|
113
|
-
return newNode;
|
|
114
|
-
} catch {
|
|
115
|
-
return node;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return tsBinary.visitEachChild(node, visitNode, ctx);
|
|
119
|
-
};
|
|
120
|
-
return tsBinary.visitNode(sf, visitNode);
|
|
121
|
-
};
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
function getNotAliasedPath(sf, matcher, text) {
|
|
125
|
-
let result = matcher(text, void 0, void 0, [
|
|
126
|
-
".ts",
|
|
127
|
-
".tsx",
|
|
128
|
-
".js",
|
|
129
|
-
".jsx"
|
|
130
|
-
]);
|
|
131
|
-
if (!result) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (os.platform() === "win32") {
|
|
135
|
-
result = result.replace(/\\/g, "/");
|
|
136
|
-
}
|
|
137
|
-
if (!path.isAbsolute(result)) {
|
|
138
|
-
if (!result.startsWith(".") && !result.startsWith("..")) {
|
|
139
|
-
try {
|
|
140
|
-
const packagePath = require.resolve(result, {
|
|
141
|
-
paths: [
|
|
142
|
-
process.cwd(),
|
|
143
|
-
...module.paths
|
|
144
|
-
]
|
|
145
|
-
});
|
|
146
|
-
if (packagePath) {
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
} catch {
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
try {
|
|
153
|
-
const packagePath = require.resolve(text, {
|
|
154
|
-
paths: [
|
|
155
|
-
process.cwd(),
|
|
156
|
-
...module.paths
|
|
157
|
-
]
|
|
158
|
-
});
|
|
159
|
-
if (packagePath) {
|
|
160
|
-
return text;
|
|
161
|
-
}
|
|
162
|
-
} catch {
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
const resolvedPath = posix.relative(dirname(sf.fileName), result) || "./";
|
|
166
|
-
return resolvedPath[0] === "." ? resolvedPath : `./${resolvedPath}`;
|
|
167
|
-
}
|
|
168
|
-
export {
|
|
169
|
-
tsconfigPathsBeforeHookFactory
|
|
170
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
class TypescriptLoader {
|
|
2
|
-
load() {
|
|
3
|
-
if (this.tsBinary) {
|
|
4
|
-
return this.tsBinary;
|
|
5
|
-
}
|
|
6
|
-
try {
|
|
7
|
-
const tsPath = require.resolve("typescript", {
|
|
8
|
-
paths: [
|
|
9
|
-
this.appDirectory || process.cwd()
|
|
10
|
-
]
|
|
11
|
-
});
|
|
12
|
-
const ts = require(tsPath);
|
|
13
|
-
return ts;
|
|
14
|
-
} catch (error) {
|
|
15
|
-
throw new Error('TypeScript could not be found! Please, install "typescript" package.');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
constructor({ appDirectory }) {
|
|
19
|
-
this.appDirectory = appDirectory;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
export {
|
|
23
|
-
TypescriptLoader
|
|
24
|
-
};
|
|
File without changes
|
|
File without changes
|