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

fix: `.` and `./` incorrectly converted

Open relign opened this issue 5 years ago • 14 comments

. and ./ are specific paths, we can't transfrom them. There are many projects(example) that use import xxx from '.' . Example code:

// index.js
export var name = 'zhangsan'
// test.js
import { name } from '.'
console.log('name', name)

The directory structure:

├── src
│   ├── index.js
│   └── mock
│       ├── index.js
│       └── test.js

Babel config:

["module-resolver", {
            "root": ["./src"]
        }]

Babel Transform Output:

// test.js
import { name } from "./..";
console.log('name', name);

And it should transfrom code import { name } from './index', not import { name } from './..'

relign avatar Sep 15 '20 12:09 relign

Please add some tests too 🙏

fatfisz avatar Sep 15 '20 13:09 fatfisz

Also this should probably affect paths like foo/bar/., and not only single dots.

Now that I think of it, maybe adding an option that introduces this behavior instead of the default one would be the best (because it's non-standard compared to Node resolve algorithm).

fatfisz avatar Sep 15 '20 14:09 fatfisz

Please add some tests too 🙏

Test Case have been supplemented

relign avatar Sep 16 '20 12:09 relign

Also this should probably affect paths like foo/bar/., and not only single dots.

Now that I think of it, maybe adding an option that introduces this behavior instead of the default one would be the best (because it's non-standard compared to Node resolve algorithm).

like foo/bar/. , it is no problem after test. I agree with you that this is not a standard usage, but we have to be compatible with it.

relign avatar Sep 16 '20 12:09 relign

I've never seen really from '.', but I've seen from './' in many projects, so if that's an issue, I definitely agree we need to fix it :) Thank you @relign!

@fatfisz You good with merging this?

tleunen avatar Sep 21 '20 11:09 tleunen

@tleunen @fatfisz Any ETA on when this will be merged/released?

juzerzarif avatar Oct 28 '20 03:10 juzerzarif

@fatfisz @tleunen up

fsmaia avatar Feb 18 '21 17:02 fsmaia

Just for curiosity: why not !path.isAbsolutePath()?

fsmaia avatar Feb 18 '21 18:02 fsmaia

Just for curiosity: why not !path.isAbsolutePath()?

path.isAbsolute('.') is false. so far, import xxx from '.' is the only problem.

relign avatar Feb 25 '21 07:02 relign

@tleunen @fatfisz can I do anything to help push this along? Just started using this excellent package, but this bug is causing pretty serious issues for us.

nwalters512 avatar Jul 27 '21 00:07 nwalters512

Hi all, has this plugin been abandoned? Have not seen any activity in months and this PR has been open since then

htang014 avatar Jan 30 '22 10:01 htang014

@tleunen just as an example react-native-calendars has this https://github.com/wix/react-native-calendars/blob/40241905cbf79948d3eb0b9f61e0728959a56e25/src/expandableCalendar/Context/Provider.tsx#L10

Grohden avatar Feb 17 '23 17:02 Grohden

@Grohden It's allowed so it should definitely be supported. I agree. Even if it's kind of risky of having circular dependencies.

tleunen avatar Feb 17 '23 18:02 tleunen

Yo, suggestion for maintainers: use prettier or dprint to format the project so we don't have unnecessary changes/noise at PRs (like formatting)

Also, for people looking for a patch (for patch package) here's the simple version (its just the if, its basically the same for 4.1.0)

babel-plugin-module-resolver+5.0.0.patch

diff --git a/node_modules/babel-plugin-module-resolver/lib/resolvePath.js b/node_modules/babel-plugin-module-resolver/lib/resolvePath.js
index cd53e95..5f17543 100644
--- a/node_modules/babel-plugin-module-resolver/lib/resolvePath.js
+++ b/node_modules/babel-plugin-module-resolver/lib/resolvePath.js
@@ -75,7 +75,11 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
 }
 const resolvers = [resolvePathFromAliasConfig, resolvePathFromRootConfig];
 function resolvePath(sourcePath, currentFile, opts) {
-  if ((0, _utils.isRelativePath)(sourcePath)) {
+  // https://github.com/tleunen/babel-plugin-module-resolver/pull/409
+  // `.` and `./` are specific paths, we can't transform them
+  const isSpecificPath = sourcePath === '.' || sourcePath === './'
+
+  if ((0, _utils.isRelativePath)(sourcePath) || isSpecificPath) {
     return sourcePath;
   }
   const normalizedOpts = (0, _normalizeOptions.default)(currentFile, opts);

btw, I wonder what kind of changes can be done to make it easier to debug whats being transformed.. I spend the whole day trying to figure out why my test was ending up with a circular import (undefined import values) in a unrelated file.. I at least added a debug flag for my own plugin

Grohden avatar Feb 17 '23 23:02 Grohden