webpack 输出丢弃依赖项

Posted

技术标签:

【中文标题】webpack 输出丢弃依赖项【英文标题】:webpack output dropping dependencies 【发布时间】:2019-12-11 22:12:00 【问题描述】:

我有一个使用 serverless + webpack 的节点项目。我遇到了一个问题,即我的 lambda 无法找到节点依赖项,即使它是在我的 package.json 中指定的并且它正在本地加载。

serverless.yml

service: test-lambda
package:
  individually: true
plugins:
  - serverless-webpack
  - serverless-offline
  - serverless-offline-scheduler
provider:
    name: aws
    runtime: nodejs10.x
    timeout: 300
    region: us-east-2
custom:
  webpack:
    webpackConfig: ./webpack.config.js 
    includeModules: true
functions:
  run:
    handler: src/handler.runTest
    events:
      - http:
          path: runTest
          method: post
      - http:
          path: runTest/project/name
          method: post

package.json 依赖项

  "devDependencies": 
    "@babel/cli": "^7.5.5",
    "@babel/core": "^7.5.5",
    "@babel/preset-env": "^7.5.5",
    "@babel/register": "^7.5.5",
    "babel-loader": "^8.0.6",
    "babel-plugin-source-map-support": "^2.1.1",
    "chromedriver": "^75.1.0",
    "geckodriver": "^1.16.2",
    "serverless": "^1.48.4",
    "serverless-offline": "^5.8.0",
    "serverless-offline-scheduler": "^0.3.8",
    "serverless-webpack": "^5.3.1",
    "webpack": "^4.38.0",
    "webpack-node-externals": "^1.7.2",
    "copy-webpack-plugin": "^5.0.4"
  ,
  "dependencies": 
    "aws-sdk": "^2.503.0",
    "chai": "^4.2.0",
    "@wdio/logger": "^5.11.0",
    "find-node-modules": "^2.0.0",
    "shelljs": "^0.8.3",
    "tmp": "^0.1.0",
    "@wdio/cli": "^5.11.10",
    "@wdio/sync": "^5.11.0",
    "@wdio/dot-reporter": "^5.11.0",
    "@wdio/local-runner": "^5.11.1",
    "@wdio/mocha-framework": "^5.11.0",
    "@wdio/reporter": "^5.11.7",
    "@wdio/spec-reporter": "^5.11.0",
    "wdio-chromedriver-service": "^5.0.2",
    "wdio-mochawesome-reporter": "^3.1.0",
    "wdio-json-reporter": "^1.3.1",
    "wdio-reporter": "^5.0.0-alpha.7",
    "wdio-timeline-reporter": "^5.0.10",
    "source-map-support": "0.5.12",
    "fs-extra": "^8.1.0",
    "prom-client": "^11.5.3",
    "dateformat": "^3.0.3"
  

在我的本地 node_modules 文件夹中,我按预期看到了这些 wdio 依赖文件夹,见下图:

https://user-images.githubusercontent.com/4528753/62386889-c0cfc900-b51e-11e9-9130-75e86cced74a.png

但是,当我运行serverless package 随后执行 webpack,然后检查生成的 zip 文件时,我看到 node_modules 文件夹缺少那些关键依赖项“wdio-*”,见下图:

https://user-images.githubusercontent.com/4528753/62386982-012f4700-b51f-11e9-9efa-cb61000a424e.png

其他的都在哪里?我没有看到 webpack 的输出表明它忽略了这些...

最后,这是我的 webpack.config.js 文件:

const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = 
  entry: slsw.lib.entries,
  target: 'node',
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // Since 'aws-sdk' is not compatible with webpack,
  // we exclude all node dependencies
  externals: [nodeExternals()],
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  optimization: 
    // We no not want to minimize our code.
    minimize: false,
  ,
  performance: 
    // Turn off size warnings for entry points
    hints: false,
  ,
  // Run babel on all .js files and skip those in node_modules
  module: 
    rules: [
      
        test: /\.js$/,
        loader: 'babel-loader',
        include: __dirname,
        exclude: /node_modules/,
      ,
    ],
  ,
  plugins: [
    new CopyPlugin([
      
        from: 'test/**/*',
        to: '.'
      ,
      
        from: 'reporters/**/*',
        to: '.'
      ,
      
        from: 'wdio.conf.js',
        to: 'wdio.conf.js'
      ,
    ]),
  ],
;

我在这里缺少什么? 我很肯定它与 serverless/serverless-webpack 无关,因为我可以自己运行 webpack 来检查它的输出,我发现模块丢失了。

附加数据

您正在使用的 Serverless-Webpack 版本:5.3.1 您正在使用的 Webpack 版本:4.38.0 您正在使用的无服务器框架版本:1.48.4 Node.js 版本:10.16.0 NPM 版本:6.9.0 操作系统:mac osx 堆栈跟踪(如果可用):...

【问题讨论】:

【参考方案1】:

您如何在 Lambda 函数中导入缺失的包? 你可能需要在你的serverless.ymlforce include他们

# serverless.yml
custom:
  webpack:
    includeModules:
      forceInclude:
        - module1
        - module2

【讨论】:

以上是关于webpack 输出丢弃依赖项的主要内容,如果未能解决你的问题,请参考以下文章

如何正确处理 Webpack 和 jQuery 依赖项

Webpack 在 bundle 中包含自己的依赖项

无法使用 Webpack 运行 Vuejs:未找到组件依赖项

Webpack 4 SplitChunksPlugin - 动态导入中的常见依赖项

TypeScript+Webpack 解析符号链接文件夹中的依赖项

Vue 3 和 webpack 5 - 错误:“模块属性已从依赖项中删除”