serverless-webpack
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -6,17 +6,59 @@ 'use strict'; | ||
const fse = require('fs-extra'); | ||
const glob = require('glob'); | ||
const lib = require('./index'); | ||
const _ = require('lodash'); | ||
function getEntryForFunction(serverlessFunction) { | ||
const handler = serverlessFunction.handler; | ||
const handlerFile = /(.*)\..*?$/.exec(handler)[1] + '.js'; | ||
/** | ||
* For automatic entry detection we sort the found files to solve ambiguities. | ||
* This should cover most of the cases. For complex setups the user should | ||
* build his own entries with help of the other exports. | ||
*/ | ||
const preferredExtensions = [ | ||
'.js', | ||
'.ts', | ||
'.jsx' | ||
]; | ||
// Create a valid entry key | ||
return { | ||
[handlerFile]: `./${handlerFile}` | ||
}; | ||
}; | ||
module.exports = { | ||
validate() { | ||
const getEntryExtension = fileName => { | ||
const files = glob.sync(`${fileName}.*`, { | ||
cwd: this.serverless.config.servicePath, | ||
nodir: true | ||
}); | ||
if (_.isEmpty(files)) { | ||
// If we cannot find any handler we should terminate with an error | ||
throw new this.serverless.classes.Error(`No matching handler found for '${fileName}'. Check your service definition.`); | ||
} | ||
// Move preferred file extensions to the beginning | ||
const sortedFiles = _.uniq( | ||
_.concat( | ||
_.sortBy( | ||
_.filter(files, file => _.includes(preferredExtensions, path.extname(file))), | ||
a => _.size(a) | ||
), | ||
files | ||
) | ||
); | ||
if (_.size(sortedFiles) > 1) { | ||
this.serverless.cli.log(`WARNING: More than one matching handlers found for '${fileName}'. Using '${_.first(sortedFiles)}'.`); | ||
} | ||
return path.extname(_.first(sortedFiles)); | ||
} | ||
const getEntryForFunction = serverlessFunction => { | ||
const handler = serverlessFunction.handler; | ||
const handlerFile = /(.*)\..*?$/.exec(handler)[1]; | ||
const ext = getEntryExtension(handlerFile); | ||
// Create a valid entry key | ||
return { | ||
[handlerFile]: `./${handlerFile}${ext}` | ||
}; | ||
}; | ||
this.webpackConfig = ( | ||
@@ -43,2 +85,6 @@ this.serverless.service.custom && | ||
// Expose service file and options | ||
lib.serverless = this.serverless; | ||
lib.options = this.options; | ||
if (_.isString(this.webpackConfig)) { | ||
@@ -45,0 +91,0 @@ const webpackConfigFilePath = path.join(this.serverless.config.servicePath, this.webpackConfig); |
{ | ||
"name": "serverless-webpack", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Serverless plugin to bundle your javascript with Webpack", | ||
@@ -34,2 +34,3 @@ "main": "index.js", | ||
"fs-extra": "^0.26.7", | ||
"glob": "^7.1.2", | ||
"lodash": "^4.17.4", | ||
@@ -36,0 +37,0 @@ "npm-programmatic": "0.0.5", |
@@ -54,3 +54,10 @@ # Serverless Webpack | ||
You can also let the plugin determine the correct handler entry points at build time. | ||
### serverless-webpack lib export helper | ||
serverless-webpack exposes a lib object, that can be used in your webpack.config.js | ||
to make the configuration easier and to build fully dynamic configurations. | ||
#### Automatic entry resolution | ||
You can let the plugin determine the correct handler entry points at build time. | ||
Then you do not have to care anymore when you add or remove functions from your service: | ||
@@ -85,2 +92,21 @@ | ||
#### Full customization (for experts) | ||
The lib export also provides the `serverless` and `options` properties, through | ||
which you can access the Serverless instance and the options given on the command-line. | ||
This enables you to have a fully customized dynamic configuration, that can evaluate | ||
anything available in the Serverless framework. There are really no limits. | ||
Samples are: The current stage and the complete service definition. You thereby | ||
have access to anything that a Serverless plugin would have access to. | ||
Both properties should be handled with care and should never be written to, | ||
as that will modify the running framework and leads to unpredictable behavior! | ||
If you have cool use cases with the full customization, we might add your solution | ||
to the plugin examples as showcase. | ||
### Output | ||
Note that, if the `output` configuration is not set, it will automatically be | ||
@@ -105,2 +131,4 @@ generated to write bundles in the `.webpack` directory. If you set your own `output` | ||
### Node modules / externals | ||
By default, the plugin will try to bundle all dependencies. However, you don't | ||
@@ -295,2 +323,6 @@ want to include all modules in some cases such as selectively import, excluding | ||
* 2.2.0 | ||
* Allow full dynamic configurations [#158][link-158] | ||
* Fix a bug that prevented the entries lib export to work with TypeScript [#165][link-165] | ||
* 2.1.0 | ||
@@ -348,1 +380,4 @@ * Added support for webpack configuration in TypeScript format [#129][link-129] | ||
[link-159]: https://github.com/elastic-coders/serverless-webpack/issues/159 | ||
[link-158]: https://github.com/elastic-coders/serverless-webpack/issues/158 | ||
[link-165]: https://github.com/elastic-coders/serverless-webpack/issues/165 |
379
41445
9
17
714
+ Addedglob@^7.1.2