Skip to content

fix: support babel config file names other than .babelrc #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ Just remember to configure your `netlify.toml` to point to the `Next.js` build f

## Webpack Configuration

By default the webpack configuration uses `babel-loader` to load all js files. Any `.babelrc` in the directory `netlify-lambda` is run from will be respected. If no `.babelrc` is found, a [few basic settings are used](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L11-L15a).
By default the webpack configuration uses `babel-loader` to load all js files.
`netlify-lambda` will search for [a valid babel config file](https://babeljs.io/docs/en/config-files) in the functions directory first and look upwards up to the directory `netlify-lambda` is run from (similar to how `babel-loader` looks for a Babel config file).
If no babel config file is found, a [few basic settings are used](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L11-L15a).

If you need to use additional webpack modules or loaders, you can specify an additional webpack config with the `-c`/`--config` option when running either `serve` or `build`.

Expand Down Expand Up @@ -383,7 +385,7 @@ The additional webpack config will be merged into the default config via [webpac

The default webpack configuration uses `babel-loader` with a [few basic settings](https://github.com/netlify/netlify-lambda/blob/master/lib/build.js#L19-L33).

However, if any `.babelrc` is found in the directory `netlify-lambda` is run from, or [folders above it](https://github.com/netlify/netlify-lambda/pull/92) (useful for monorepos), it will be used instead of the default one.
However, if any valid Babel config file is found in the directory `netlify-lambda` is run from, or [folders above it](https://github.com/netlify/netlify-lambda/pull/92) (useful for monorepos), it will be used instead of the default one.

It is possible to disable this behaviour by passing `--babelrc false`.

Expand All @@ -401,7 +403,7 @@ npm install --save-dev @babel/preset-typescript

You may also want to add `typescript @types/node @types/aws-lambda`.

2. Create a custom `.babelrc` file:
2. Create a Babel config file, e.g. `.babelrc`:

```diff
{
Expand Down Expand Up @@ -465,7 +467,7 @@ If you need an escape hatch and are building your lambda in some way that is inc

Defaults to `true`

Use a `.babelrc` found in the directory `netlify-lambda` is run from. This can be useful when you have conflicting babel-presets, more info [here](#babel-configuration)
Use a Babel config file found in the directory `netlify-lambda` is run from. This can be useful when you have conflicting babel-presets, more info [here](#babel-configuration)

## Netlify Identity

Expand Down
45 changes: 33 additions & 12 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ var path = require('path');
var conf = require('./config');
var webpack = require('webpack');
var merge = require('webpack-merge');
const findUp = require('find-up');

/*
* Possible babel files were taken from
* https://github.com/babel/babel/blob/master/packages/babel-core/src/config/files/configuration.js#L24
*/

const BABEL_ROOT_CONFIG_FILENAMES = [
'babel.config.js',
'babel.config.cjs',
'babel.config.mjs',
'babel.config.json',
];

const BABEL_RELATIVE_CONFIG_FILENAMES = [
'.babelrc',
'.babelrc.js',
'.babelrc.cjs',
'.babelrc.mjs',
'.babelrc.json',
];

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

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

function haveBabelrc(functionsDir) {
const cwd = process.cwd();

return (
fs.existsSync(path.join(cwd, '.babelrc')) ||
functionsDir.split('/').some((dir) => {
const indexOf = functionsDir.indexOf(dir);
const dirToSearch = functionsDir.substr(0, indexOf);

return fs.existsSync(path.join(cwd, dirToSearch, '.babelrc'));
})
function existsBabelConfig(functionsDir) {
const babelConfigFile = findUp.sync(
[...BABEL_ROOT_CONFIG_FILENAMES, ...BABEL_RELATIVE_CONFIG_FILENAMES],
{
cwd: path.join(process.cwd(), functionsDir),
},
);
if (babelConfigFile) {
// Ensure babel config files outside of working dir are not accounted for
return babelConfigFile.includes(process.cwd());
Copy link
Contributor

@erezrokah erezrokah Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work for monorepos when the babel config is in the root directory?
We could find-up a .git and use that value (or cwd if it doesn't exist).

We might also want to use a matcher and findUp.stop to stop the search if we detect we're outside the root.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would make sense to search outside the cwd. Babel will by default also not look outside the cwd for a config file.

Do you imagine a scenario in a monorepo where the user runs netlify-lambda from a subdir while they want to use the babel config in the root dir?

}
return false;
}

function webpackConfig(dir, { userWebpackConfig, useBabelrc } = {}) {
var config = conf.load();
var envConfig = conf.loadContext(config).environment;
var babelOpts = { cacheDirectory: true };
if (!haveBabelrc(dir)) {
if (!existsBabelConfig(dir)) {
babelOpts.presets = [
[
require.resolve('@babel/preset-env'),
Expand Down
Loading