@bleedingdev/modern-js-server-utils 3.9.0-ultramodern.2 → 3.9.0-ultramodern.21
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 +15 -3
- package/dist/cjs/compilers/rslib.js +305 -0
- package/dist/cjs/compilers/tsgo.js +192 -0
- package/dist/esm/common/index.mjs +15 -3
- package/dist/esm/compilers/rslib.mjs +257 -0
- package/dist/esm/compilers/tsgo.mjs +135 -0
- package/dist/esm-node/common/index.mjs +15 -3
- package/dist/esm-node/compilers/rslib.mjs +258 -0
- package/dist/esm-node/compilers/tsgo.mjs +140 -0
- package/dist/types/common/index.d.ts +2 -3
- package/dist/types/compilers/rslib.d.ts +10 -0
- package/dist/types/compilers/tsgo.d.ts +49 -0
- package/package.json +6 -7
- package/dist/cjs/compilers/typescript/importRewriter.js +0 -175
- package/dist/cjs/compilers/typescript/index.js +0 -314
- package/dist/cjs/compilers/typescript/tsconfigPathsPlugin.js +0 -194
- package/dist/esm/compilers/typescript/importRewriter.mjs +0 -134
- package/dist/esm/compilers/typescript/index.mjs +0 -257
- package/dist/esm/compilers/typescript/tsconfigPathsPlugin.mjs +0 -143
- package/dist/esm-node/compilers/typescript/importRewriter.mjs +0 -135
- package/dist/esm-node/compilers/typescript/index.mjs +0 -262
- package/dist/esm-node/compilers/typescript/tsconfigPathsPlugin.mjs +0 -145
- package/dist/types/compilers/typescript/importRewriter.d.ts +0 -8
- package/dist/types/compilers/typescript/index.d.ts +0 -35
- package/dist/types/compilers/typescript/tsconfigPathsPlugin.d.ts +0 -3
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import __rslib_shim_module__ from "node:module";
|
|
2
|
-
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import { fs, getAliasConfig, logger } from "@modern-js/utils";
|
|
4
|
-
import { spawn } from "child_process";
|
|
5
|
-
import path from "path";
|
|
6
|
-
import { assertNoNativeModuleOutputCollision, rewriteImportSpecifiers } from "./importRewriter.mjs";
|
|
7
|
-
import { createTsconfigPathsMatcher, getNotAliasedPath } from "./tsconfigPathsPlugin.mjs";
|
|
8
|
-
import { fileURLToPath as __rspack_fileURLToPath } from "node:url";
|
|
9
|
-
import { dirname as __rspack_dirname } from "node:path";
|
|
10
|
-
var __rspack_import_meta_dirname__ = __rspack_dirname(__rspack_fileURLToPath(import.meta.url));
|
|
11
|
-
const copyFiles = async (from, to, appDirectory)=>{
|
|
12
|
-
if (await fs.pathExists(from)) {
|
|
13
|
-
const relativePath = path.relative(appDirectory, from);
|
|
14
|
-
const targetDir = path.join(to, relativePath);
|
|
15
|
-
await fs.copy(from, targetDir, {
|
|
16
|
-
filter: (src)=>!/\.(?:[cm]?ts|tsx|jsx?)$/u.test(src) && !src.endsWith('tsconfig.json')
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
let resolvedConfigCount = 0;
|
|
21
|
-
const createResolvedTsgoConfig = async (appDirectory, tsconfigPath, distDir, sourceDirs, moduleType, tsgoBinPath)=>{
|
|
22
|
-
const tsconfigDir = path.dirname(tsconfigPath);
|
|
23
|
-
const output = await runTsgo(tsgoBinPath, [
|
|
24
|
-
'--showConfig',
|
|
25
|
-
'-p',
|
|
26
|
-
tsconfigPath
|
|
27
|
-
], {
|
|
28
|
-
cwd: tsconfigDir
|
|
29
|
-
});
|
|
30
|
-
const config = JSON.parse(output.stdout);
|
|
31
|
-
config.compilerOptions ??= {};
|
|
32
|
-
config.compilerOptions.rootDir = appDirectory;
|
|
33
|
-
config.compilerOptions.outDir = distDir;
|
|
34
|
-
config.compilerOptions.composite = false;
|
|
35
|
-
config.compilerOptions.declarationMap = false;
|
|
36
|
-
config.compilerOptions.emitDeclarationOnly = false;
|
|
37
|
-
config.compilerOptions.incremental = false;
|
|
38
|
-
config.compilerOptions.noEmit = false;
|
|
39
|
-
if (void 0 === config.compilerOptions.jsx || 'preserve' === config.compilerOptions.jsx) config.compilerOptions.jsx = 'react-jsx';
|
|
40
|
-
delete config.compilerOptions.tsBuildInfoFile;
|
|
41
|
-
if (true === config.compilerOptions.allowImportingTsExtensions) config.compilerOptions.rewriteRelativeImportExtensions = true;
|
|
42
|
-
config.files = filterSourceFiles(tsconfigDir, sourceDirs, config.files);
|
|
43
|
-
delete config.include;
|
|
44
|
-
delete config.references;
|
|
45
|
-
delete config.compilerOptions.baseUrl;
|
|
46
|
-
if ([
|
|
47
|
-
'node',
|
|
48
|
-
'node10'
|
|
49
|
-
].includes(String(config.compilerOptions.moduleResolution).toLowerCase())) delete config.compilerOptions.moduleResolution;
|
|
50
|
-
if ('module' !== moduleType) {
|
|
51
|
-
if ([
|
|
52
|
-
'preserve',
|
|
53
|
-
'esnext',
|
|
54
|
-
'es2015',
|
|
55
|
-
'es2020',
|
|
56
|
-
'es2022',
|
|
57
|
-
'es6'
|
|
58
|
-
].includes(String(config.compilerOptions.module).toLowerCase())) {
|
|
59
|
-
config.compilerOptions.module = 'commonjs';
|
|
60
|
-
delete config.compilerOptions.moduleResolution;
|
|
61
|
-
}
|
|
62
|
-
config.compilerOptions.verbatimModuleSyntax = false;
|
|
63
|
-
}
|
|
64
|
-
const resolvedConfigPath = path.join(tsconfigDir, `.tsgo.${process.pid}.${resolvedConfigCount++}.resolved.json`);
|
|
65
|
-
await fs.writeFile(resolvedConfigPath, JSON.stringify(config, null, 2));
|
|
66
|
-
return {
|
|
67
|
-
config,
|
|
68
|
-
resolvedConfigPath
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
const filterSourceFiles = (tsconfigDir, sourceDirs, files = [])=>{
|
|
72
|
-
const sourcePosixPaths = sourceDirs.map((sourceDir)=>sourceDir.split(path.sep).join(path.posix.sep));
|
|
73
|
-
return files.filter((fileName)=>{
|
|
74
|
-
const absoluteFileName = path.resolve(tsconfigDir, fileName).split(path.sep).join(path.posix.sep);
|
|
75
|
-
if (isAppRouterGeneratedDeclaration(absoluteFileName)) return false;
|
|
76
|
-
return fileName.endsWith('.d.ts') || sourcePosixPaths.some((sourceDir)=>absoluteFileName.includes(sourceDir));
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
const isAppRouterGeneratedDeclaration = (absoluteFileName)=>/\/src\/modern-tanstack\/.*\.gen\.d\.ts$/u.test(absoluteFileName);
|
|
80
|
-
const runTsgo = (tsgoBinPath, args, options)=>new Promise((resolve, reject)=>{
|
|
81
|
-
const child = spawn(process.execPath, [
|
|
82
|
-
tsgoBinPath,
|
|
83
|
-
...args
|
|
84
|
-
], {
|
|
85
|
-
cwd: options.cwd,
|
|
86
|
-
stdio: [
|
|
87
|
-
'ignore',
|
|
88
|
-
'pipe',
|
|
89
|
-
'pipe'
|
|
90
|
-
]
|
|
91
|
-
});
|
|
92
|
-
let stdout = '';
|
|
93
|
-
let stderr = '';
|
|
94
|
-
child.stdout.on('data', (data)=>{
|
|
95
|
-
stdout += data;
|
|
96
|
-
});
|
|
97
|
-
child.stderr.on('data', (data)=>{
|
|
98
|
-
stderr += data;
|
|
99
|
-
});
|
|
100
|
-
child.on('error', reject);
|
|
101
|
-
child.on('close', (code)=>{
|
|
102
|
-
const result = {
|
|
103
|
-
stdout,
|
|
104
|
-
stderr,
|
|
105
|
-
code: code ?? 1
|
|
106
|
-
};
|
|
107
|
-
if (false !== options.reject && 0 !== result.code) return void reject(new Error(stderr || stdout || `tsgo exited with ${result.code}`));
|
|
108
|
-
resolve(result);
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
const getTsgoBinEntry = (pkg)=>{
|
|
112
|
-
if ('string' == typeof pkg.bin) return pkg.bin;
|
|
113
|
-
return pkg.bin?.tsc ?? pkg.bin?.tsgo;
|
|
114
|
-
};
|
|
115
|
-
const resolveTsgoBinPath = (pkgPath)=>{
|
|
116
|
-
const pkgDir = path.dirname(pkgPath);
|
|
117
|
-
const pkg = require(pkgPath);
|
|
118
|
-
const declaredBinEntry = getTsgoBinEntry(pkg);
|
|
119
|
-
const candidates = [
|
|
120
|
-
declaredBinEntry ? path.resolve(pkgDir, declaredBinEntry) : void 0,
|
|
121
|
-
path.join(pkgDir, 'bin/tsgo.js')
|
|
122
|
-
].filter((candidate)=>Boolean(candidate));
|
|
123
|
-
return candidates.find((candidate)=>fs.existsSync(candidate)) ?? candidates[0];
|
|
124
|
-
};
|
|
125
|
-
const getTsgoBinPath = (appDirectory, resolvePaths = [
|
|
126
|
-
appDirectory,
|
|
127
|
-
__rspack_import_meta_dirname__
|
|
128
|
-
])=>{
|
|
129
|
-
try {
|
|
130
|
-
let pkgPath;
|
|
131
|
-
try {
|
|
132
|
-
pkgPath = require.resolve("@typescript/native/package.json", {
|
|
133
|
-
paths: resolvePaths
|
|
134
|
-
});
|
|
135
|
-
} catch {
|
|
136
|
-
pkgPath = require.resolve("@typescript/native-preview/package.json", {
|
|
137
|
-
paths: resolvePaths
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
return resolveTsgoBinPath(pkgPath);
|
|
141
|
-
} catch {
|
|
142
|
-
throw new Error('tsgo could not be found! Please install "@typescript/native" (or legacy "@typescript/native-preview") in your project to compile BFF/server code.');
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
|
-
const OUTPUT_SOURCE_EXTENSIONS = {
|
|
146
|
-
'.js': [
|
|
147
|
-
'.ts',
|
|
148
|
-
'.tsx',
|
|
149
|
-
'.js',
|
|
150
|
-
'.jsx'
|
|
151
|
-
],
|
|
152
|
-
'.mjs': [
|
|
153
|
-
'.mts',
|
|
154
|
-
'.mjs'
|
|
155
|
-
],
|
|
156
|
-
'.cjs': [
|
|
157
|
-
'.cts',
|
|
158
|
-
'.cjs'
|
|
159
|
-
],
|
|
160
|
-
'.d.ts': [
|
|
161
|
-
'.ts',
|
|
162
|
-
'.tsx',
|
|
163
|
-
'.js',
|
|
164
|
-
'.jsx'
|
|
165
|
-
],
|
|
166
|
-
'.d.mts': [
|
|
167
|
-
'.mts',
|
|
168
|
-
'.mjs'
|
|
169
|
-
],
|
|
170
|
-
'.d.cts': [
|
|
171
|
-
'.cts',
|
|
172
|
-
'.cjs'
|
|
173
|
-
]
|
|
174
|
-
};
|
|
175
|
-
const getSourceFileForOutput = (appDirectory, distDir, outputFile)=>{
|
|
176
|
-
const relativeOutput = path.relative(distDir, outputFile);
|
|
177
|
-
const parsed = path.parse(relativeOutput);
|
|
178
|
-
const isDeclaration = parsed.name.endsWith('.d');
|
|
179
|
-
const outputExtension = isDeclaration ? `.d${parsed.ext}` : parsed.ext;
|
|
180
|
-
const sourceName = isDeclaration ? parsed.name.slice(0, -2) : parsed.name;
|
|
181
|
-
const sourceBase = path.join(appDirectory, parsed.dir, sourceName);
|
|
182
|
-
const extensions = OUTPUT_SOURCE_EXTENSIONS[outputExtension] ?? OUTPUT_SOURCE_EXTENSIONS['.js'];
|
|
183
|
-
for (const extension of extensions){
|
|
184
|
-
const candidate = `${sourceBase}${extension}`;
|
|
185
|
-
if (fs.existsSync(candidate)) return candidate;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
const sourceMappingUrlRE = /^\/\/[#@] sourceMappingURL=.*$/gm;
|
|
189
|
-
const dropStaleSourceMap = async (outputFile, content)=>{
|
|
190
|
-
const mapFile = `${outputFile}.map`;
|
|
191
|
-
if (await fs.pathExists(mapFile)) await fs.remove(mapFile);
|
|
192
|
-
return content.replace(sourceMappingUrlRE, '');
|
|
193
|
-
};
|
|
194
|
-
const rewriteOutputSpecifiers = async (appDirectory, distDir, baseUrl, paths, moduleType)=>{
|
|
195
|
-
if (!await fs.pathExists(distDir)) return;
|
|
196
|
-
const matcher = createTsconfigPathsMatcher(baseUrl, paths);
|
|
197
|
-
if (!matcher) return;
|
|
198
|
-
const files = await collectOutputFiles(distDir);
|
|
199
|
-
await Promise.all(files.map(async (file)=>{
|
|
200
|
-
const sourceFile = getSourceFileForOutput(appDirectory, distDir, file);
|
|
201
|
-
if (!sourceFile) return;
|
|
202
|
-
assertNoNativeModuleOutputCollision(sourceFile, fs.existsSync);
|
|
203
|
-
const content = await fs.readFile(file, 'utf8');
|
|
204
|
-
const { content: rewritten, changed } = rewriteImportSpecifiers(content, (specifier)=>getNotAliasedPath(sourceFile, matcher, specifier, moduleType));
|
|
205
|
-
if (changed) await fs.writeFile(file, await dropStaleSourceMap(file, rewritten));
|
|
206
|
-
}));
|
|
207
|
-
};
|
|
208
|
-
const collectOutputFiles = async (dir)=>{
|
|
209
|
-
const entries = await fs.readdir(dir, {
|
|
210
|
-
withFileTypes: true
|
|
211
|
-
});
|
|
212
|
-
const files = await Promise.all(entries.map(async (entry)=>{
|
|
213
|
-
const fullPath = path.join(dir, entry.name);
|
|
214
|
-
if (entry.isDirectory()) return collectOutputFiles(fullPath);
|
|
215
|
-
return /\.(?:c|m)?js$|\.d\.(?:c|m)?ts$/.test(entry.name) ? [
|
|
216
|
-
fullPath
|
|
217
|
-
] : [];
|
|
218
|
-
}));
|
|
219
|
-
return files.flat();
|
|
220
|
-
};
|
|
221
|
-
const compileByTs = async (appDirectory, config, compileOptions)=>{
|
|
222
|
-
logger.info("Running tsgo compile...");
|
|
223
|
-
const { sourceDirs, distDir, tsconfigPath } = compileOptions;
|
|
224
|
-
if (!tsconfigPath) return;
|
|
225
|
-
const { alias } = config;
|
|
226
|
-
const aliasOption = getAliasConfig(alias, {
|
|
227
|
-
appDirectory,
|
|
228
|
-
tsconfigPath
|
|
229
|
-
});
|
|
230
|
-
const { paths = {}, absoluteBaseUrl = './' } = aliasOption;
|
|
231
|
-
const tsgoBinPath = getTsgoBinPath(appDirectory);
|
|
232
|
-
const { config: tsgoConfig, resolvedConfigPath } = await createResolvedTsgoConfig(appDirectory, tsconfigPath, distDir, sourceDirs, compileOptions.moduleType, tsgoBinPath);
|
|
233
|
-
let result;
|
|
234
|
-
try {
|
|
235
|
-
result = await runTsgo(tsgoBinPath, [
|
|
236
|
-
'-p',
|
|
237
|
-
resolvedConfigPath
|
|
238
|
-
], {
|
|
239
|
-
cwd: appDirectory,
|
|
240
|
-
reject: false
|
|
241
|
-
});
|
|
242
|
-
} finally{
|
|
243
|
-
await fs.remove(resolvedConfigPath);
|
|
244
|
-
}
|
|
245
|
-
if (result.stderr) logger.error(result.stderr);
|
|
246
|
-
if (result.stdout) logger.info(result.stdout);
|
|
247
|
-
await rewriteOutputSpecifiers(appDirectory, distDir, absoluteBaseUrl, paths, compileOptions.moduleType);
|
|
248
|
-
if (0 !== result.code) {
|
|
249
|
-
const noEmitOnError = tsgoConfig.compilerOptions?.noEmitOnError;
|
|
250
|
-
if (void 0 === noEmitOnError || true === noEmitOnError) if (compileOptions.throwErrorInsteadOfExit) {
|
|
251
|
-
logger.error('TS-Go compilation failed');
|
|
252
|
-
throw new Error([
|
|
253
|
-
`TS-Go compilation failed with exit code ${result.code}.`,
|
|
254
|
-
result.stderr.trim() || result.stdout.trim()
|
|
255
|
-
].filter(Boolean).join('\n'));
|
|
256
|
-
} else process.exit(1);
|
|
257
|
-
}
|
|
258
|
-
for (const source of sourceDirs)await copyFiles(source, distDir, appDirectory);
|
|
259
|
-
await rewriteOutputSpecifiers(appDirectory, distDir, absoluteBaseUrl, paths, compileOptions.moduleType);
|
|
260
|
-
logger.info("TS-Go compile succeed");
|
|
261
|
-
};
|
|
262
|
-
export { compileByTs, createResolvedTsgoConfig, getTsgoBinPath, rewriteOutputSpecifiers };
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import __rslib_shim_module__ from "node:module";
|
|
2
|
-
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(/*#__PURE__*/ (()=>import.meta.url)());
|
|
3
|
-
import { findMatchedSourcePath, findSourceEntry } from "@modern-js/utils";
|
|
4
|
-
import { createMatchPath } from "@modern-js/utils/tsconfig-paths";
|
|
5
|
-
import path from "path";
|
|
6
|
-
const windowsAbsolutePathRE = /^[A-Za-z]:[\\/]/u;
|
|
7
|
-
const isAbsolutePath = (input)=>path.isAbsolute(input) || path.win32.isAbsolute(input);
|
|
8
|
-
const pathApiFor = (...inputs)=>inputs.some((input)=>windowsAbsolutePathRE.test(input)) ? path.win32 : path;
|
|
9
|
-
const toImportSpecifier = (sourceFile, resolvedPath)=>{
|
|
10
|
-
const pathApi = pathApiFor(sourceFile, resolvedPath);
|
|
11
|
-
const relativePath = pathApi.relative(pathApi.dirname(sourceFile), resolvedPath).split(pathApi.sep).join(path.posix.sep);
|
|
12
|
-
return '.' === relativePath[0] ? relativePath : `./${relativePath}`;
|
|
13
|
-
};
|
|
14
|
-
const SOURCE_TO_OUTPUT_EXTENSION = new Map([
|
|
15
|
-
[
|
|
16
|
-
'.ts',
|
|
17
|
-
'.js'
|
|
18
|
-
],
|
|
19
|
-
[
|
|
20
|
-
'.tsx',
|
|
21
|
-
'.js'
|
|
22
|
-
],
|
|
23
|
-
[
|
|
24
|
-
'.js',
|
|
25
|
-
'.js'
|
|
26
|
-
],
|
|
27
|
-
[
|
|
28
|
-
'.jsx',
|
|
29
|
-
'.js'
|
|
30
|
-
],
|
|
31
|
-
[
|
|
32
|
-
'.mts',
|
|
33
|
-
'.mjs'
|
|
34
|
-
],
|
|
35
|
-
[
|
|
36
|
-
'.mjs',
|
|
37
|
-
'.mjs'
|
|
38
|
-
],
|
|
39
|
-
[
|
|
40
|
-
'.cts',
|
|
41
|
-
'.cjs'
|
|
42
|
-
],
|
|
43
|
-
[
|
|
44
|
-
'.cjs',
|
|
45
|
-
'.cjs'
|
|
46
|
-
]
|
|
47
|
-
]);
|
|
48
|
-
const toEsmOutputPath = (resolvedPath)=>{
|
|
49
|
-
const sourcePath = (findSourceEntry(resolvedPath) || resolvedPath).replace(/\\/g, '/');
|
|
50
|
-
const ext = path.extname(sourcePath);
|
|
51
|
-
if (!ext) return `${sourcePath}.js`;
|
|
52
|
-
const outputExtension = SOURCE_TO_OUTPUT_EXTENSION.get(ext);
|
|
53
|
-
if (!outputExtension) return sourcePath;
|
|
54
|
-
return `${sourcePath.slice(0, -ext.length)}${outputExtension}`;
|
|
55
|
-
};
|
|
56
|
-
const resolveRelativeEsmSpecifier = (sourceFile, text)=>{
|
|
57
|
-
if (!text.startsWith('./') && !text.startsWith('../')) return;
|
|
58
|
-
const pathApi = pathApiFor(sourceFile);
|
|
59
|
-
return pathApi.resolve(pathApi.dirname(sourceFile), text);
|
|
60
|
-
};
|
|
61
|
-
const isRegExpKey = (str)=>str.startsWith('^') || str.endsWith('$');
|
|
62
|
-
const resolveAliasPath = (baseUrl, filePath)=>{
|
|
63
|
-
if (filePath.startsWith('.') || filePath.startsWith('..')) return path.resolve(baseUrl, filePath);
|
|
64
|
-
return filePath;
|
|
65
|
-
};
|
|
66
|
-
const createAliasMatcher = (baseUrl, alias)=>{
|
|
67
|
-
const aliasPairs = Object.keys(alias).reduce((o, key)=>{
|
|
68
|
-
if (isRegExpKey(key)) {
|
|
69
|
-
const regexp = new RegExp(key);
|
|
70
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
71
|
-
o.push([
|
|
72
|
-
regexp,
|
|
73
|
-
aliasPath
|
|
74
|
-
]);
|
|
75
|
-
} else {
|
|
76
|
-
const aliasPath = resolveAliasPath(baseUrl, alias[key]);
|
|
77
|
-
o.push([
|
|
78
|
-
key,
|
|
79
|
-
aliasPath
|
|
80
|
-
]);
|
|
81
|
-
}
|
|
82
|
-
return o;
|
|
83
|
-
}, []);
|
|
84
|
-
const cacheMap = new Map();
|
|
85
|
-
return (requestedModule)=>{
|
|
86
|
-
if (cacheMap.has(requestedModule)) return cacheMap.get(requestedModule);
|
|
87
|
-
for (const [key, value] of aliasPairs){
|
|
88
|
-
if (key instanceof RegExp) {
|
|
89
|
-
if (key.test(requestedModule)) {
|
|
90
|
-
cacheMap.set(requestedModule, value);
|
|
91
|
-
return value;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (requestedModule === key) {
|
|
95
|
-
cacheMap.set(requestedModule, value);
|
|
96
|
-
return value;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
};
|
|
101
|
-
const createTsconfigPathsMatcher = (baseUrl, paths)=>{
|
|
102
|
-
const tsPaths = {};
|
|
103
|
-
const alias = {};
|
|
104
|
-
Object.keys(paths).forEach((key)=>{
|
|
105
|
-
if (Array.isArray(paths[key])) tsPaths[key] = paths[key];
|
|
106
|
-
else alias[key] = paths[key];
|
|
107
|
-
});
|
|
108
|
-
const matchAliasPath = createAliasMatcher(baseUrl, alias);
|
|
109
|
-
const matchTsPath = createMatchPath(baseUrl, tsPaths, [
|
|
110
|
-
'main'
|
|
111
|
-
]);
|
|
112
|
-
return (requestedModule, readJSONSync, fileExists, extensions)=>{
|
|
113
|
-
const result = matchTsPath(requestedModule, readJSONSync, fileExists, extensions);
|
|
114
|
-
if (result) return result;
|
|
115
|
-
return matchAliasPath(requestedModule);
|
|
116
|
-
};
|
|
117
|
-
};
|
|
118
|
-
function getNotAliasedPath(sourceFile, matcher, text, moduleType) {
|
|
119
|
-
let result = findMatchedSourcePath(matcher, text);
|
|
120
|
-
if (!result && 'module' === moduleType) result = resolveRelativeEsmSpecifier(sourceFile, text);
|
|
121
|
-
if (!result) return;
|
|
122
|
-
if (!isAbsolutePath(result)) {
|
|
123
|
-
if (!result.startsWith('.') && !result.startsWith('..')) try {
|
|
124
|
-
const packagePath = require.resolve(result, {
|
|
125
|
-
paths: [
|
|
126
|
-
process.cwd(),
|
|
127
|
-
...module.paths
|
|
128
|
-
]
|
|
129
|
-
});
|
|
130
|
-
if (packagePath) return result;
|
|
131
|
-
} catch {}
|
|
132
|
-
try {
|
|
133
|
-
const packagePath = require.resolve(text, {
|
|
134
|
-
paths: [
|
|
135
|
-
process.cwd(),
|
|
136
|
-
...module.paths
|
|
137
|
-
]
|
|
138
|
-
});
|
|
139
|
-
if (packagePath) return text;
|
|
140
|
-
} catch {}
|
|
141
|
-
}
|
|
142
|
-
if ('module' === moduleType || /\.[cm]ts$/iu.test(result)) result = toEsmOutputPath(result);
|
|
143
|
-
return toImportSpecifier(sourceFile, result) || './';
|
|
144
|
-
}
|
|
145
|
-
export { createTsconfigPathsMatcher, getNotAliasedPath };
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
type SpecifierRewrite = (specifier: string) => string | undefined;
|
|
2
|
-
export declare const assertNoNativeModuleOutputCollision: (sourceFile: string, fileExists: (file: string) => boolean) => void;
|
|
3
|
-
/** Rewrite static module specifiers without touching runtime data or syntax. */
|
|
4
|
-
export declare const rewriteImportSpecifiers: (content: string, rewrite: SpecifierRewrite) => {
|
|
5
|
-
content: string;
|
|
6
|
-
changed: boolean;
|
|
7
|
-
};
|
|
8
|
-
export {};
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import type { CompileFunc } from '../../common/index.js';
|
|
2
|
-
type TsgoConfig = {
|
|
3
|
-
compilerOptions?: {
|
|
4
|
-
allowImportingTsExtensions?: boolean;
|
|
5
|
-
baseUrl?: unknown;
|
|
6
|
-
composite?: boolean;
|
|
7
|
-
declaration?: boolean;
|
|
8
|
-
declarationMap?: boolean;
|
|
9
|
-
emitDeclarationOnly?: boolean;
|
|
10
|
-
incremental?: boolean;
|
|
11
|
-
jsx?: string;
|
|
12
|
-
module?: string;
|
|
13
|
-
moduleResolution?: string;
|
|
14
|
-
noEmit?: boolean;
|
|
15
|
-
noEmitOnError?: boolean;
|
|
16
|
-
outDir?: string;
|
|
17
|
-
rootDir?: string;
|
|
18
|
-
rewriteRelativeImportExtensions?: boolean;
|
|
19
|
-
tsBuildInfoFile?: string;
|
|
20
|
-
verbatimModuleSyntax?: boolean;
|
|
21
|
-
};
|
|
22
|
-
files?: string[];
|
|
23
|
-
include?: string[];
|
|
24
|
-
references?: Array<{
|
|
25
|
-
path: string;
|
|
26
|
-
}>;
|
|
27
|
-
};
|
|
28
|
-
export declare const createResolvedTsgoConfig: (appDirectory: string, tsconfigPath: string, distDir: string, sourceDirs: string[], moduleType: 'module' | 'commonjs' | undefined, tsgoBinPath: string) => Promise<{
|
|
29
|
-
config: TsgoConfig;
|
|
30
|
-
resolvedConfigPath: string;
|
|
31
|
-
}>;
|
|
32
|
-
export declare const getTsgoBinPath: (appDirectory: string, resolvePaths?: string[]) => string;
|
|
33
|
-
export declare const rewriteOutputSpecifiers: (appDirectory: string, distDir: string, baseUrl: string, paths: Record<string, string[] | string>, moduleType?: 'module' | 'commonjs') => Promise<void>;
|
|
34
|
-
export declare const compileByTs: CompileFunc;
|
|
35
|
-
export {};
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { MatchPath } from '@modern-js/utils/tsconfig-paths';
|
|
2
|
-
export declare const createTsconfigPathsMatcher: (baseUrl: string, paths: Record<string, string[] | string>) => MatchPath | undefined;
|
|
3
|
-
export declare function getNotAliasedPath(sourceFile: string, matcher: MatchPath, text: string, moduleType?: 'module' | 'commonjs'): string | undefined;
|