gpt4 book ai didi

webpack - 无服务器 webpack 不捆绑 handlers.js

转载 作者:行者123 更新时间:2023-12-04 08:02:04 26 4
gpt4 key购买 nike

我是 webpack 和 serverless 的新手,所以如果这看起来微不足道,请原谅我。我从 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

网络包: 4.22.0(全局)

无服务器 webpack: 5.3.0(全局)

最佳答案

  • 首先,安装 webpack。
    npm install --save-dev webpack
  • 安装插件 serverless 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 运行部署
  • 另外,还有 mocha您可以在进行部署之前测试您的代码。为此,您将拥有该配置 babel

  • 我为您准备了一个带有 webpack4 和无服务器的基本示例 hello-world:
    https://github.com/ns4lin4s/stackoverflow

    不要忘记,在apigateway中添加响应应用程序/json:

    enter image description here

    让我知道如何工作..

    关于webpack - 无服务器 webpack 不捆绑 handlers.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52950455/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com