我是webpack和无服务器的新手,所以如果这很琐碎,请原谅我。我从npm serverless-webpack复制了这些代码。
我尝试使用serverless webpack --out dist
,但是我的命令行无法识别webpack
。如果我尝试serverless deploy <opts> <opt>
,那么它将编译并捆绑到一个.serverless文件中,但是缺少基本的JS文件。
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(全局)
无服务器Webpack: 5.3.0(全局)
答案 0 :(得分:6)
首先,安装webpack。
npm install --save-dev webpack
安装无服务器插件Webpack
npm install serverless-webpack --save-dev
将插件添加到serverless.yml
service: hello-world
plugins:
- serverless-webpack
custom:
webpackIncludeModules: true
您的package.json将是以下内容:
"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"
}
然后,您可以使用以下命令进行部署: npm run deploy
我准备使用webpack4和无服务器的基本示例hello-world:
https://github.com/ns4lin4s/stackoverflow
别忘了,在apigateway中添加响应应用程序/ json:
让我知道如何工作。