Skip to content
This repository was archived by the owner on Mar 10, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ your prefix/suffix.
"rules": {},
"settings": {
"import/resolver": {
"babel-plugin-root-import": {}
"babel-plugin-root-import": {
"babelrc": ".customBabelrc",
"options": [{
"rootPathPrefix": "~",
"rootPathSuffix": "src/"
}]
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-import-resolver-babel-root-import",
"version": "1.0.0",
"version": "1.1.0",
"main": "src/index.js",
"description": "babel-plugin-root-import resolver for eslint-plugin-import",
"repository": {
Expand All @@ -16,6 +16,10 @@
{
"name": "Christian Le",
"url": "http://christianle.com"
},
{
"name": "Yuseong Cho",
"email": "[email protected]"
}
],
"license": "MIT",
Expand Down
45 changes: 6 additions & 39 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const path = require('path');
const fs = require('fs');
const JSON5 = require('json5');

const nodeResolve = require('eslint-import-resolver-node').resolve;

const { getOptions } = require('./options');

/* eslint-disable no-console */
const babelRootImport = require('babel-root-import/build/helper.js');

Expand All @@ -24,38 +22,6 @@ if (babelRootImport.default) {
transformRelativeToRootPath = transformRelativeToRootPath.bind(babelRootImportObj);
}

// returns the root import config as an object
function getConfigFromBabel(start, babelrc = '.babelrc') {
if (start === '/') return [];

const packageJSONPath = path.join(start, 'package.json');
const packageJSON = require(packageJSONPath);
const babelConfig = packageJSON.babel;
if (babelConfig) {
const pluginConfig = babelConfig.plugins.find(p => (
p[0] === 'babel-root-import'
));
process.chdir(path.dirname(packageJSONPath));
return pluginConfig[1];
}

const babelrcPath = path.join(start, babelrc);
if (fs.existsSync(babelrcPath)) {
const babelrcJson = JSON5.parse(fs.readFileSync(babelrcPath, 'utf8'));
if (babelrcJson && Array.isArray(babelrcJson.plugins)) {
const pluginConfig = babelrcJson.plugins.find(p => (
p[0] === 'babel-plugin-root-import'
));
// The src path inside babelrc are from the root so we have
// to change the working directory for the same directory
// to make the mapping to work properly
process.chdir(path.dirname(babelrcPath));
return pluginConfig[1];
}
}
return getConfigFromBabel(path.dirname(start));
}

exports.interfaceVersion = 2;

/**
Expand All @@ -65,11 +31,12 @@ exports.interfaceVersion = 2;
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param {object} config - the resolver options
* @param {string} babelrc - the name of the babelrc file
* @param {string} [config.babelrc] - the path of the babelrc file
* @param {(object|array)} [config.options] - the options passed to the plugin
* @return {object}
*/
exports.resolve = (source, file, config, babelrc) => {
const opts = getConfigFromBabel(process.cwd(), babelrc);
exports.resolve = (source, file, config) => {
const opts = getOptions(config, process.cwd());

// [{rootPathPrefix: rootPathSuffix}]
const rootPathConfig = [];
Expand Down
94 changes: 94 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const path = require('path');
const fs = require('fs');
const JSON5 = require('json5');

const ROOT_PATH = path.resolve('/');

const PLUGIN_NAMES = [
'babel-plugin-root-import',
'babel-root-import',
'root-import',
];

const existsBabelrc = (filepath) => (
fs.existsSync(filepath)
);

const parseBabelrc = (filepath) => (
JSON5.parse(fs.readFileSync(filepath, 'utf-8'))
);

const existsPackage = (filepath) => (
// eslint-disable-next-line global-require
fs.existsSync(filepath) && !!(require(filepath).babel)
);

const parsePackage = (filepath) => (
// eslint-disable-next-line global-require
require(filepath).babel
);

const extractOptions = babelConfig => {
let plugin;

if (babelConfig && babelConfig.plugins) {
plugin = babelConfig.plugins.find(([pluginName]) => (
PLUGIN_NAMES.includes(pluginName)
));
}

return (plugin ? plugin[1] : []);
};

const mergeOptions = (...options) => (
options.reduce((result, item) => {
if (Array.isArray(item)) {
result.push(...item);
} else {
result.push(item);
}

return result;
}, [])
);

const getOptions = (lintConfig, cwd) => {
// If all trials fail until reaching the root, stop finding
if (cwd === ROOT_PATH) {
return null;
}

// If the lint config exists, just use it
if (lintConfig) {
const babelrcOptions = lintConfig.babelrc && (
extractOptions(parseBabelrc(path.resolve(cwd, lintConfig.babelrc)))
);

if (babelrcOptions && lintConfig.options) {
return mergeOptions(babelrcOptions, lintConfig.options);
} else if (babelrcOptions) {
return babelrcOptions;
} else if (lintConfig.options) {
return lintConfig.options;
}
}

// Otherwise, find .babelrc file from the cwd
const babelrcPath = path.join(cwd, '.babelrc');

if (existsBabelrc(babelrcPath)) {
return extractOptions(parseBabelrc(babelrcPath));
}

// Otherwise, check if package.json has babel section
const packagePath = path.join(cwd, 'package.json');

if (existsPackage(packagePath)) {
return extractOptions(parsePackage(packagePath));
}

// If all trials fail, retry from the parent directory
return getOptions(lintConfig, path.resolve(cwd, '..'));
};

exports.getOptions = getOptions;
10 changes: 8 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ test((t) => {
path: path.resolve(__dirname, 'somepath/file.js'),
});

const result2 = resolve('@/file', __filename, {}, '.babelrcArray');
const result2 = resolve('@/file', __filename, { options: { rootPathSuffix: 'test/somepath', rootPathPrefix: '@' } });
t.deepEqual(result2, {
found: true,
path: path.resolve(__dirname, 'somepath/file.js'),
});

const result3 = resolve('_/file', __filename, {}, '.babelrcArray');
const result3 = resolve('@/file', __filename, { babelrc: '.babelrcArray' });
t.deepEqual(result3, {
found: true,
path: path.resolve(__dirname, 'somepath/file.js'),
});

const result4 = resolve('_/file', __filename, { babelrc: '.babelrcArray' });
t.deepEqual(result4, {
found: true,
path: path.resolve(__dirname, 'anotherpath/file.js'),
});
Expand Down