This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Use babel
's built-in option manager for loading configs.
#28
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
64bde6d
Use `babel`'s built-in option manager for loading configs.
izaakschroeder 4dec959
wip thing.
izaakschroeder 8a88194
fix thing.
izaakschroeder 877989c
this will go.
izaakschroeder 358419c
Survive babel death.
izaakschroeder 9c57800
Jiggly pokey the internals to guard more aggressively.
izaakschroeder 6e859f5
Use straight from babel-core.
izaakschroeder c618af7
more feedbacks
izaakschroeder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
* | ||
|
@@ -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)), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.