Skip to content

Commit a01e148

Browse files
Simon Mollweidejantimon
authored andcommitted
fix(js-config-webpack-plugin): find, register and invoke correct babel configs
1 parent a855ac5 commit a01e148

File tree

8 files changed

+144
-9
lines changed

8 files changed

+144
-9
lines changed

packages/js-config-webpack-plugin/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
"name": "Jan Nicklas",
3434
"email": "j.nicklas@me.com",
3535
"url": "https://github.com/jantimon"
36+
},
37+
{
38+
"name": "Simon Mollweide",
39+
"email": "simon.mollweide@gmail.com",
40+
"url": "https://github.com/smollweide"
3641
}
3742
],
3843
"engines": {

packages/js-config-webpack-plugin/src/JsConfigWebpackPlugin.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
'use strict';
1212
const path = require('path');
13-
const findRelativeConfig = require('@babel/core/lib/config/files/configuration').findRelativeConfig;
1413
const findPackageData = require('@babel/core/lib/config/files/package').findPackageData;
14+
const findRelativeConfig = require('@babel/core/lib/config/files/configuration').findRelativeConfig;
15+
const findRootConfig = require('@babel/core/lib/config/files/configuration').findRootConfig;
1516

1617
class JsConfigWebpackPlugin {
1718
/**
@@ -30,11 +31,20 @@ class JsConfigWebpackPlugin {
3031
resolveBabelConfigFilePath(contextPath, environmentName) {
3132
// From https://github.com/babel/babel/blob/52a569056c6008c453bf26219461655c7d0322c4/packages/babel-core/src/config/files/package.js#L15
3233
const packageData = findPackageData(contextPath);
34+
// needed because babels `findRelativeConfig` search just in parent directories
35+
packageData.directories.push(packageData.filepath);
3336
// From https://github.com/babel/babel/blob/52a569056c6008c453bf26219461655c7d0322c4/packages/babel-core/src/config/files/configuration.js#L26
34-
const resolvedFilePath = findRelativeConfig(packageData, environmentName);
37+
const resolvedRelativeConfig = findRelativeConfig(packageData, environmentName);
38+
const resolvedRootConfig = findRootConfig(packageData.filepath);
39+
40+
// babel.config.js
41+
if (resolvedRootConfig && resolvedRootConfig.filepath) {
42+
return resolvedRootConfig.filepath;
43+
}
3544

36-
if (resolvedFilePath.config) {
37-
return resolvedFilePath.config.filepath;
45+
// .babelrc.js and .babelrc
46+
if (resolvedRelativeConfig && resolvedRelativeConfig.config) {
47+
return resolvedRelativeConfig.config.filepath;
3848
}
3949

4050
console.warn(

packages/js-config-webpack-plugin/test/JsConfigWebpackPlugin.test.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ describe('JsConfigWebpackPlugin inside context', () => {
114114
it('should have the correct babelConfigFile option in development mode', (done) => {
115115
const webpackContext = path.join(__dirname, 'fixtures/babel');
116116
const babelConfigFileContext = path.join(__dirname, '../config');
117-
const babelConfigFileName = '.babelrc.base.json';
117+
const babelConfigFileName = '.babelrc';
118118

119119
const instance = webpack({
120120
mode: 'development',
@@ -125,14 +125,14 @@ describe('JsConfigWebpackPlugin inside context', () => {
125125
const ruleToTest = instance.options.module.rules[0];
126126
const ruleOptions = ruleToTest.use[1].options;
127127

128-
expect(ruleOptions.extends).toEqual(path.resolve(babelConfigFileContext, babelConfigFileName));
128+
expect(ruleOptions.extends).toEqual(path.resolve(webpackContext, babelConfigFileName));
129129
done();
130130
});
131131

132132
it('should have the correct babelConfigFile option in production mode', (done) => {
133133
const webpackContext = path.join(__dirname, 'fixtures/babel');
134134
const babelConfigFileContext = path.join(__dirname, '../config');
135-
const babelConfigFileName = '.babelrc.base.json';
135+
const babelConfigFileName = '.babelrc';
136136

137137
const instance = webpack({
138138
mode: 'production',
@@ -143,10 +143,46 @@ describe('JsConfigWebpackPlugin inside context', () => {
143143
const ruleToTest = instance.options.module.rules[0];
144144
const ruleOptions = ruleToTest.use[1].options;
145145

146+
expect(ruleOptions.extends).toEqual(path.resolve(webpackContext, babelConfigFileName));
147+
done();
148+
});
149+
150+
it('should load the babel fallback config', (done) => {
151+
const webpackContext = path.join(__dirname, 'fixtures/babel-no-babel');
152+
const babelConfigFileContext = path.join(__dirname, '../config');
153+
const babelConfigFileName = '.babelrc.base.json';
154+
155+
const instance = webpack({
156+
mode: 'development',
157+
context: webpackContext,
158+
plugins: [new JsConfigWebpackPlugin()],
159+
});
160+
161+
const ruleToTest = instance.options.module.rules[0];
162+
const ruleOptions = ruleToTest.use[1].options;
163+
146164
expect(ruleOptions.extends).toEqual(path.resolve(babelConfigFileContext, babelConfigFileName));
147165
done();
148166
});
149167

168+
it('should load the root babel config', (done) => {
169+
const webpackContext = path.join(__dirname, 'fixtures/babel-root-babel');
170+
const babelConfigFileContext = path.join(__dirname, '../config');
171+
const babelConfigFileName = 'babel.config.js';
172+
173+
const instance = webpack({
174+
mode: 'development',
175+
context: webpackContext,
176+
plugins: [new JsConfigWebpackPlugin()],
177+
});
178+
179+
const ruleToTest = instance.options.module.rules[0];
180+
const ruleOptions = ruleToTest.use[1].options;
181+
182+
expect(ruleOptions.extends).toEqual(path.resolve(webpackContext, babelConfigFileName));
183+
done();
184+
});
185+
150186
it('should allow to pass the babelConfigFile option in development mode', (done) => {
151187
const webpackContext = path.join(__dirname, 'fixtures/babel');
152188
const babelConfigFilePath = path.join(webpackContext, '.babelrc');
@@ -205,7 +241,7 @@ describe('JsConfigWebpackPlugin inside context', () => {
205241
it('the correct base .babelrc file should be registered and invoked', (done) => {
206242
const webpackContext = path.join(__dirname, 'fixtures/babel');
207243
const babelConfigFileContext = path.join(__dirname, '../config');
208-
const babelConfigFileName = '.babelrc.base.json';
244+
const babelConfigFileName = '.babelrc';
209245

210246
const compiler = webpack({
211247
mode: 'production',
@@ -218,7 +254,7 @@ describe('JsConfigWebpackPlugin inside context', () => {
218254

219255
// should not fail because base-file of babelrc has no errors
220256
compiler.run((_, stats) => {
221-
expect(ruleOptions.extends).toBe(path.resolve(babelConfigFileContext, babelConfigFileName));
257+
expect(ruleOptions.extends).toBe(path.resolve(webpackContext, babelConfigFileName));
222258
expect(stats.compilation.errors).toEqual([]);
223259
done();
224260
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"description": "package-file-for-babel",
3+
"version": "0.1.0",
4+
"author": "Namics AG - Technology & Application",
5+
"license": "MIT",
6+
"main": "./src/index.js"
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// classes
2+
class Plugin {
3+
constructor(name) {
4+
this.name = name;
5+
}
6+
7+
run() {
8+
console.log(`${name} is running!`);
9+
}
10+
}
11+
12+
// arrow functions
13+
const main = () => {
14+
const pluginOne = new Plugin('One');
15+
pluginOne.run();
16+
};
17+
18+
main();
19+
20+
//map with pow
21+
[1, 2, 3].map((number) => number ** 2);
22+
23+
// shorthand Object method
24+
var obj = {
25+
shorthand,
26+
test() {
27+
return 'test';
28+
},
29+
};
30+
31+
// spread
32+
const arr = [1, 2, 3, 4, 5];
33+
const spreadArr = [...arr];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: ['@babel/env'],
3+
ignore: ['foo.js', 'bar/**/*.js'],
4+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"description": "package-file-for-babel",
3+
"version": "0.1.0",
4+
"author": "Namics AG - Technology & Application",
5+
"license": "MIT",
6+
"main": "./src/index.js"
7+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// classes
2+
class Plugin {
3+
constructor(name) {
4+
this.name = name;
5+
}
6+
7+
run() {
8+
console.log(`${name} is running!`);
9+
}
10+
}
11+
12+
// arrow functions
13+
const main = () => {
14+
const pluginOne = new Plugin('One');
15+
pluginOne.run();
16+
};
17+
18+
main();
19+
20+
//map with pow
21+
[1, 2, 3].map((number) => number ** 2);
22+
23+
// shorthand Object method
24+
var obj = {
25+
shorthand,
26+
test() {
27+
return 'test';
28+
},
29+
};
30+
31+
// spread
32+
const arr = [1, 2, 3, 4, 5];
33+
const spreadArr = [...arr];

0 commit comments

Comments
 (0)