Skip to content

Commit

Permalink
fix: pass module into loadModule
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Jan 23, 2025
1 parent 9622bba commit e30fc15
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
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 @@ -41,21 +41,23 @@ 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 {
mod = dynamicRequire(module, moduleName);
mod = dynamicRequire(existingModule, moduleName);
} catch (e) {
// no-empty
}

if (!mod) {
try {
const { cwd } = dynamicRequire(module, 'process');
mod = dynamicRequire(module, `${cwd()}/node_modules/${moduleName}`) as T;
const { cwd } = dynamicRequire(existingModule, 'process');
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 @@ -301,7 +301,7 @@ const makeWrappedCreateRequestHandler = () =>
export function instrumentServer(): 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

0 comments on commit e30fc15

Please sign in to comment.