Skip to content

Commit 84fae49

Browse files
committed
fix: support babel config file names other than .babelrc
I've added find-up as a dependency for finding the babel config file. The babel config files to look for have been taken from @babel/core's sourcecode Finally, as babel-loader itself will by default not look outside of the cwd for the config file, we don't want to have false positives for any babel config files that exist outside of the cwd Closes netlify#200, netlify#188
1 parent 710819b commit 84fae49

File tree

3 files changed

+14729
-125
lines changed

3 files changed

+14729
-125
lines changed

lib/build.js

+31-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ var path = require('path');
33
var conf = require('./config');
44
var webpack = require('webpack');
55
var merge = require('webpack-merge');
6+
const findUp = require('find-up');
7+
8+
/*
9+
* Possible babel files were taken from
10+
* https://github.com/babel/babel/blob/master/packages/babel-core/src/config/files/configuration.js#L24
11+
*/
12+
13+
const BABEL_ROOT_CONFIG_FILENAMES = [
14+
'babel.config.js',
15+
'babel.config.cjs',
16+
'babel.config.mjs',
17+
'babel.config.json'
18+
];
19+
20+
const BABEL_RELATIVE_CONFIG_FILENAMES = [
21+
'.babelrc',
22+
'.babelrc.js',
23+
'.babelrc.cjs',
24+
'.babelrc.mjs',
25+
'.babelrc.json'
26+
];
627

728
const testFilePattern = '\\.(test|spec)\\.?';
829

@@ -15,25 +36,22 @@ function getBabelTarget(envConfig) {
1536
return unknown ? '8.15.0' : current.replace(/^nodejs/, '');
1637
}
1738

18-
function haveBabelrc(functionsDir) {
19-
const cwd = process.cwd();
20-
21-
return (
22-
fs.existsSync(path.join(cwd, '.babelrc')) ||
23-
functionsDir.split('/').some((dir) => {
24-
const indexOf = functionsDir.indexOf(dir);
25-
const dirToSearch = functionsDir.substr(0, indexOf);
26-
27-
return fs.existsSync(path.join(cwd, dirToSearch, '.babelrc'));
28-
})
29-
);
39+
function existsBabelConfig(functionsDir) {
40+
const babelConfigFile = findUp.sync([...BABEL_ROOT_CONFIG_FILENAMES, ...BABEL_RELATIVE_CONFIG_FILENAMES], {
41+
cwd: path.join(process.cwd(), functionsDir)
42+
});
43+
if (babelConfigFile) {
44+
// Ensure babel config files outside of working dir are not accounted for
45+
return babelConfigFile.includes(process.cwd());
46+
}
47+
return false;
3048
}
3149

3250
function webpackConfig(dir, { userWebpackConfig, useBabelrc } = {}) {
3351
var config = conf.load();
3452
var envConfig = conf.loadContext(config).environment;
3553
var babelOpts = { cacheDirectory: true };
36-
if (!haveBabelrc(dir)) {
54+
if (!existsBabelConfig(dir)) {
3755
babelOpts.presets = [
3856
[
3957
require.resolve('@babel/preset-env'),

0 commit comments

Comments
 (0)