babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

Relative path '.' is transpiled to package.json main field

Open juzerzarif opened this issue 5 years ago • 3 comments

I have a directory structure as follows:

src/some/directory
├── SomeModule.js
└── index.js

And I have a main field specified in my package.json: "main": "src/main.js". If I have a relative import of index.js in SomeModule.js, it gets transpiled to the main field from the package.json.

import utils from '.'; // gets transpiled to '../../main'
import utils from './'; // works

Will try to look into where the resolution is coming from a bit more after work today.

juzerzarif avatar Oct 26 '20 15:10 juzerzarif

Just found where it is coming from: https://github.com/tleunen/babel-plugin-module-resolver/blob/f533cc2b43f867c213d4f7c87bf437a2e2280a4b/src/utils.js#L13

The method isRelativePath isn't matching ..

fsmaia avatar Feb 18 '21 17:02 fsmaia

Fix suggestions:

  1. Replacing custom matcher with !path.isAbsolutePath() (reference)
  2. Refining the regex to make slash optional

This leads me to a question: is there a reason to have a custom matcher? Is there any use case where resolving . to the current package.json main entry is the desired behavior?

fsmaia avatar Feb 18 '21 17:02 fsmaia

Too late I guess :laughing: This is a duplicate of #373, and it will be fixed on #409.

fsmaia avatar Feb 18 '21 18:02 fsmaia