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

Commit

Permalink
feat: Add monorepo support (#101)
Browse files Browse the repository at this point in the history
Passes the correct cwd to babel's OptionManager to determine the babelrc location.

Fixes #100
  • Loading branch information
Karolis Šarapnickis authored and tleunen committed Apr 25, 2019
1 parent 0dddb1e commit f932339
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ const pkgUp = require('pkg-up');
const { resolvePath } = require('babel-plugin-module-resolver');
const { OptionManager } = require('@babel/core');

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

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

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

return instances.reduce(
(config, plugin) => ({
Expand Down Expand Up @@ -83,6 +84,7 @@ 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
9 changes: 9 additions & 0 deletions test/examples/monorepo/my-lib/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"plugins": [
["module-resolver", {
"alias": {
"~": "./src"
}
}]
]
}
13 changes: 13 additions & 0 deletions test/examples/monorepo/my-lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "my-lib",
"version": "1.0.0-test.0",
"description": "Imagine a monorepo",
"private": true,
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}
Empty file.
10 changes: 10 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,14 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});
});

describe('usage in a monorepo', () => {
it('should return `true` when mapped to a file', () => {
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'),
});
});
});
});

0 comments on commit f932339

Please sign in to comment.