Skip to content

Commit 19caf69

Browse files
authored
fix(babel): Update to latest babel version and fix babelrc behaviour (#583)
This removes the custom behaviour in babel-loader of the option `babelrc`. It now cannot be a string anymore and should be boolean and instead extends should be used. BREAKING CHANGE: Option `babelrc` needs to be boolean now. String values are not allowed anymore. Use `extends` option to specify a specific config file. A warning will be triggered when setting babelrc to a string.
1 parent 7bf66bd commit 19caf69

File tree

8 files changed

+984
-987
lines changed

8 files changed

+984
-987
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"webpack": ">=2"
2020
},
2121
"devDependencies": {
22-
"@babel/cli": "7.0.0-beta.5",
23-
"@babel/core": "7.0.0-beta.5",
24-
"@babel/preset-env": "7.0.0-beta.5",
22+
"@babel/cli": "^7.0.0-beta.40",
23+
"@babel/core": "^7.0.0-beta.40",
24+
"@babel/preset-env": "^7.0.0-beta.40",
2525
"ava": "0.25.0",
2626
"babel-eslint": "^8.0.0",
2727
"babel-plugin-istanbul": "^4.0.0",

src/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,21 @@ module.exports = function(source, inputSourceMap) {
113113
const loaderOptions = loaderUtils.getOptions(this) || {};
114114
const fileSystem = this.fs ? this.fs : fs;
115115
let babelrcPath = null;
116-
if (loaderOptions.babelrc !== false) {
117-
babelrcPath =
118-
typeof loaderOptions.babelrc === "string" &&
119-
exists(fileSystem, loaderOptions.babelrc)
120-
? loaderOptions.babelrc
121-
: resolveRc(fileSystem, path.dirname(filename));
116+
117+
// Deprecation handling
118+
if (typeof loaderOptions.babelrc === "string") {
119+
console.warn(
120+
"The option `babelrc` should not be set to a string anymore in the babel-loader config. " +
121+
"Please update your configuration and set `babelrc` to true or false.\n" +
122+
"If you want to specify a specific babel config file to inherit config from " +
123+
"please use the `extends` option.\nFor more information about this options see " +
124+
"https://babeljs.io/docs/core-packages/#options",
125+
);
126+
}
127+
if (loaderOptions.babelrc !== false && loaderOptions.extends) {
128+
babelrcPath = exists(fileSystem, loaderOptions.extends)
129+
? loaderOptions.extends
130+
: resolveRc(fileSystem, path.dirname(filename));
122131
}
123132

124133
if (babelrcPath) {
@@ -134,6 +143,7 @@ module.exports = function(source, inputSourceMap) {
134143
"@babel/loader": pkg.version,
135144
"@babel/core": babel.version,
136145
babelrc: babelrcPath ? read(fileSystem, babelrcPath) : null,
146+
options: loaderOptions,
137147
env:
138148
loaderOptions.forceEnv ||
139149
process.env.BABEL_ENV ||

test/cache.test.js

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const babelLoader = path.join(__dirname, "../lib");
1616
const globalConfig = {
1717
entry: path.join(__dirname, "fixtures/basic.js"),
1818
module: {
19-
loaders: [
19+
rules: [
2020
{
2121
test: /\.js$/,
2222
loader: babelLoader,
@@ -53,12 +53,12 @@ test.cb("should output files to cache directory", t => {
5353
path: t.context.directory,
5454
},
5555
module: {
56-
loaders: [
56+
rules: [
5757
{
5858
test: /\.js$/,
5959
loader: babelLoader,
6060
exclude: /node_modules/,
61-
query: {
61+
options: {
6262
cacheDirectory: t.context.cacheDirectory,
6363
presets: ["@babel/preset-env"],
6464
},
@@ -67,8 +67,10 @@ test.cb("should output files to cache directory", t => {
6767
},
6868
});
6969

70-
webpack(config, err => {
70+
webpack(config, (err, stats) => {
7171
t.is(err, null);
72+
t.deepEqual(stats.compilation.errors, []);
73+
t.deepEqual(stats.compilation.warnings, []);
7274

7375
fs.readdir(t.context.cacheDirectory, (err, files) => {
7476
t.is(err, null);
@@ -86,12 +88,12 @@ test.cb.serial(
8688
path: t.context.directory,
8789
},
8890
module: {
89-
loaders: [
91+
rules: [
9092
{
9193
test: /\.jsx?/,
9294
loader: babelLoader,
9395
exclude: /node_modules/,
94-
query: {
96+
options: {
9597
cacheDirectory: true,
9698
presets: ["@babel/preset-env"],
9799
},
@@ -100,8 +102,10 @@ test.cb.serial(
100102
},
101103
});
102104

103-
webpack(config, err => {
105+
webpack(config, (err, stats) => {
104106
t.is(err, null);
107+
t.deepEqual(stats.compilation.errors, []);
108+
t.deepEqual(stats.compilation.warnings, []);
105109

106110
fs.readdir(defaultCacheDir, (err, files) => {
107111
files = files.filter(file => /\b[0-9a-f]{5,40}\.json\.gz\b/.test(file));
@@ -122,7 +126,7 @@ test.cb.serial(
122126
path: t.context.directory,
123127
},
124128
module: {
125-
loaders: [
129+
rules: [
126130
{
127131
test: /\.jsx?/,
128132
loader: `${babelLoader}?cacheDirectory=true&presets[]=@babel/preset-env`,
@@ -132,8 +136,10 @@ test.cb.serial(
132136
},
133137
});
134138

135-
webpack(config, err => {
139+
webpack(config, (err, stats) => {
136140
t.is(err, null);
141+
t.deepEqual(stats.compilation.errors, []);
142+
t.deepEqual(stats.compilation.warnings, []);
137143

138144
fs.readdir(defaultCacheDir, (err, files) => {
139145
files = files.filter(file => /\b[0-9a-f]{5,40}\.json\.gz\b/.test(file));
@@ -153,12 +159,12 @@ test.cb.skip("should read from cache directory if cached file exists", t => {
153159
path: t.context.directory,
154160
},
155161
module: {
156-
loaders: [
162+
rules: [
157163
{
158164
test: /\.jsx?/,
159165
loader: babelLoader,
160166
exclude: /node_modules/,
161-
query: {
167+
options: {
162168
cacheDirectory: t.context.cacheDirectory,
163169
presets: ["@babel/preset-env"],
164170
},
@@ -169,8 +175,10 @@ test.cb.skip("should read from cache directory if cached file exists", t => {
169175

170176
// @TODO Find a way to know if the file as correctly read without relying on
171177
// Istanbul for coverage.
172-
webpack(config, err => {
178+
webpack(config, (err, stats) => {
173179
t.is(err, null);
180+
t.deepEqual(stats.compilation.errors, []);
181+
t.deepEqual(stats.compilation.warnings, []);
174182

175183
webpack(config, err => {
176184
t.is(err, null);
@@ -189,12 +197,12 @@ test.cb("should have one file per module", t => {
189197
path: t.context.directory,
190198
},
191199
module: {
192-
loaders: [
200+
rules: [
193201
{
194202
test: /\.jsx?/,
195203
loader: babelLoader,
196204
exclude: /node_modules/,
197-
query: {
205+
options: {
198206
cacheDirectory: t.context.cacheDirectory,
199207
presets: ["@babel/preset-env"],
200208
},
@@ -203,8 +211,10 @@ test.cb("should have one file per module", t => {
203211
},
204212
});
205213

206-
webpack(config, err => {
214+
webpack(config, (err, stats) => {
207215
t.is(err, null);
216+
t.deepEqual(stats.compilation.errors, []);
217+
t.deepEqual(stats.compilation.warnings, []);
208218

209219
fs.readdir(t.context.cacheDirectory, (err, files) => {
210220
t.is(err, null);
@@ -221,12 +231,12 @@ test.cb("should generate a new file if the identifier changes", t => {
221231
path: t.context.directory,
222232
},
223233
module: {
224-
loaders: [
234+
rules: [
225235
{
226236
test: /\.jsx?/,
227237
loader: babelLoader,
228238
exclude: /node_modules/,
229-
query: {
239+
options: {
230240
cacheDirectory: t.context.cacheDirectory,
231241
cacheIdentifier: "a",
232242
presets: ["@babel/preset-env"],
@@ -240,12 +250,12 @@ test.cb("should generate a new file if the identifier changes", t => {
240250
path: t.context.directory,
241251
},
242252
module: {
243-
loaders: [
253+
rules: [
244254
{
245255
test: /\.jsx?/,
246256
loader: babelLoader,
247257
exclude: /node_modules/,
248-
query: {
258+
options: {
249259
cacheDirectory: t.context.cacheDirectory,
250260
cacheIdentifier: "b",
251261
presets: ["@babel/preset-env"],
@@ -258,8 +268,10 @@ test.cb("should generate a new file if the identifier changes", t => {
258268
let counter = configs.length;
259269

260270
configs.forEach(config => {
261-
webpack(config, err => {
271+
webpack(config, (err, stats) => {
262272
t.is(err, null);
273+
t.deepEqual(stats.compilation.errors, []);
274+
t.deepEqual(stats.compilation.warnings, []);
263275
counter -= 1;
264276

265277
if (!counter) {
@@ -281,14 +293,15 @@ test.cb("should allow to specify the .babelrc file", t => {
281293
path: t.context.directory,
282294
},
283295
module: {
284-
loaders: [
296+
rules: [
285297
{
286298
test: /\.jsx?/,
287299
loader: babelLoader,
288300
exclude: /node_modules/,
289-
query: {
301+
options: {
290302
cacheDirectory: t.context.cacheDirectory,
291-
babelrc: path.join(__dirname, "fixtures/babelrc"),
303+
extends: path.join(__dirname, "fixtures/babelrc"),
304+
babelrc: false,
292305
presets: ["@babel/preset-env"],
293306
},
294307
},
@@ -301,12 +314,12 @@ test.cb("should allow to specify the .babelrc file", t => {
301314
path: t.context.directory,
302315
},
303316
module: {
304-
loaders: [
317+
rules: [
305318
{
306319
test: /\.jsx?/,
307320
loader: babelLoader,
308321
exclude: /node_modules/,
309-
query: {
322+
options: {
310323
cacheDirectory: t.context.cacheDirectory,
311324
presets: ["@babel/preset-env"],
312325
},
@@ -316,8 +329,12 @@ test.cb("should allow to specify the .babelrc file", t => {
316329
}),
317330
];
318331

319-
webpack(config, err => {
332+
webpack(config, (err, multiStats) => {
320333
t.is(err, null);
334+
t.deepEqual(multiStats.stats[0].compilation.errors, []);
335+
t.deepEqual(multiStats.stats[0].compilation.warnings, []);
336+
t.deepEqual(multiStats.stats[1].compilation.errors, []);
337+
t.deepEqual(multiStats.stats[1].compilation.warnings, []);
321338

322339
fs.readdir(t.context.cacheDirectory, (err, files) => {
323340
t.is(err, null);

test/loader.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ const babelLoader = path.join(__dirname, "../lib");
1010
const globalConfig = {
1111
entry: path.join(__dirname, "fixtures/basic.js"),
1212
module: {
13-
loaders: [
13+
rules: [
1414
{
1515
test: /\.jsx?/,
1616
loader: babelLoader,
17-
query: {
17+
options: {
1818
presets: ["@babel/preset-env"],
1919
},
2020
exclude: /node_modules/,
@@ -84,11 +84,11 @@ test.cb("should use correct env", t => {
8484
path: t.context.directory,
8585
},
8686
module: {
87-
loaders: [
87+
rules: [
8888
{
8989
test: /\.jsx?/,
9090
loader: babelLoader,
91-
query: {
91+
options: {
9292
forceEnv: "testenv",
9393
env: {
9494
testenv: {
@@ -126,11 +126,11 @@ test.serial.cb("should not polute BABEL_ENV after using forceEnv", t => {
126126
path: t.context.directory,
127127
},
128128
module: {
129-
loaders: [
129+
rules: [
130130
{
131131
test: /\.jsx?/,
132132
loader: babelLoader,
133-
query: {
133+
options: {
134134
forceEnv: "testenv",
135135
env: {
136136
testenv: {
@@ -161,11 +161,11 @@ test.serial.cb(
161161
path: t.context.directory,
162162
},
163163
module: {
164-
loaders: [
164+
rules: [
165165
{
166166
test: /\.jsx?/,
167167
loader: babelLoader,
168-
query: {
168+
options: {
169169
forceEnv: "testenv",
170170
env: {
171171
testenv: {
@@ -197,11 +197,11 @@ test.serial.cb("should not change BABEL_ENV when using forceEnv", t => {
197197
path: t.context.directory,
198198
},
199199
module: {
200-
loaders: [
200+
rules: [
201201
{
202202
test: /\.jsx?/,
203203
loader: babelLoader,
204-
query: {
204+
options: {
205205
forceEnv: "testenv",
206206
env: {
207207
testenv: {
@@ -245,7 +245,7 @@ test.cb("should not throw without config", t => {
245245
path: t.context.directory,
246246
},
247247
module: {
248-
loaders: [
248+
rules: [
249249
{
250250
test: /\.jsx?/,
251251
loader: babelLoader,

test/metadata.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const globalConfig = {
1919
},
2020
plugins: [new ReactIntlPlugin()],
2121
module: {
22-
loaders: [
22+
rules: [
2323
{
2424
test: /\.jsx?/,
2525
loader: babelLoader,
26-
query: {
26+
options: {
2727
metadataSubscribers: [ReactIntlPlugin.metadataContextFunctionName],
2828
plugins: [["react-intl", { enforceDescriptions: false }]],
2929
presets: [],
@@ -114,11 +114,11 @@ test.cb("should pass metadata code snippet ( cache version )", t => {
114114
filename: "[id].metadata.js",
115115
},
116116
module: {
117-
loaders: [
117+
rules: [
118118
{
119119
test: /\.jsx?/,
120120
loader: babelLoader,
121-
query: {
121+
options: {
122122
metadataSubscribers: [ReactIntlPlugin.metadataContextFunctionName],
123123
plugins: [["react-intl", { enforceDescriptions: false }]],
124124
cacheDirectory: cacheDir,

0 commit comments

Comments
 (0)