From 9117c36d858d228130b8a6c11db2eead33107f53 Mon Sep 17 00:00:00 2001 From: Ollie Monk Date: Fri, 17 May 2019 13:30:59 +0800 Subject: [PATCH 1/3] add new flag to opt out of default babelrc loading --- .gitignore | 1 + bin/cmd.js | 13 ++++++++++++- lib/build.js | 8 ++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a6ec74b1..7bfa5e6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +.vscode/ *.swp diff --git a/bin/cmd.js b/bin/cmd.js index 16f637fd..5518c6af 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -15,9 +15,18 @@ var serve = require("../lib/serve"); program.version(pkg.version); +const stringBooleanToBoolean = val => { + if (typeof val !== 'string' && (val !== 'true' || val !== 'false')) { + throw Error(`Incorrect string value: ${val}`); + } + + return val === 'true'; +}; + program .option("-c --config ", "additional webpack configuration") .option("-p --port ", "port to serve from (default: 9000)") + .option("-b --babelrc ", "use .babelrc in root (default: true)", stringBooleanToBoolean) .option( "-t --timeout ", "function invocation timeout in seconds (default: 10)" @@ -57,8 +66,10 @@ program .description("build functions") .action(function(cmd, options) { console.log("netlify-lambda: Building functions"); + + const { config: userWebpackConfig, babelrc: useBabelrc = true} = program; build - .run(cmd, program.config) + .run(cmd, { userWebpackConfig, useBabelrc}) .then(function(stats) { console.log(stats.toString({ color: true })); }) diff --git a/lib/build.js b/lib/build.js index c64d695c..9b5a7824 100644 --- a/lib/build.js +++ b/lib/build.js @@ -31,7 +31,7 @@ function haveBabelrc(functionsDir) { ); } -function webpackConfig(dir, additionalConfig) { +function webpackConfig(dir, {userWebpackConfig, useBabelrc}) { var config = conf.load(); var envConfig = conf.loadContext(config).environment; var babelOpts = { cacheDirectory: true }; @@ -88,7 +88,7 @@ function webpackConfig(dir, additionalConfig) { ), use: { loader: require.resolve('babel-loader'), - options: babelOpts + options: {...babelOpts, babelrc: useBabelrc} } } ] @@ -129,8 +129,8 @@ function webpackConfig(dir, additionalConfig) { ` ); } - if (additionalConfig) { - var webpackAdditional = require(path.join(process.cwd(), additionalConfig)); + if (userWebpackConfig) { + var webpackAdditional = require(path.join(process.cwd(), userWebpackConfig)); return merge.smart(webpackConfig, webpackAdditional); } From c6d266386be88e4ac24174f9497c947cb8f55a27 Mon Sep 17 00:00:00 2001 From: Ollie Monk Date: Fri, 17 May 2019 13:37:00 +0800 Subject: [PATCH 2/3] update readme --- README.md | 11 ++++++++++- bin/cmd.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d450d62..ada70705 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,9 @@ 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 `.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. + +It is possible to disable this behaviour by passing `--babelrc false`. If you need to run different babel versions for your lambda and for your app, [check this issue](https://github.com/netlify/netlify-lambda/issues/34) to override your webpack babel-loader. @@ -393,6 +395,7 @@ There are additional CLI options: -p --port -s --static -t --timeout +-b --babelrc ``` ### --config option @@ -422,6 +425,12 @@ The serving port can be changed with the `-p`/`--port` option. If you need an escape hatch and are building your lambda in some way that is incompatible with our build process, you can skip the build with the `-s` or `--static` flag. [More info here](https://github.com/netlify/netlify-lambda/pull/62). +### --babelrc + +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) + ## Netlify Identity Make sure to [read the docs](https://www.netlify.com/docs/functions/#identity-and-functions) on how Netlify Functions and Netlify Identity work together. Basically you have to make your request with an `authorization` header and a `Bearer` token with your Netlify Identity JWT supplied. You can get this JWT from any of our Identity solutions from [gotrue-js](https://github.com/netlify/gotrue-js) to [netlify-identity-widget](https://github.com/netlify/netlify-identity-widget). diff --git a/bin/cmd.js b/bin/cmd.js index 5518c6af..23ff5298 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -26,7 +26,7 @@ const stringBooleanToBoolean = val => { program .option("-c --config ", "additional webpack configuration") .option("-p --port ", "port to serve from (default: 9000)") - .option("-b --babelrc ", "use .babelrc in root (default: true)", stringBooleanToBoolean) + .option("-b --babelrc babelrc>", "use .babelrc in root (default: true)", stringBooleanToBoolean) .option( "-t --timeout ", "function invocation timeout in seconds (default: 10)" From 0fd4b187def6efb7d526f321ffbd61d51852d751 Mon Sep 17 00:00:00 2001 From: Ollie Monk Date: Fri, 17 May 2019 13:47:07 +0800 Subject: [PATCH 3/3] add missing < --- bin/cmd.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index 23ff5298..23460aa8 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -16,6 +16,7 @@ var serve = require("../lib/serve"); program.version(pkg.version); const stringBooleanToBoolean = val => { + console.log({val}); if (typeof val !== 'string' && (val !== 'true' || val !== 'false')) { throw Error(`Incorrect string value: ${val}`); } @@ -26,7 +27,7 @@ const stringBooleanToBoolean = val => { program .option("-c --config ", "additional webpack configuration") .option("-p --port ", "port to serve from (default: 9000)") - .option("-b --babelrc babelrc>", "use .babelrc in root (default: true)", stringBooleanToBoolean) + .option("-b --babelrc ", "use .babelrc in root (default: true)", stringBooleanToBoolean) .option( "-t --timeout ", "function invocation timeout in seconds (default: 10)"