【问题标题】:Excluding node modules from Webpack on Serverless从无服务器的 Webpack 中排除节点模块
【发布时间】:2026-02-06 03:00:02
【问题描述】:

我正在阅读这篇 Medium 文章 https://medium.com/@awesome1888/how-to-use-serverless-locally-with-webpack-and-docker-5e268f71715,其中设置了具有这些依赖项的项目,

$ npm install serverless serverless-offline serverless-webpack webpack webpack-node-externals babel-loader @babel/core @babel/preset-env @babel/plugin-proposal-object-rest-spread --save-dev

这个serverless.yml 文件,

service: my-first-lambda

# enable required plugins, in order to make what we want
plugins:
  - serverless-webpack
  - serverless-offline

# serverless supports different cloud environments to run at.
# we will be deploying and running this project at AWS cloud with Node v8.10 environment
provider:
  name: aws
  runtime: nodejs8.10
  region: eu-central-1
  stage: dev

# here we describe our lambda function
functions:
  hello: # function name
    handler: src/handler.main # where the actual code is located
    # to call our function from outside, we need to expose it to the outer world
    # in order to do so, we create a REST endpoint
    events:
      - http:
          path: hello # path for the endpoint
          method: any # HTTP method for the endpoint

custom:
  webpack:
    webpackConfig: 'webpack.config.js' # name of webpack configuration file
    includeModules: true # add excluded modules to the bundle
    packager: 'npm' # package manager we use

还有这个webpack.config.js

const path = require('path');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  externals: [nodeExternals()],
  output: {
    libraryTarget: 'commonjs',
    // pay attention to this
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              // ... and this
              presets: [['@babel/env', { targets: { node: '8.10' } }]],
              plugins: [
                '@babel/plugin-proposal-object-rest-spread',
              ]
            },
          },
        ],
      },
    ],
  },
};

这似乎遵循https://github.com/serverless-heaven/serverless-webpack#node-modules--externals 中记录的模式。但我不太明白的是,为什么这不等于将includeModules 保留为默认值false?从https://www.npmjs.com/package/webpack-node-externals 看来,两者都将排除node_modules 依赖项。

【问题讨论】:

    标签: webpack serverless


    【解决方案1】:

    includeModules: false 表示所有依赖项都将成为捆绑包的一部分,从而生成一个 JavaScript 文件(没有外部依赖项)。

    externals: [nodeExternals()] 告诉 Webpack 不要捆绑外部依赖项,因此生成的 JavaScript 文件将只包含您的代码。

    由于您的代码可能需要这些外部依赖项,includeModules: true 告诉 无服务器 webpack 插件 将这些依赖项包含在 node_modules 目录下生成的 zip 包中。

    您可以尝试查看.serverless 下生成的压缩文件,以了解这些模式之间的区别。

    yaml 文件中的注释 includeModules: true # add excluded modules to the bundle 具有误导性。

    应该是includeModules: true # add excluded modules to the generated zip package

    主要是区分bundling(由Webpack完成)和packaging(由插件完成)。

    【讨论】:

    • 感谢您提供的非常好的解释!简单明了。如果你还没有写博客,你应该写博客!
    最近更新 更多