Skip to content

fix(v8/core): Pass module into loadModule (#15139) #15166

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

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Now, there are two options to set up the SDK:
);
```

Work in this release was contributed by @jahands. Thank you for your contribution!
Work in this release was contributed by @jahands and @jrandolf. Thank you for your contributions!

## 8.51.0

Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/utils-hoist/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,26 @@ export function dynamicRequire(mod: any, request: string): any {
* That is to mimic the behavior of `require.resolve` exactly.
*
* @param moduleName module name to require
* @param existingModule module to use for requiring
* @returns possibly required module
*/
export function loadModule<T>(moduleName: string): T | undefined {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function loadModule<T>(moduleName: string, existingModule: any = module): T | undefined {
let mod: T | undefined;

try {
// eslint-disable-next-line deprecation/deprecation
mod = dynamicRequire(module, moduleName);
mod = dynamicRequire(existingModule, moduleName);
} catch (e) {
// no-empty
}

if (!mod) {
try {
// eslint-disable-next-line deprecation/deprecation
const { cwd } = dynamicRequire(module, 'process');
const { cwd } = dynamicRequire(existingModule, 'process');
// eslint-disable-next-line deprecation/deprecation
mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T;
mod = dynamicRequire(existingModule, `${cwd()}/node_modules/${moduleName}`) as T;
} catch (e) {
// no-empty
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export function constructWebpackConfigFunction(
// Symbolication for dev-mode errors is done elsewhere.
if (!isDev) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin') ?? {};
const { sentryWebpackPlugin } = loadModule<{ sentryWebpackPlugin: any }>('@sentry/webpack-plugin', module) ?? {};

if (sentryWebpackPlugin) {
if (!userSentryOptions.sourcemaps?.disable) {
Expand Down
2 changes: 1 addition & 1 deletion packages/remix/src/utils/instrumentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ const makeWrappedCreateRequestHandler = (options: RemixOptions) =>
export function instrumentServer(options: RemixOptions): void {
const pkg = loadModule<{
createRequestHandler: CreateRequestHandlerFunction;
}>('@remix-run/server-runtime');
}>('@remix-run/server-runtime', module);

if (!pkg) {
DEBUG_BUILD && logger.warn('Remix SDK was unable to require `@remix-run/server-runtime` package.');
Expand Down
Loading