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

Kur*_*eek 2 webpack 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
Run Code Online (Sandbox Code Playgroud)

这个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
Run Code Online (Sandbox Code Playgroud)

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',
              ]
            },
          },
        ],
      },
    ],
  },
};
Run Code Online (Sandbox Code Playgroud)

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

Ere*_*rez 9

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

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

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

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

注释includeModules: true # add excluded modules to the bundle将在yaml文件被误导。

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

主要是区分打包(由 Webpack 完成)和打包(由插件完成)。

  • 感谢您的很好的解释!简单明了。如果你还没有的话,你应该写博客! (2认同)