I am new to webpack and serverless, so please forgive me if this seems trivial. I copied these codes from npm serverless-webpack.
I tried to use serverless webpack --out dist
, but my command line didn't recognise webpack
. If I tried to serverless deploy <opts> <opt>
, then it compiled and bundled into a .serverless but missing the essential JS files.
webpack.config.js
var path = require('path');
var slsw = require('serverless-webpack');
var nodeExternals = require('webpack-node-externals');
module.exports = {
context: path.resolve(__dirname, './src'),
entry: slsw.lib.entries,
target: 'node',
externals: [nodeExternals()],
output: {
libraryTarget: 'commonjs',
path: path.resolve(__dirname, '.webpack'),
filename: '[name].js',
},
module: {
rules: [
{
test: /\.jsx$/,
loader: ["babel-loader"],
include: __dirname,
exclude: /node_modules/
}
]
}
};
Serverless.yml
service: hello-world
frameworkVersion: '>=1.2.0 <2.0.0'
provider:
name: aws
runtime: nodejs8.10
deploymentBucket:
name: test-bucket
plugin:
- serverless-webpack
- serverless-prune-plugin
custom:
prune:
automatic: true
number: 3
webpack: webpack.config.js
webpackIncludeModules:
packagePath: ./src/package.json
forceInclude:
- express
- body-parser
functions:
getHelloWorld:
handler: functions/test.hello
events:
- http:
path: test/hello
method: get
webpack: 4.22.0 (global)
serverless-webpack: 5.3.0 (global)
First, install webpack.
npm install --save-dev webpack
Install plugin serverless webpack
npm install serverless-webpack --save-dev
Add plugin to serverless.yml
service: hello-world
plugins:
- serverless-webpack
custom:
webpackIncludeModules: true
Your package.json will be something that:
"scripts": {
"test-process": "mocha --require babel-core/register ./tests/unit.test.js",
"deploy": "./node_modules/.bin/serverless remove --stage dev --region us-east-1 && ./node_modules/.bin/serverless deploy -v --stage dev --region us-east-1"
}
Then, you can deploy with this command: npm run deploy
Also, with mocha you can test your code before doing the deploy. For that, you will have that configure babel
I prepare you a basic example hello-world with webpack4 and serverless:
https://github.com/ns4lin4s/stackoverflow
Don't forget, add response application/json in apigateway:
let me know how did work..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With