tleunen / babel-plugin-module-resolver

Custom module resolver plugin for Babel
MIT License
3.46k stars 205 forks source link

Require sibling index.js regression #296

Open albertfdp opened 6 years ago

albertfdp commented 6 years ago

It seems that requiring a sibling index.js file in a folder with the require('.') notation stopped working since https://github.com/tleunen/babel-plugin-module-resolver/pull/253

I usually have the following structure for components:

|-- components
|    -- Button
|        -- index.js
|        -- spec.js

and I'd require the file on spec.js with:

const Button = require('.');

This stopped working on 3.1.0 and I need to require('./index.js') which is a bit cumbersome and also in nodejs it's standard to resolve require('.') to the sibling index.js file.

This is an example breaking test, which passes before https://github.com/tleunen/babel-plugin-module-resolver/commit/7dc2da6fe2deb26c1cb717a1a10aca048217dc82

describe('when requiring a sibling index.js', () => {
    const siblingAliasTransformOpts = {
      babelrc: false,
      plugins: [
        [plugin, {
          root: ['./src'],
        }],
      ],
    };

    it('should resolve an index.js on a folder name', () => {
      testWithImport(
        '.',
        '.',
        siblingAliasTransformOpts,
      );
    });
  });
  ● module-resolver › when requiring a sibling index.js › should resolve an index.js on a folder name

    expect(received).toBe(expected) // Object.is equality

    Expected value to be:
      "import something from \".\";"
    Received:
      "import something from \"./src\";"

      10 |     const result = transform(code, transformerOpts);
      11 |
    > 12 |     expect(result.code).toBe(`import something from "${output}";`);
      13 |   }
      14 |
      15 |   describe('exports', () => {

I'm happy to help with a PR or to complement with more details. Thanks!

fatfisz commented 6 years ago

Seems like a legit bug.

Thanks for volunteering to create a PR - that's the spirit! I'd only recommend adding filename to the test options (can point e.g. to './test/testproject/src/components/App.js' because it has a sibling index.js file) and using './test/testproject/src' as root.