From 153f3dab6cd8bb991f0f92bab2df18f3cf0d4f6c Mon Sep 17 00:00:00 2001 From: Izaak Schroeder Date: Wed, 2 Nov 2016 03:23:58 -0700 Subject: [PATCH] Use `babel`'s built-in option manager for loading configs. There is an issue if you configure this plugin anywhere outside of your `.babelrc`. For example, if you have: ```json { "presets": ["my-preset"] } ``` And in your preset you have this plugin configured, then this plugin will fail to find that configuration value. By leveraging babel's built-in option manager for doing all the heavy lifting we avoid these pitfalls. --- package.json | 3 ++- src/index.js | 67 +++++++++++++++++++--------------------------------- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/package.json b/package.json index 7b96a30..9938540 100644 --- a/package.json +++ b/package.json @@ -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": "^3.0.0-beta.1", "eslint": "^3.3.0", @@ -43,6 +43,7 @@ "standard-version": "^3.0.0" }, "peerDependencies": { + "babel-core": "^6.0.0", "babel-plugin-module-resolver": "^3.0.0-beta" }, "scripts": { diff --git a/src/index.js b/src/index.js index f0b8b59..adfb1b0 100644 --- a/src/index.js +++ b/src/index.js @@ -1,41 +1,11 @@ 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; +const OptionManager = + require('babel-core/lib/transformation/file/options/option-manager'); +const buildConfigChain = + require('babel-core/lib/transformation/file/options/build-config-chain'); function opts(file, config) { return Object.assign( @@ -59,15 +29,26 @@ 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; - const cwd = babelrcPath - ? path.dirname(babelrcPath) - : process.cwd(); - try { - const pluginOpts = getPluginOpts(config); + const manager = new OptionManager(); + const babelOptions = { + babelrc: true, + filename: file, + }; + const configs = buildConfigChain(babelOptions); + configs.forEach(x => manager.mergeOptions(x)); + manager.normaliseOptions(babelOptions); + const cwd = configs.length > 0 ? configs[0].dirname : process.cwd(); + const instances = manager.options.plugins.filter((plugin) => { + const plug = OptionManager.memoisedPlugins.find(item => + item.plugin === plugin[0] + ); + return plug && plug.container === targetPlugin; + }); + const pluginOpts = instances.reduce((config, plugin) => ({ + root: config.root.concat(plugin[1] ? plugin[1].root : []), + alias: Object.assign(config.alias, plugin[1] ? plugin[1].alias : {}), + }), { root: [], alias: {} }); const src = mapModule(source, file, pluginOpts, cwd) || source; return { found: true,