Skip to content

Commit

Permalink
fix(remix-dev): use correct require context in serverBareModulesPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey committed Apr 26, 2023
1 parent b2df1c7 commit 57ecccc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-taxis-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Use correct require context in bareImports plugin.
13 changes: 7 additions & 6 deletions packages/remix-dev/compiler/server/plugins/bareImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ` +
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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("/");

Expand Down

0 comments on commit 57ecccc

Please sign in to comment.