From cc8ec49ca0d91ae821ea3d1cc7bd45a49d56b9aa Mon Sep 17 00:00:00 2001 From: Mathias Biilmann Christensen Date: Fri, 5 Jan 2018 18:47:01 -0800 Subject: [PATCH] Support for customizing babel and webpack This makes netlify-lambda respect any .babelrc in the folder it runs from It also adds a -c flag for passing an additional webpack config to the build or serve command --- README.md | 8 ++++++++ bin/cmd.js | 7 +++++-- lib/build.js | 35 ++++++++++++++++++++++------------- package.json | 3 ++- yarn.lock | 6 ++++++ 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index f8b6b671..937a4fde 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,14 @@ http://localhost:9000/hello -> folder/hello.js (must export a handler(event, con The `build` function will run a single build of the functions in the folder. +## 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. + +If you need to use additional webpack modules or loaders, you can specify an additional webpack config with the `-c` option when running either `serve` or `build`. + +The additional webpack config will be merged into the default config via [webpack-merge's](https://www.npmjs.com/package/webpack-merge) `merge.smart` method. + ## License [MIT](LICENSE) \ No newline at end of file diff --git a/bin/cmd.js b/bin/cmd.js index ab372c8d..e473a769 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -15,13 +15,16 @@ var serve = require("../lib/serve"); program.version(pkg.version); +program + .option("-c --config ", "additional webpack configuration") + program .command("serve ") .description("serve and watch functions") .action(function(cmd, options) { console.log("Starting server"); var server = serve.listen(9000); - build.watch(cmd, function(err, stats) { + build.watch(cmd, program.config, function(err, stats) { if (err) { console.error(err); return; @@ -41,7 +44,7 @@ program .action(function(cmd, options) { console.log("Building functions"); build - .run(cmd) + .run(cmd, program.config) .then(function(stats) { console.log(stats.toString({ color: true })); }) diff --git a/lib/build.js b/lib/build.js index eae2f2e2..307a06a4 100644 --- a/lib/build.js +++ b/lib/build.js @@ -2,9 +2,20 @@ var fs = require("fs"); var path = require("path"); var conf = require("./config"); var webpack = require("webpack"); +var merge = require("webpack-merge"); -function webpackConfig(dir) { +function webpackConfig(dir, additionalConfig) { var config = conf.load(); + var babelOpts = {cacheDirectory: true}; + if (!fs.existsSync(path.join(process.cwd(), '.babelrc'))) { + babelOpts.presets = ["es2015"]; + babelOptsplugins = [ + "transform-class-properties", + "transform-object-assign", + "transform-object-rest-spread" + ]; + } + var webpackConfig = { module: { rules: [ @@ -13,15 +24,7 @@ function webpackConfig(dir) { exclude: /(node_modules|bower_components)/, use: { loader: "babel-loader", - options: { - cacheDirectory: true, - presets: ["es2015"], - plugins: [ - "transform-class-properties", - "transform-object-assign", - "transform-object-rest-spread" - ] - } + options: babelOpts } } ] @@ -46,12 +49,18 @@ function webpackConfig(dir) { webpackConfig.entry[name] = "./" + name; } }); + if (additionalConfig) { + var webpackAdditional = require(path.join(process.cwd(), additionalConfig)); + + return merge.smart(webpackConfig, webpackAdditional); + } + return webpackConfig; } -exports.run = function(dir) { +exports.run = function(dir, additionalConfig) { return new Promise(function(resolve, reject) { - webpack(webpackConfig(dir), function(err, stats) { + webpack(webpackConfig(dir, additionalConfig), function(err, stats) { if (err) { return reject(err); } @@ -60,7 +69,7 @@ exports.run = function(dir) { }); }; -exports.watch = function(dir, cb) { +exports.watch = function(dir, additionalConfig, cb) { var compiler = webpack(webpackConfig(dir)); compiler.watch(webpackConfig(dir), cb); }; diff --git a/package.json b/package.json index 1ab81c41..a07bd441 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "express": "^4.16.2", "express-logging": "^1.1.1", "toml": "^2.3.3", - "webpack": "^3.8.1" + "webpack": "^3.8.1", + "webpack-merge": "^4.1.1" } } diff --git a/yarn.lock b/yarn.lock index 56e841b4..5113b6b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2791,6 +2791,12 @@ watchpack@^1.4.0: chokidar "^1.7.0" graceful-fs "^4.1.2" +webpack-merge@^4.1.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/webpack-merge/-/webpack-merge-4.1.1.tgz#f1197a0a973e69c6fbeeb6d658219aa8c0c13555" + dependencies: + lodash "^4.17.4" + webpack-sources@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/webpack-sources/-/webpack-sources-1.0.2.tgz#d0148ec083b3b5ccef1035a6b3ec16442983b27a"