RUM-12185: Support aliased import paths for local SVG discovery - #1352
RUM-12185: Support aliased import paths for local SVG discovery#1352jonathanmos wants to merge 2 commits into
Conversation
|
1a97eab to
ffa6d6f
Compare
There was a problem hiding this comment.
Pull request overview
Adds support for resolving aliased local .svg imports during Session Replay asset discovery, so ReactNativeSVG.buildSvgMap() can map SVG components even when imports use TS/JS path mappings or babel-plugin-module-resolver.
Changes:
- Introduces
PathAliasResolverto resolve non-relative SVG import specifiers viababel-plugin-module-resolverandtsconfig.json/jsconfig.jsonpaths. - Updates
ReactNativeSVG.buildSvgMap()to use the resolver (and reset cached alias config between runs). - Adds regression tests and a benchmark scenario exercising aliased SVG imports.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| yarn.lock | Adds lock entries for tsconfig-paths and babel-plugin-module-resolver (and transitive deps). |
| packages/react-native-babel-plugin/test/react-native-svg.test.ts | Adds coverage for alias resolution via tsconfig/jsconfig + babel-module-resolver, and precedence/fallback behavior. |
| packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts | New resolver implementing alias lookup via Babel partial config + tsconfig-paths. |
| packages/react-native-babel-plugin/src/libraries/react-native-svg/index.ts | Wires alias resolution into buildSvgMap() via resolveImportSource(). |
| packages/react-native-babel-plugin/package.json | Adds tsconfig-paths dependency and babel-plugin-module-resolver devDependency. |
| benchmarks/src/scenario/SessionReplay/component/Svg.tsx | Adds an aliased SVG import case (Group H) to the benchmark test screen. |
| benchmarks/package.json | Adds babel-plugin-module-resolver for the benchmark app. |
| benchmarks/babel.config.js | Configures module-resolver alias used by the benchmark aliased SVG case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ffa6d6f to
b95312d
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Suppressed comments (1)
packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts:39
isRelativePath()only matches./and../with forward slashes, so module-resolver results likesrc/components/icon.svgor Windows-style..\\src\\icon.svgwill be treated as non-relative and discarded, preventing alias resolution.
function isRelativePath(value: string): boolean {
return /^\.?\.\//.test(value);
}
b95312d to
d8bb805
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 11 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts:39
isRelativePath()only matches./...paths; it does not match../...(or deeper) relative paths.babel-plugin-module-resolvercan legally return../...when an alias resolves outside the importing file’s directory, and the current check would incorrectly treat that as non-relative and returnnull(falling back to unresolved resolution).
function isRelativePath(value: string): boolean {
return /^\.?\.\//.test(value);
}
packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts:200
parseExtraNodeModulesSpecifier()returnssubpathincluding a leading "/". Passing that intopath.join(target, subpath)causes Node to discardtarget(because an absolute segment resets the join), so extraNodeModules aliases likeassets/icon.svgwill resolve to/icon.svginstead of${target}/icon.svg.
const { packageName, subpath } = parseExtraNodeModulesSpecifier(
importSource
);
const target = extraNodeModules[packageName];
if (!target) {
return null;
}
return subpath ? pathN.join(target, subpath) : target;
d8bb805 to
d890b12
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 11 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts:39
isRelativePath()only matches sources starting with./(due to^\.?\.\/), so it will incorrectly treat../...(and Windows.\\/..\\) paths returned by babel-plugin-module-resolver as non-relative and skip resolution. That can break aliases whose replacement uses parent-directory segments.
function isRelativePath(value: string): boolean {
return /^\.?\.\//.test(value);
}
packages/react-native-babel-plugin/src/libraries/react-native-svg/index.ts:265
- Same issue as the ImportDeclaration handler: relative/absolute re-export sources that don't end in
.svgcan never be resolved byPathAliasResolver, so collecting them for the second pass is unnecessary overhead.
const source = path.node.source?.value;
if (!source) {
return;
}
if (source.endsWith('.svg')) {
const resolved = this.resolveImportSource(
file,
source
);
this.populateExportedSvgNames(path, resolved);
return;
}
const candidateNames: string[] = [];
for (const spec of path.node.specifiers) {
if (spec.type !== 'ExportSpecifier') {
continue;
}
// spec.exported is the name consumers import under
// ('default' would be wrong for `export { default as Logo }`)
const exported = spec.exported;
const name = getNodeName(
this.t,
this.t.isStringLiteral(exported)
? exported.value
: exported.name
);
if (name) {
candidateNames.push(name);
}
}
if (!candidateNames.length) {
return;
}
pendingBareSources.push({
file,
source,
candidateNames,
populate: resolved =>
this.populateExportedSvgNames(path, resolved)
});
}
d890b12 to
a1af24b
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 11 changed files in this pull request and generated no new comments.
Suppressed comments (1)
packages/react-native-babel-plugin/src/libraries/react-native-svg/pathAliasResolver.ts:39
isRelativePath()only recognizes./and../with forward slashes. On Windows, babel-plugin-module-resolver commonly returns relative paths using backslashes (e.g..\\src\\.../..\\...), which will be treated as non-relative here and make aliased imports fail to resolve on Windows.
function isRelativePath(value: string): boolean {
return /^\.?\.\//.test(value);
}
What does this PR do?
Adds support for resolving aliased local SVG imports during Session Replay asset discovery.
Supports TypeScript/JavaScript path mappings and babel-plugin-module-resolver, including .babelrc, function-form plugin configurations, and Windows paths. Includes regression coverage for the supported configurations.
Handle alias entries in babel-plugin-module-resolver that resolve to absolute paths, not just relative ones
Detect aliased SVG imports whose specifier has no .svg extension (alias points directly at the file)
Add support for metro.config.js's resolver.extraNodeModules, matching Metro's own scoped-specifier parsing
Before

After

Motivation
What inspired you to submit this pull request?
Additional Notes
Anything else we should know when reviewing?
Review checklist (to be filled by reviewers)