我是webpack和无服务器的新手,所以请原谅我,如果这看起来微不足道.我从
npm serverless-webpack复制了这些代码.
我试图使用无服务器webpack –out dist,但我的命令行无法识别webpack.如果我尝试无服务器部署< 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(全球)
serverless-webpack:5.3.0(全局)
解决方法
>首先,安装webpack.
npm install --save-dev webpack
>安装插件无服务器webpack
npm install serverless-webpack --save-dev
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
>此外,使用mocha,您可以在部署之前测试代码.为此,您将配置babel
我用webpack4和无服务器为你准备一个基本的示例hello-world:
https://github.com/ns4lin4s/stackoverflow
别忘了,在apigateway中添加响应应用程序/ json:
让我知道怎么工作..