-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrspack-tsconfig-paths.js
More file actions
70 lines (62 loc) · 2.26 KB
/
Copy pathrspack-tsconfig-paths.js
File metadata and controls
70 lines (62 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Helper function to resolve TypeScript paths for Rspack
* Reads tsconfig.base.json and converts path mappings to webpack-like aliases
*/
const fs = require('fs');
const path = require('path');
function getTsconfigPaths(tsconfigPath, baseDir) {
try {
// Read the tsconfig file
const content = fs.readFileSync(tsconfigPath, 'utf-8');
// Remove comments and parse JSON
const jsonContent = content
.replace(/\/\*[\s\S]*?\*\//g, '') // Remove block comments
.replace(/\/\/.*/g, ''); // Remove line comments
const tsconfig = JSON.parse(jsonContent);
const paths = tsconfig.compilerOptions?.paths || {};
const aliases = {};
// Convert TypeScript paths to webpack aliases
for (const [pattern, targets] of Object.entries(paths)) {
if (targets.length > 0) {
// Remove /* from pattern and get the actual alias
const alias = pattern.replace(/\/\*$/, '');
// Get the first target path
const targetPath = targets[0].replace(/\/\*$/, '');
// Resolve relative to the base directory
const resolvedPath = path.resolve(baseDir, targetPath);
aliases[alias] = resolvedPath;
}
}
return aliases;
} catch (error) {
console.warn(`Failed to parse tsconfig paths: ${error.message}`);
return {};
}
}
/**
* Conditionally wraps an Rspack config with Zephyr Cloud deployment.
* Only activates for production builds (NODE_ENV=production) when ZE_SECRET_TOKEN is set.
* Skipped during dev serve since the explicit MF dev-server executor cannot pass env vars.
*/
function maybeWithZephyr(config) {
// Silence source-map-loader warnings from @module-federation packages,
// whose published builds reference .ts source files that are not shipped.
const ignoreWarnings = [
...(config.ignoreWarnings || []),
{
module: /@module-federation[\\/]/,
message: /Failed to parse source map/,
},
];
const configWithIgnores = { ...config, ignoreWarnings };
if (
process.env.ZE_SECRET_TOKEN &&
!process.env.SKIP_ZEPHYR &&
process.env.NODE_ENV === 'production'
) {
const { withZephyr } = require('zephyr-rspack-plugin');
return withZephyr()(configWithIgnores);
}
return configWithIgnores;
}
module.exports = { getTsconfigPaths, maybeWithZephyr };