diff --git a/.changeset/empty-taxis-call.md b/.changeset/empty-taxis-call.md new file mode 100644 index 00000000000..65bfc9b0de8 --- /dev/null +++ b/.changeset/empty-taxis-call.md @@ -0,0 +1,5 @@ +--- +"@remix-run/dev": patch +--- + +Use correct require context in bareImports plugin. diff --git a/packages/remix-dev/compiler/server/plugins/bareImports.ts b/packages/remix-dev/compiler/server/plugins/bareImports.ts index ba7e78b961a..acb4283f4da 100644 --- a/packages/remix-dev/compiler/server/plugins/bareImports.ts +++ b/packages/remix-dev/compiler/server/plugins/bareImports.ts @@ -86,7 +86,7 @@ export function serverBareModulesPlugin({ config, options }: Context): Plugin { (pkgManager === "yarn" && process.versions.pnp == null)) ) { try { - require.resolve(path); + require.resolve(path, { paths: [importer] }); } catch (error: unknown) { options.onWarning( `The path "${path}" is imported in ` + @@ -117,7 +117,7 @@ export function serverBareModulesPlugin({ config, options }: Context): Plugin { kind !== "dynamic-import" && config.serverPlatform === "node" ) { - warnOnceIfEsmOnlyPackage(packageName, path, options.onWarning); + warnOnceIfEsmOnlyPackage(packageName, path, importer, options.onWarning); } // Externalize everything else if we've gotten here. @@ -148,10 +148,11 @@ function isBareModuleId(id: string): boolean { function warnOnceIfEsmOnlyPackage( packageName: string, fullImportPath: string, - onWarning: (msg: string, key: string) => void + importer: string, + onWarning: (msg: string, key: string) => void, ) { try { - let packageDir = resolveModuleBasePath(packageName, fullImportPath); + let packageDir = resolveModuleBasePath(packageName, fullImportPath, importer); let packageJsonFile = path.join(packageDir, "package.json"); if (!fs.existsSync(packageJsonFile)) { @@ -193,8 +194,8 @@ function warnOnceIfEsmOnlyPackage( // https://github.com/nodejs/node/issues/33460#issuecomment-919184789 // adapted to use the fullImportPath to resolve sub packages like @heroicons/react/solid -function resolveModuleBasePath(packageName: string, fullImportPath: string) { - let moduleMainFilePath = require.resolve(fullImportPath); +function resolveModuleBasePath(packageName: string, fullImportPath: string, importer: string) { + let moduleMainFilePath = require.resolve(fullImportPath, { paths: [importer] }); let packageNameParts = packageName.split("/");