从无服务器的 Webpack 中排除节点模块

Excluding node modules from Webpack on Serverless

我正在阅读这篇 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. What I don't quite understand though, is why this is not equivalent to just leaving includeModules at its default value of false? It seems from https://www.npmjs.com/package/webpack-node-externals 中记录的模式,两者都将排除 node_modules 依赖项。

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

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

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

您可以尝试查看 .serverless 下生成的 zip 文件以了解模式之间的差异。

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

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

主要是区分bundling(由Webpack完成)和packaging(由plugin完成)