Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add exclude Web Replay option to Metro plugin #4006

Merged
merged 40 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
73c80fc
soft rollback later
lucas-zimerman Aug 2, 2024
8b42ad9
add sentry exclude for replay
lucas-zimerman Aug 9, 2024
bac3255
Merge remote-tracking branch 'origin/main' into ref/exclude-replay
lucas-zimerman Aug 9, 2024
76764d1
changelog
lucas-zimerman Aug 9, 2024
ce604ba
clear tests
lucas-zimerman Aug 9, 2024
3f909f1
clearup PT2
lucas-zimerman Aug 9, 2024
4651c7e
Merge branch 'main' into ref/exclude-replay
lucas-zimerman Aug 9, 2024
9318296
mergeConfig not compatible with old RN 0.65.3
lucas-zimerman Aug 9, 2024
cc4f11d
Merge remote-tracking branch 'origin/ref/exclude-replay' into ref/exc…
lucas-zimerman Aug 9, 2024
3d1b3a1
add changelog snippet, also remove it on expo
lucas-zimerman Aug 9, 2024
c0556fc
Update CHANGELOG.md
lucas-zimerman Aug 15, 2024
0799e0e
Update src/js/tools/metroconfig.ts
lucas-zimerman Aug 15, 2024
7011eb4
Merge branch 'main' into ref/exclude-replay
lucas-zimerman Aug 15, 2024
271bebc
add tests and requested changes
lucas-zimerman Aug 15, 2024
060294d
changelog
lucas-zimerman Aug 15, 2024
27132ce
fix
lucas-zimerman Aug 15, 2024
46ab2a9
Merge branch 'main' into ref/exclude-replay
krystofwoldrich Aug 28, 2024
e663679
bad merge
krystofwoldrich Aug 28, 2024
262c1a7
Update CHANGELOG.md change to feature
krystofwoldrich Aug 28, 2024
ea84b51
requested changes
lucas-zimerman Aug 30, 2024
2f852f8
Merge branch 'main' into ref/exclude-replay
lucas-zimerman Aug 30, 2024
ff7a001
lint fix
lucas-zimerman Sep 2, 2024
984c19e
test disable code
lucas-zimerman Sep 3, 2024
9b155ed
test compatibility with old metro
lucas-zimerman Sep 3, 2024
661a8d0
add tests for old metro
lucas-zimerman Sep 3, 2024
cf29b11
unified tests and replay check
lucas-zimerman Sep 4, 2024
eaa090f
add test for null platform
lucas-zimerman Sep 4, 2024
6b58292
test resolveRequest as null
lucas-zimerman Sep 4, 2024
25437a4
document why null
lucas-zimerman Sep 4, 2024
b48308d
nit
lucas-zimerman Sep 4, 2024
208ae13
test refactor
lucas-zimerman Sep 5, 2024
a843d73
add error
lucas-zimerman Sep 5, 2024
7366928
force error on all versions.
lucas-zimerman Sep 5, 2024
204db68
test console error.
lucas-zimerman Sep 5, 2024
631e930
console error ok, logger logs?
lucas-zimerman Sep 5, 2024
faa94ec
logger doesnt work, should we brek the process?
lucas-zimerman Sep 5, 2024
47d6cd1
final tests
lucas-zimerman Sep 5, 2024
97f0c28
yran fix and set default to true
lucas-zimerman Sep 5, 2024
03b8acc
Apply suggestions from code review
krystofwoldrich Sep 6, 2024
3d5a1c0
Update src/js/tools/metroconfig.ts
krystofwoldrich Sep 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions src/js/tools/metroconfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { logger } from '@sentry/utils';
import type { MetroConfig, MixedOutput, Module, ReadOnlyGraph } from 'metro';
import type { CustomResolutionContext, Resolution } from 'metro-resolver';
import * as process from 'process';
import { env } from 'process';

import { enableLogger } from './enableLogger';
import { cleanDefaultBabelTransformerPath, saveDefaultBabelTransformerPath } from './sentryBabelTransformerUtils';
import { createSentryMetroSerializer, unstable_beforeAssetSerializationPlugin } from './sentryMetroSerializer';
import type { DefaultConfigOptions } from './vendor/expo/expoconfig';

export * from './sentryMetroSerializer';

enableLogger();
Expand Down Expand Up @@ -158,26 +158,83 @@ function withSentryDebugId(config: MetroConfig): MetroConfig {
};
}

type ResolverThreeParams = (
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
context: CustomResolutionContext,
moduleName: string,
platform: string | null,
) => Resolution;
type ResolverFourParams = (
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
context: CustomResolutionContext,
moduleName: string,
platform: string | null,
realModuleName?: string,
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
) => Resolution;

/**
* Includes `@sentry/replay` packages based on the `includeWebReplay` flag and current bundle `platform`.
*/
export function withSentryResolver(config: MetroConfig, includeWebReplay: boolean | undefined): MetroConfig {
const originalResolver = config.resolver?.resolveRequest;
const originalResolver = config.resolver?.resolveRequest as ResolverThreeParams | ResolverFourParams;

const hasSentryReplay = (platform: string | null, moduleName: string): boolean => {
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
return (
(includeWebReplay === false || (includeWebReplay === undefined && platform !== 'web')) &&
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
moduleName.includes('@sentry/replay')
);
};

let resolver: ResolverThreeParams | ResolverFourParams;

// eslint-disable-next-line @typescript-eslint/no-var-requires
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
const metro = require('metro/package.json') as { version: string };
const [major, minor] = metro.version.split('.').map(Number);

if (minor >= 68 || major >= 1) {
// New method introduced on metro 0.68 and newer.
resolver = (context: CustomResolutionContext, moduleName: string, platform: string | null) => {
if (hasSentryReplay(platform, moduleName)) {
return { type: 'empty' } as Resolution;
}
if (originalResolver) {
return originalResolver(context, moduleName, platform);
}
return context.resolveRequest(context, moduleName, platform);
};
} else {
// On older Metro, the given context from resolver is not the defaultResolver but the called function itself.
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-var-requires, import/no-extraneous-dependencies
const defaultResolver = require('metro-resolver').resolve;
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

resolver = (
context: CustomResolutionContext,
realModuleName: string,
platform: string | null,
moduleName?: string,
) => {
if (moduleName && hasSentryReplay(platform, moduleName)) {
return { type: 'empty' };
}
if (originalResolver) {
return originalResolver(context, realModuleName, platform, moduleName);
}

return defaultResolver(
{
...context,
resolveRequest: null,
},
moduleName,
platform,
realModuleName,
);
};
}

return {
...config,
resolver: {
...config.resolver,
resolveRequest: (context, moduleName, platform) => {
if ((includeWebReplay === false || (includeWebReplay === undefined && platform !== 'web'))
&& moduleName.includes('@sentry/replay')) {
return { type: 'empty' };
}
if (originalResolver) {
return originalResolver(context, moduleName, platform);
}
return context.resolveRequest(context, moduleName, platform);
},
resolveRequest: resolver,
},
};
}
Expand Down
Loading
Loading