Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
feat: Allow early exit on existing directories (#115)
Browse files Browse the repository at this point in the history
* add option to early exit on existing directories

* fix linting issues

* add tests

* check for directory if it exists

* remove previous check because it's not needed

* fix: add doc for allowExistingDirectories
  • Loading branch information
dword-design committed Apr 19, 2021
1 parent 14a78a6 commit f5f205e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ And see [babel-plugin-module-resolver][babel-plugin-module-resolver] to know how
}
```

## Directory Imports

Some babel plugins like [babel-plugin-import-directory](https://github.com/Anmo/babel-plugin-import-directory) or [babel-plugin-wildcard](https://github.com/vihanb/babel-plugin-wildcard) allow to import directories (i.e. each file inside a directory) as an object. In order to support this, you can activate the `allowExistingDirectories` option as follows:

```
"settings": {
"import/resolver": {
"babel-module": { allowExistingDirectories: true }
}
}
```

Now when you import a directory like this, the ESLint plugin won't complain and recognize the existing directory:

```
import * as Items from './dir';
```

## License

MIT, see [LICENSE.md](/LICENSE.md) for details.
Expand Down
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const resolve = require('resolve');
const pkgUp = require('pkg-up');
const { resolvePath } = require('babel-plugin-module-resolver');
const { OptionManager } = require('@babel/core');
const fs = require('fs');

function getPlugins(file, cwd, babelOptions) {
try {
Expand Down Expand Up @@ -109,6 +110,11 @@ exports.resolve = (source, file, opts) => {
const finalSource = stripWebpack(source, pluginOptions.alias);
const resolvePathFunc = pluginOptions.resolvePath || resolvePath;
const src = resolvePathFunc(finalSource, file, pluginOptions);
const absoluteSrc = path.join(path.dirname(file), src);

if (options.allowExistingDirectories && fs.existsSync(absoluteSrc)) {
return { found: true, path: absoluteSrc };
}

const extensions = options.extensions || pluginOptions.extensions;

Expand Down
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,14 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});
});

describe('allowExistingDirectories', () => {
it('should return `true` when directory exists', () => {
expect(resolverPlugin.resolve('components/sub', path.resolve('./test/examples/components/sub/c1'), { ...opts, allowExistingDirectories: true }))
.toEqual({
found: true,
path: `${path.resolve(__dirname, './examples/components/sub')}/`,
});
});
});
});

0 comments on commit f5f205e

Please sign in to comment.