Skip to content

Commit 01aceac

Browse files
committed
chore: configure shared application import aliases
1 parent 0c9f5fc commit 01aceac

7 files changed

Lines changed: 131 additions & 5 deletions

File tree

app/theme.tsx renamed to app/theme/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import hoistNonReactStatics from 'hoist-non-react-statics';
22
import { createContext, useContext, type ComponentType } from 'react';
33

4-
import { type IThemePreference } from './definitions/ITheme';
5-
import { type TNavigationOptions } from './definitions/navigationTypes';
6-
import { colors } from './lib/constants/colors';
4+
import { type IThemePreference } from '../definitions/ITheme';
5+
import { type TNavigationOptions } from '../definitions/navigationTypes';
6+
import { colors } from '../lib/constants/colors';
77

88
export type TSupportedThemes = keyof typeof colors;
99
export type TColors = (typeof colors)[TSupportedThemes];

babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
const getAliasConfig = require('./config/import-aliases');
2+
13
module.exports = {
24
presets: ['babel-preset-expo'],
35
plugins: [
6+
['module-resolver', { alias: getAliasConfig().babel }],
47
['@babel/plugin-proposal-decorators', { legacy: true }],
58
'@babel/plugin-transform-named-capturing-groups-regex',
69
['module:react-native-dotenv'],

config/import-aliases.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const tsconfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, '../tsconfig.json'), 'utf8'));
5+
const paths = tsconfig.compilerOptions.paths;
6+
7+
const entries = Object.entries(paths).sort(([left], [right]) => right.length - left.length);
8+
9+
const stripWildcard = value => value.replace(/\*$/, '');
10+
const stripAliasSuffix = value => stripWildcard(value).replace(/\/$/, '');
11+
const relativeTarget = value => stripAliasSuffix(value).replace(/^\.\//, '');
12+
const relativeTargetWithSlash = value => stripWildcard(value).replace(/^\.\//, '');
13+
14+
const getAliasConfig = () => ({
15+
babel: Object.fromEntries(entries.map(([key, [value]]) => [stripAliasSuffix(key), `./${relativeTarget(value)}`])),
16+
jest: Object.fromEntries(
17+
entries.map(([key, [value]]) => [
18+
`^${stripWildcard(key).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(.*)$`,
19+
`<rootDir>/${relativeTargetWithSlash(value)}$1`
20+
])
21+
)
22+
});
23+
24+
module.exports = getAliasConfig;

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const getAliasConfig = require('./config/import-aliases');
2+
13
module.exports = {
24
modulePathIgnorePatterns: ['<rootDir>/.*worktrees/'],
35
testPathIgnorePatterns: [
@@ -16,7 +18,8 @@ module.exports = {
1618
collectCoverage: false,
1719
moduleNameMapper: {
1820
'.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy',
19-
'.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js'
21+
'.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js',
22+
...getAliasConfig().jest
2023
},
2124
setupFilesAfterEnv: ['./jest.setup.js']
2225
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
"@types/url-parse": "^1.4.8",
193193
"babel-jest": "~29.7.0",
194194
"babel-loader": "~9.1.3",
195+
"babel-plugin-module-resolver": "^5.0.3",
195196
"babel-plugin-react-compiler": "1.0.0",
196197
"babel-plugin-transform-remove-console": "^6.9.4",
197198
"babel-preset-expo": "~54.0.9",

pnpm-lock.yaml

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
"experimentalDecorators": true,
1616
"skipLibCheck": true,
1717
"forceConsistentCasingInFileNames": true,
18-
"resolveJsonModule": true
18+
"resolveJsonModule": true,
19+
"paths": {
20+
"~/*": ["./app/*"],
21+
"~/.rnstorybook/*": ["./.rnstorybook/*"]
22+
}
1923
},
2024
"exclude": ["node_modules", "__mocks__", "**/.worktrees/**"]
2125
}

0 commit comments

Comments
 (0)