Skip to content

Commit 3255228

Browse files
committed
fix: add resolve-aliases script in prepack process
1 parent 19a4db2 commit 3255228

3 files changed

Lines changed: 99 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
"description": "Material design for React Native",
55
"main": "lib/commonjs/index.js",
66
"module": "lib/module/index.js",
7-
"react-native": "src/index.tsx",
7+
"react-native": "lib/module/index.js",
88
"source": "src/index.tsx",
99
"types": "lib/typescript/index.d.ts",
1010
"files": [
11-
"src",
1211
"lib",
1312
"react-navigation",
1413
"babel.js"
@@ -42,7 +41,7 @@
4241
"lint": "yarn lint-no-fix --fix",
4342
"lint-no-fix": "eslint --ext '.js,.ts,.tsx' .",
4443
"test": "jest",
45-
"prepack": "bob build && node ./scripts/generate-mappings.js",
44+
"prepack": "bob build && node ./scripts/resolve-aliases.js && node ./scripts/generate-mappings.js",
4645
"release": "release-it",
4746
"docs": "yarn --cwd docs",
4847
"example": "yarn --cwd example"

scripts/generate-ts-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const getFiles = () =>
3636
const writeFiles = (files) => {
3737
mkdirSync(DESTINATION_DIR);
3838

39-
files.forEach((f) =>
39+
files.map((f) =>
4040
writeFileSync(
4141
path.join(
4242
DESTINATION_DIR,

scripts/resolve-aliases.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const LIB_DIR = path.resolve(__dirname, '..', 'lib');
5+
const ALIAS_PREFIX = '@/';
6+
7+
// Recursively find all files with given extensions
8+
function findFiles(dir, extensions) {
9+
const results = [];
10+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
11+
const fullPath = path.join(dir, entry.name);
12+
if (entry.isDirectory()) {
13+
results.push(...findFiles(fullPath, extensions));
14+
} else if (extensions.some((ext) => entry.name.endsWith(ext))) {
15+
results.push(fullPath);
16+
}
17+
}
18+
return results;
19+
}
20+
21+
// Given a file path inside lib/<target>/..., compute the relative path
22+
// from the file's directory to the target root (e.g. lib/commonjs/)
23+
function resolveAlias(filePath, targetRoot, importPath) {
24+
if (!importPath.startsWith(ALIAS_PREFIX)) {
25+
return importPath;
26+
}
27+
28+
const stripped = importPath.slice(ALIAS_PREFIX.length);
29+
const fileDir = path.dirname(filePath);
30+
let relativePath = path.relative(fileDir, path.join(targetRoot, stripped));
31+
32+
// Ensure it starts with ./ or ../
33+
if (!relativePath.startsWith('.')) {
34+
relativePath = `./${relativePath}`;
35+
}
36+
37+
return relativePath;
38+
}
39+
40+
// Patterns that match import/require statements with @/ aliases
41+
// Handles: require("@/..."), from '@/...', from "@/...", import("@/...")
42+
const IMPORT_PATTERN =
43+
/(require\(["']|from\s+["']|import\(["'])(@\/[^"']+)(["'])/g;
44+
45+
function processFile(filePath, targetRoot) {
46+
const content = fs.readFileSync(filePath, 'utf8');
47+
const updated = content.replace(
48+
IMPORT_PATTERN,
49+
(_match, prefix, importPath, suffix) => {
50+
const resolved = resolveAlias(filePath, targetRoot, importPath);
51+
return prefix + resolved + suffix;
52+
}
53+
);
54+
55+
if (content !== updated) {
56+
fs.writeFileSync(filePath, updated);
57+
return true;
58+
}
59+
return false;
60+
}
61+
62+
// Process each build target directory
63+
const targets = [
64+
{ dir: 'commonjs', extensions: ['.js', '.js.map'] },
65+
{ dir: 'module', extensions: ['.js', '.js.map'] },
66+
{ dir: 'typescript', extensions: ['.d.ts', '.d.ts.map'] },
67+
];
68+
69+
let totalResolved = 0;
70+
71+
for (const target of targets) {
72+
const targetDir = path.join(LIB_DIR, target.dir);
73+
if (!fs.existsSync(targetDir)) {
74+
continue;
75+
}
76+
77+
const files = findFiles(targetDir, target.extensions);
78+
let count = 0;
79+
80+
for (const file of files) {
81+
if (processFile(file, targetDir)) {
82+
count++;
83+
}
84+
}
85+
86+
if (count > 0) {
87+
console.log(`Resolved aliases in ${count} files in lib/${target.dir}/`);
88+
totalResolved += count;
89+
}
90+
}
91+
92+
if (totalResolved > 0) {
93+
console.log(`Done — resolved @/ aliases in ${totalResolved} files total.`);
94+
} else {
95+
console.log('No @/ aliases found in build output.');
96+
}

0 commit comments

Comments
 (0)