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

fix(monorepo): fix babel options resolve #104

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"import"
],
"dependencies": {
"find-babel-config": "^1.2.0",
Copy link
Owner

Choose a reason for hiding this comment

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

Funny how I was thinking to deprecate the library as there should be a better "official" way of fetching the config now (see babel/babel#7472)

"pkg-up": "^2.0.0",
"resolve": "^1.10.0"
},
Expand Down
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const path = require('path');
const resolve = require('resolve');
const pkgUp = require('pkg-up');
const findBabelConfig = require('find-babel-config');
const { resolvePath } = require('babel-plugin-module-resolver');
const { OptionManager } = require('@babel/core');

function getPlugins(file, cwd) {
function getPlugins(file) {
try {
const manager = new OptionManager();
const result = manager.init({
babelrc: true,
filename: file,
cwd,
cwd: path.dirname(findBabelConfig.sync(file).file),
});

return result.plugins.filter(plugin => plugin.key === 'module-resolver');
Expand All @@ -24,8 +25,8 @@ function getPlugins(file, cwd) {
}
}

function getPluginOptions(file, cwd, defaultOptions) {
const instances = getPlugins(file, cwd);
function getPluginOptions(file, defaultOptions) {
const instances = getPlugins(file);

return instances.reduce(
(config, plugin) => ({
Expand Down Expand Up @@ -84,7 +85,6 @@ exports.resolve = (source, file, opts) => {
try {
const pluginOptions = getPluginOptions(
file,
projectRootDir,
{
// if .babelrc doesn't exist, try to get the configuration information from `options`,
// which gets defined by the eslint configuration file.
Expand Down
10 changes: 10 additions & 0 deletions test/examples/monorepo/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
["module-resolver", {
"alias": {
"Common": "./src/components/Common",
},
"cwd": "babelrc"
}]
]
}
11 changes: 11 additions & 0 deletions test/examples/monorepo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "monorepo",
"version": "0.0.0",
"description": "Root of monorepo",
"private": true,
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "",
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test1",
"main": "Test.js",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test2",
"main": "Test.js",
"private": true
}
12 changes: 11 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,22 @@ describe('eslint-import-resolver-module-resolver', () => {
});

describe('usage in a monorepo', () => {
it('should return `true` when mapped to a file', () => {
it('should return `true` when mapped to a file based on babel config in the child package', () => {
expect(resolverPlugin.resolve('~/item', path.resolve('./test/examples/monorepo/my-lib/src/item'), extensionOpts))
.toEqual({
found: true,
path: path.resolve(__dirname, './examples/monorepo/my-lib/src/item.js'),
});
});

it('should return `true` when mapped to a file based on root babel config', () => {
expect(resolverPlugin.resolve(
'Common/Test2', path.resolve('./test/examples/monorepo/src/components/Common/Test1'), extensionOpts,
))
.toEqual({
found: true,
path: path.resolve(__dirname, './examples/monorepo/src/components/Common/Test2/Test.js'),
});
});
});
});
Loading