Skip to content
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

Support for customizing babel and webpack #5

Merged
merged 1 commit into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 5 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ var serve = require("../lib/serve");

program.version(pkg.version);

program
.option("-c --config <webpack-config>", "additional webpack configuration")

program
.command("serve <dir>")
.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;
Expand All @@ -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 }));
})
Expand Down
35 changes: 22 additions & 13 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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
}
}
]
Expand All @@ -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);
}
Expand All @@ -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);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 6 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down