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

fix(remix-dev): use correct require context in serverBareModulesPlugin #6181

Merged
merged 1 commit into from
Apr 26, 2023
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
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