Skip to content

Commit

Permalink
Resolve .babelrc files in a monorepo. (#4132)
Browse files Browse the repository at this point in the history
* Resolve .babelrc files in a monorepo.

* Resolve .babelrc files in a monorepo.

* Add a unit test.

Co-authored-by: Jasper De Moor <[email protected]>
  • Loading branch information
astegmaier and DeMoorJasper authored Feb 25, 2020
1 parent 7418b91 commit bb5ad3d
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/core/integration-tests/test/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ describe('babel', function() {
assert(file.includes('hello there'));
});

it('should support merging .babelrc and babel.config.json in a monorepo', async function() {
await bundle(
path.join(
__dirname,
'/integration/babel-config-monorepo/packages/pkg-a/src/index.js',
),
);

let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!file.includes('REPLACE_ME'));
assert(file.includes('string from a plugin in babel.config.json'));
assert(!file.includes('ANOTHER_THING_TO_REPLACE'));
assert(file.includes('string from a plugin in .babelrc'));
assert(file.includes('SOMETHING ELSE'));
assert(!file.includes('string from a plugin from a different sub-package'));
});

describe('tests needing the real filesystem', () => {
beforeEach(async () => {
await fs.rimraf(inputDir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = () => {
return {
visitor: {
StringLiteral(path, state) {
const opts = state.opts;

if (path.node.value === 'REPLACE_ME') {
path.node.value = 'string from a plugin in babel.config.json';
}
}
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["./babel-plugin-dummy-1"]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "monorepo-babel-config",
"private": true,
"workspaces": ["pkg-a", "pkg-b"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["./babel-plugin-dummy-2"]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = () => {
return {
visitor: {
StringLiteral(path, state) {
const opts = state.opts;

if (path.node.value === 'ANOTHER_THING_TO_REPLACE') {
path.node.value = 'string from a plugin in .babelrc';
}
}
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "pkg-a"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function() {
const thing1 = "REPLACE_ME";
const thing2 = "ANOTHER_THING_TO_REPLACE"
const thing3 = "SOMETHING ELSE"
return thing1 + thing2 + thing3;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"plugins": ["./babel-plugin-dummy-3"]
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = () => {
return {
visitor: {
StringLiteral(path, state) {
const opts = state.opts;

if (path.node.value === 'ANOTHER_THING_TO_REPLACE') {
path.node.value = 'string from a plugin from a different sub-package';
}
}
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "pkg-b"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function() {
const thing1 = "REPLACE_ME";
const thing2 = "ANOTHER_THING_TO_REPLACE"
const thing3 = "SOMETHING ELSE"
return thing1 + thing2 + thing3;
}
Empty file.
17 changes: 16 additions & 1 deletion packages/transformers/babel/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {PluginLogger} from '@parcel/logger';
import nullthrows from 'nullthrows';
import path from 'path';
import * as bundledBabelCore from '@babel/core';
import {md5FromObject} from '@parcel/utils';
import {md5FromObject, resolveConfig} from '@parcel/utils';

import getEnvOptions from './env';
import getJSXOptions from './jsx';
Expand All @@ -29,6 +29,20 @@ export async function load(
return buildDefaultBabelConfig(config);
}

// If we are in a monorepo, also find .babelrc configs in the sub packages.
let babelrcRoots = [options.projectRoot];
let packageJSONPath = await resolveConfig(
options.inputFS,
config.searchPath,
['package.json'],
);
if (packageJSONPath) {
let packageRoot = path.dirname(packageJSONPath);
if (packageRoot && packageRoot !== options.projectRoot) {
babelrcRoots.push(packageRoot);
}
}

let babelCore = await options.packageManager.require(
'@babel/core',
config.searchPath,
Expand All @@ -38,6 +52,7 @@ export async function load(
filename: config.searchPath,
cwd: path.dirname(config.searchPath),
root: options.projectRoot,
babelrcRoots,
});

// loadPartialConfig returns null when the file should explicitly not be run through babel (ignore/exclude)
Expand Down

0 comments on commit bb5ad3d

Please sign in to comment.