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

Use babel's built-in option manager for loading configs. #28

Merged
merged 8 commits into from
Nov 9, 2016
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"import"
],
"dependencies": {
"find-babel-config": "^1.0.0",
"resolve": "^1.1.7"
},
"devDependencies": {
"babel-core": "^6.0.0",
"babel-jest": "^16.0.0",
"babel-plugin-module-resolver": "^2.3.0",
"eslint": "^3.3.0",
Expand All @@ -43,7 +43,8 @@
"standard-version": "^3.0.0"
},
"peerDependencies": {
"babel-plugin-module-resolver": "^2.3.0"
"babel-core": "^6.0.0",
"babel-plugin-module-resolver": "^3.0.0-beta"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert to the 2.3.0 version, I'm dropping the v3 beta for now.

},
"scripts": {
"lint": "eslint src test",
Expand Down
81 changes: 33 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,7 @@
const path = require('path');
const resolve = require('resolve');
const mapModule = require('babel-plugin-module-resolver').mapModule;
const findBabelConfig = require('find-babel-config'); // eslint-disable-line

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);

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];
}
}

if (pluginConfig) {
return pluginConfig[1];
}
}

return {};
}
const targetPlugin = require('babel-plugin-module-resolver').default;

function opts(file, config) {
return Object.assign(
Expand All @@ -49,6 +15,30 @@ function opts(file, config) {

exports.interfaceVersion = 2;

function getPlugins(file, target) {
try {
const OptionManager = require('babel-core').OptionManager; // eslint-disable-line
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not: Should be safe to move it again at the top of the file, right?

const manager = new OptionManager();
const result = manager.init({
babelrc: true,
filename: file,
});
return result.plugins.filter((plugin) => {
const plug = OptionManager.memoisedPlugins.find(item =>
item.plugin === plugin[0]
);
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/28');
return [];
}
}

/**
* Find the full path to 'source', given 'file' as a full reference path.
*
Expand All @@ -61,20 +51,15 @@ 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();

try {
const pluginOpts = getPluginOpts(config);
if (pluginOpts.cwd !== 'babelrc') {
cwd = pluginOpts.cwd || cwd;
}

const src = mapModule(source, file, pluginOpts, cwd) || source;
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: process.cwd() });
pluginOpts.cwd = path.resolve(pluginOpts.cwd);
const src = mapModule(source, file, pluginOpts, pluginOpts.cwd) || source;
return {
found: true,
path: resolve.sync(src, opts(file, options)),
Expand Down
1 change: 1 addition & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"plugins": [
["module-resolver", {
"cwd": "./test",
"alias": {
"components": "./examples/components",
"underscore": "npm:lodash"
Expand Down
1 change: 1 addition & 0 deletions test/examples/components/sub/envonly/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"test": {
"plugins": [
["module-resolver", {
"cwd": "./test/examples/components/sub/envonly",
"alias": {
"subsub": "../sub"
}
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

const opts = {};
const extensionOpts = { extensions: ['.js', '.jsx'] };
Expand Down Expand Up @@ -134,4 +135,23 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});
});

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

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

it('should survive babel blowing up', () => {
OptionManager.prototype.init = () => { throw new TypeError(); };
expect(resolverPlugin.resolve('underscore', path.resolve('./test/examples/file1'), opts))
.toEqual({
found: false,
});
});
});
});