Skip to content

Commit

Permalink
feat: Support for new version of babel-plugin-module-resolver v2
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This removes the support for babel-plugin-module-alias v1
  • Loading branch information
Tommy Leunen committed Aug 14, 2016
1 parent 71d7071 commit f2fc1f3
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 80 deletions.
17 changes: 0 additions & 17 deletions .babelrc

This file was deleted.

4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ indent_style = space
indent_size = 4

[*.{json,yml}]
indent_style = space
indent_size = 2

[.*]
indent_size = 2
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# eslint-import-resolver-babel-module-alias
# eslint-import-resolver-babel-module
[![npm][npm-version-image]][npm-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coverage-image]][coverage-url]

A [babel-plugin-module-alias][module-alias] resolver for [eslint-plugin-import][eslint-plugin-import]
A [babel-plugin-module-resolver][babel-plugin-module-resolver] resolver for [eslint-plugin-import][eslint-plugin-import]

## Installation

```sh
npm install --save-dev eslint-plugin-import eslint-import-resolver-babel-module-alias
npm install --save-dev eslint-plugin-import eslint-import-resolver-babel-module
```

## Usage
Expand All @@ -15,24 +15,24 @@ Inside your `.eslintrc` file, pass this resolver to `eslint-plugin-import`:
```
"settings": {
"import/resolver": {
"babel-module-alias": {}
"babel-module": {}
}
}
```

And see [babel-plugin-module-alias][module-alias] to know how to configure your aliases.
And see [babel-plugin-module-resolver][babel-plugin-module-resolver] to know how to configure your aliases.

### Example

```json
{
"extends": "airbnb",
"rules": {
"indent": [2, 4, { "SwitchCase": 1 }]
"comma-dangle": 0
},
"settings": {
"import/resolver": {
"babel-module-alias": {}
"babel-module": {}
}
}
}
Expand All @@ -43,11 +43,11 @@ And see [babel-plugin-module-alias][module-alias] to know how to configure your
MIT, see [LICENSE.md](/LICENSE.md) for details.


[ci-image]: https://circleci.com/gh/tleunen/eslint-import-resolver-babel-module-alias.svg?style=shield
[ci-url]: https://circleci.com/gh/tleunen/eslint-import-resolver-babel-module-alias
[coverage-image]: https://codecov.io/gh/tleunen/eslint-import-resolver-babel-module-alias/branch/master/graph/badge.svg
[coverage-url]: https://codecov.io/gh/tleunen/eslint-import-resolver-babel-module-alias
[npm-version-image]: https://img.shields.io/npm/v/eslint-import-resolver-babel-module-alias.svg
[npm-url]: https://www.npmjs.com/package/eslint-import-resolver-babel-module-alias
[module-alias]: https://github.com/tleunen/babel-plugin-module-alias
[ci-image]: https://circleci.com/gh/tleunen/eslint-import-resolver-babel-module.svg?style=shield
[ci-url]: https://circleci.com/gh/tleunen/eslint-import-resolver-babel-module
[coverage-image]: https://codecov.io/gh/tleunen/eslint-import-resolver-babel-module/branch/master/graph/badge.svg
[coverage-url]: https://codecov.io/gh/tleunen/eslint-import-resolver-babel-module
[npm-version-image]: https://img.shields.io/npm/v/eslint-import-resolver-babel-module.svg
[npm-url]: https://www.npmjs.com/package/eslint-import-resolver-babel-module
[babel-plugin-module-resolver]: https://github.com/tleunen/babel-plugin-module-resolver
[eslint-plugin-import]: https://github.com/benmosher/eslint-plugin-import
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
comment:
layout: "header, diff, tree"
layout: "header, tree"
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "eslint-import-resolver-babel-module-alias",
"name": "eslint-import-resolver-babel-module",
"version": "1.5.1",
"main": "src/index.js",
"description": "babel-plugin-module-alias resolver for eslint-plugin-import",
"description": "babel-plugin-module-resolver resolver for eslint-plugin-import",
"repository": {
"type": "git",
"url": "https://github.com/tleunen/eslint-import-resolver-babel-module-alias.git"
"url": "https://github.com/tleunen/eslint-import-resolver-babel-module.git"
},
"author": "Tommy Leunen <[email protected]> (https://tommyleunen.com)",
"license": "MIT",
Expand All @@ -16,6 +16,7 @@
"babel",
"babel-plugin",
"module",
"resolver",
"alias",
"rewrite",
"resolve",
Expand All @@ -26,22 +27,21 @@
],
"dependencies": {
"find-babel-config": "^1.0.0",
"object-assign": "^4.0.1",
"resolve": "^1.1.7"
},
"devDependencies": {
"babel-plugin-module-alias": "^1.6.0",
"babel-plugin-module-resolver": "^2.0.0",
"chai": "^3.5.0",
"eslint": "^3.3.0",
"eslint-config-airbnb-base": "^5.0.2",
"eslint-plugin-import": "^1.13.0",
"lodash": "^4.15.0",
"mocha": "^3.0.2",
"nyc": "^8.0.0",
"nyc": "^8.1.0",
"standard-version": "^2.4.0"
},
"peerDependencies": {
"babel-plugin-module-alias": "1.x"
"babel-plugin-module-resolver": "2.x"
},
"scripts": {
"lint": "eslint src test",
Expand Down
65 changes: 29 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,44 @@
const path = require('path');
const resolve = require('resolve');
const mapModule = require('babel-plugin-module-alias').mapModule;
const assign = require('object-assign');
const mapModule = require('babel-plugin-module-resolver').mapModule;
const findBabelConfig = require('find-babel-config'); // eslint-disable-line

function findModuleAliasConfig(conf) {
return conf.plugins.find(p => p[0] === 'module-alias' || p[0] === 'babel-plugin-module-alias');
if (conf.plugins) {
return conf.plugins.find(p => p[0] === 'module-resolver' || p[0] === 'babel-plugin-module-resolver');
}
return null;
}

function getMappingFromBabel(start) {
// `let` is not supported outside of the strict mode in node 4 :/
// eslint-disable-next-line strict
'use strict';

const c = findBabelConfig.sync(start);
function getPluginOpts(config) {
const env = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';

if (c && c.config) {
let pluginConfig;
if (c.config.plugins) {
pluginConfig = findModuleAliasConfig(c.config);
}
if (config) {
const pluginConfig = findModuleAliasConfig(config);

if (c.config.env && c.config.env[env] && Array.isArray(c.config.env[env].plugins)) {
const envPluginConfig = findModuleAliasConfig(c.config.env[env]);
if (config.env && config.env[env]) {
const envPluginConfig = findModuleAliasConfig(config.env[env]);
if (envPluginConfig) {
if (pluginConfig) {
pluginConfig[1] = pluginConfig[1].concat(envPluginConfig[1]);
} else {
pluginConfig = envPluginConfig;
return {
root: [].concat(pluginConfig[1].root, envPluginConfig[1].root),
alias: Object.assign({}, pluginConfig[1].alias, envPluginConfig[1].alias)
};
}
return envPluginConfig[1];
}
}

if (pluginConfig) {
// The src path inside babelrc are from the root so we have
// to change the working directory for the "current file directory"
// in order for the mapping in the editor (atom/sublime) to work properly
process.chdir(path.dirname(c.file));
return pluginConfig[1];
}
}

// istanbul ignore next
// cannot reach in this test suite
return [];
return {};
}

function opts(file, config) {
return assign(
return Object.assign(
{},
config,
{ basedir: path.dirname(file) }
Expand All @@ -63,22 +53,25 @@ exports.interfaceVersion = 2;
* resolveImport('./foo', '/Users/ben/bar.js') => '/Users/ben/foo.js'
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param {object} config - the resolver options
* @param {object} options - the resolver options
* @return {object}
*/
exports.resolve = (source, file, config) => {
const mapping = getMappingFromBabel(path.dirname(file)).reduce((memo, e) => {
memo[e.expose] = e.src; // eslint-disable-line no-param-reassign
return memo;
}, {});

exports.resolve = (source, file, options) => {
if (resolve.isCore(source)) return { found: true, path: null };

const { file: babelrcPath, config } = findBabelConfig.sync(path.dirname(file));
if (babelrcPath) {
// Editor have a working directory equals to the directory in which `file` is located
// But the babel plugin works with a working directory based on the babelrc file
process.chdir(path.dirname(babelrcPath));
}

try {
const src = mapModule(source, file, mapping) || source;
const pluginOpts = getPluginOpts(config);
const src = mapModule(source, file, pluginOpts) || source;
return {
found: true,
path: resolve.sync(src, opts(file, config)),
path: resolve.sync(src, opts(file, options)),
};
} catch (e) {
return { found: false };
Expand Down
21 changes: 21 additions & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"plugins": [
["module-resolver", {
"alias": {
"components": "./examples/components",
"underscore": "npm:lodash"
}
}]
],
"env": {
"test": {
"plugins": [
["babel-plugin-module-resolver", {
"alias": {
"subsub": "./examples/components/sub/sub"
}
}]
]
}
}
}
8 changes: 5 additions & 3 deletions test/examples/components/sub/envonly/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"env": {
"test": {
"plugins": [
["module-alias", [
{ "src": "../sub", "expose": "subsub" }
]]
["module-resolver", {
"alias": {
"subsub": "../sub"
}
}]
]
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ const expect = require('chai').expect;
const path = require('path');
const resolverPlugin = require('../src/index');

const savedWorkingDirectory = process.cwd();

const opts = {
};
const extensionOpts = { extensions: ['.js', '.jsx'] };

describe('eslint-import-resolver-module-alias', () => {
describe.only('eslint-import-resolver-module-resolver', () => {
beforeEach(() => {
// reset the working directory because the script updates it
process.chdir(savedWorkingDirectory);
});

it('should export the interfaceVersion', () => {
expect(resolverPlugin)
.to.have.property('interfaceVersion')
Expand Down

0 comments on commit f2fc1f3

Please sign in to comment.