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

Commit

Permalink
revert: "Use babel's built-in option manager for loading configs." (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tleunen committed Nov 11, 2016
1 parent f811220 commit 6d1add0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 57 deletions.
3 changes: 1 addition & 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,6 @@
"standard-version": "^3.0.0"
},
"peerDependencies": {
"babel-core": "^6.0.0",
"babel-plugin-module-resolver": "^2.3.0"
},
"scripts": {
Expand Down
81 changes: 48 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,41 @@
const path = require('path');
const resolve = require('resolve');
const mapModule = require('babel-plugin-module-resolver').mapModule;
const targetPlugin = require('babel-plugin-module-resolver').default;
const OptionManager = require('babel-core').OptionManager;
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 {};
}

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

exports.interfaceVersion = 2;

function getPlugins(file, target) {
try {
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 @@ -51,15 +61,20 @@ function getPlugins(file, target) {
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 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;
const pluginOpts = getPluginOpts(config);
if (pluginOpts.cwd !== 'babelrc') {
cwd = pluginOpts.cwd || cwd;
}

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

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 @@ -135,23 +134,4 @@ 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,
});
});
});
});

0 comments on commit 6d1add0

Please sign in to comment.