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

Commit

Permalink
feat: Use babel's built-in option manager for loading configs (#34)
Browse files Browse the repository at this point in the history
The babel-plugin-module-resolver can now be set into a preset and the eslint plugin will work the same.
BREAKING CHANGE: The root config and alias must be relative to the project root, not relative to the babelrc file.
  • Loading branch information
tleunen committed Dec 26, 2016
1 parent fb3617d commit c526927
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 137 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
["transform-object-rest-spread", { "useBuiltIns": true }]
]
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ node_modules/
npm-debug.log

coverage/
.nyc_output
lib/
72 changes: 0 additions & 72 deletions lib/index.js

This file was deleted.

13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "eslint-import-resolver-babel-module",
"version": "2.2.1",
"main": "src/index.js",
"main": "lib/index.js",
"description": "babel-plugin-module-resolver resolver for eslint-plugin-import",
"repository": {
"type": "git",
"url": "https://github.com/tleunen/eslint-import-resolver-babel-module.git"
},
"files": [
"src"
"lib"
],
"author": "Tommy Leunen <tommy.leunen@gmail.com> (https://tommyleunen.com)",
"license": "MIT",
Expand All @@ -29,11 +29,15 @@
"import"
],
"dependencies": {
"find-babel-config": "^1.0.1",
"pkg-up": "^1.0.0",
"resolve": "^1.2.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
"babel-core": "^6.21.0",
"babel-jest": "^18.0.0",
"babel-plugin-module-resolver": "^2.4.0",
"babel-plugin-transform-object-rest-spread": "^6.20.2",
"eslint": "^3.12.2",
"eslint-config-tleunen": "^1.0.0",
"eslint-plugin-import": "^2.2.0",
Expand All @@ -42,14 +46,17 @@
"standard-version": "^4.0.0"
},
"peerDependencies": {
"babel-core": "^6.0.0",
"babel-plugin-module-resolver": "^2.4.0"
},
"scripts": {
"lint": "eslint src test",
"compile": "babel src --out-dir lib",
"pretest": "npm run lint",
"test": "jest --coverage",
"test:suite": "jest",
"test:watch": "jest --watch",
"prepublish": "npm run compile",
"release": "standard-version"
},
"jest": {
Expand Down
82 changes: 36 additions & 46 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,41 @@

const path = require('path');
const resolve = require('resolve');
const pkgUp = require('pkg-up');
const targetPlugin = require('babel-plugin-module-resolver').default;
const mapModule = require('babel-plugin-module-resolver').mapModule;
const findBabelConfig = require('find-babel-config'); // eslint-disable-line
const OptionManager = require('babel-core').OptionManager;

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

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

if (config) {
const pluginConfig = findModuleAliasConfig(config);
function getPlugins(file, target) {
try {
const manager = new OptionManager();
const result = manager.init({
babelrc: true,
filename: file,
});

if (config.env && config.env[env]) {
const envPluginConfig = findModuleAliasConfig(config.env[env]);
if (envPluginConfig) {
if (pluginConfig) {
return {
root: [].concat(pluginConfig[1].root, envPluginConfig[1].root),
alias: Object.assign({}, pluginConfig[1].alias, envPluginConfig[1].alias),
};
}
return envPluginConfig[1];
}
}
return result.plugins.filter((plugin) => {
const plug = OptionManager.memoisedPlugins.find(item =>
item.plugin === plugin[0],
);

if (pluginConfig) {
return pluginConfig[1];
}
return plug && plug.container === target;
});
} catch (err) {
// This error should only occur if something goes wrong with babel's
// internals. Dump it to console so people know what's going on,
// elsewise the error will simply be squelched in the calling code.
console.error('[eslint-import-resolver-babel-module]', err);
console.error('See: https://github.com/tleunen/eslint-import-resolver-babel-module/pull/34');
return [];
}

return {};
}

function opts(file, config) {
return Object.assign(
{},
config,
{ basedir: path.dirname(file) },
);
return {
...config,
basedir: path.dirname(file),
};
}

exports.interfaceVersion = 2;
Expand All @@ -61,20 +53,18 @@ exports.interfaceVersion = 2;
exports.resolve = (source, file, options) => {
if (resolve.isCore(source)) return { found: true, path: null };

const babelConfig = findBabelConfig.sync(path.dirname(file));
const babelrcPath = babelConfig.file;
const config = babelConfig.config;
let cwd = babelrcPath
? path.dirname(babelrcPath)
: process.cwd();
const projectRootDir = path.dirname(pkgUp.sync(file));

try {
const pluginOpts = getPluginOpts(config);
if (pluginOpts.cwd !== 'babelrc') {
cwd = pluginOpts.cwd || cwd;
}
const instances = getPlugins(file, targetPlugin);

const pluginOpts = instances.reduce((config, plugin) => ({
cwd: plugin[1] && plugin[1].cwd ? plugin[1].cwd : config.cwd,
root: config.root.concat(plugin[1] && plugin[1].root ? plugin[1].root : []),
alias: Object.assign(config.alias, plugin[1] ? plugin[1].alias : {}),
}), { root: [], alias: {}, cwd: projectRootDir });

const src = mapModule(source, file, pluginOpts, cwd) || source;
const src = mapModule(source, file, pluginOpts, path.resolve(pluginOpts.cwd)) || source;
return {
found: true,
path: resolve.sync(src, opts(file, options)),
Expand Down
4 changes: 2 additions & 2 deletions test/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"plugins": [
["module-resolver", {
"alias": {
"components": "./examples/components",
"components": "./test/examples/components",
"underscore": "npm:lodash"
}
}]
Expand All @@ -12,7 +12,7 @@
"plugins": [
["babel-plugin-module-resolver", {
"alias": {
"subsub": "./examples/components/sub/sub"
"subsub": "./test/examples/components/sub/sub"
}
}]
]
Expand Down
13 changes: 0 additions & 13 deletions test/examples/components/sub/envonly/.babelrc

This file was deleted.

28 changes: 28 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

const path = require('path');
const OptionManager = require('babel-core').OptionManager;
const resolverPlugin = require('../src/index');

const opts = {};
Expand Down Expand Up @@ -134,4 +135,31 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});
});

describe('babel internals', () => {
let oldInit;
let error;

beforeEach(() => {
oldInit = OptionManager.prototype.init;
error = console.error;
});
afterEach(() => {
OptionManager.prototype.init = oldInit;
console.error = error;
});

it('should survive babel blowing up', () => {
console.error = jest.fn();

OptionManager.prototype.init = undefined;

expect(resolverPlugin.resolve('underscore', path.resolve('./test/examples/file1'), opts))
.toEqual({
found: false,
});

expect(console.error).toBeCalled();
});
});
});

0 comments on commit c526927

Please sign in to comment.