1

serverless deploy使用最新的 webpack 版本抛出此错误。

Serverless: Bundling with Webpack...

  Webpack Options Validation Error -----------------------

  WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.entry should be a non-empty object.

webpack.config.js


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

    module.exports = {
      mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
      entry: slsw.lib.entries,
      // externals: [nodeExternals()],
      devtool: 'source-map',
      resolve: {
        extensions: ['.js', '.jsx', '.json', '.ts', '.tsx'],
      },
      output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, '.webpack'),
        filename: '[name].js',
      },
      target: 'node',
      module: {
        rules: [
          // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
          { test: /\.tsx?$/, loader: 'ts-loader' },
        ],
      },
    };

包.json


    "serverless": "^1.72.0",
    "serverless-aws-documentation": "^1.1.0",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-iam-roles-per-function": "^2.0.2",
    "serverless-offline": "^6.4.0",
    "serverless-reqvalidator-plugin": "^1.0.3",
    "serverless-s3-local": "^0.6.2",
    "serverless-webpack": "^5.3.2",
    "ts-loader": "^7.0.5",
    "typescript": "^3.9.5",
    "webpack": "^4.43.0"

这里有一些相关的东西:https ://github.com/serverless-heaven/serverless-webpack/issues/372

serverless.yml 是非常标准的,并且可以使用较旧的 webpack 版本。不是这里的 webpack 专家,非常感谢任何帮助。

4

1 回答 1

0

解决方案是从头开始重新创建项目并重新安装包:
sls create --template aws-nodejs-typescript --path projectname

在这种情况下,webpack.config.json 生成如下所示:

const path = require('path');
const slsw = require('serverless-webpack');
const nodeExternals = require('webpack-node-externals');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = {
  context: __dirname,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  entry: slsw.lib.entries,
  devtool: slsw.lib.webpack.isLocal ? 'cheap-module-eval-source-map' : 'source-map',
  resolve: {
    extensions: ['.mjs', '.json', '.ts'],
    symlinks: false,
    cacheWithContext: false,
  },
  output: {
    libraryTarget: 'commonjs',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
  },
  target: 'node',
  externals: [nodeExternals()],
  module: {
    rules: [
      // all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
      {
        test: /\.(tsx?)$/,
        loader: 'ts-loader',
        exclude: [
          [
            path.resolve(__dirname, 'node_modules'),
            path.resolve(__dirname, '.serverless'),
            path.resolve(__dirname, '.webpack'),
          ],
        ],
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
        },
      },
    ],
  },
  plugins: [
    // new ForkTsCheckerWebpackPlugin({
    //   eslint: true,
    //   eslintOptions: {
    //     cache: true
    //   }
    // })
  ],
};

和 package.json:

    "@types/aws-lambda": "^8.10.17",
    "@types/node": "^10.12.18",
    "fork-ts-checker-webpack-plugin": "^3.0.1",
    "serverless": "^1.73.1",
    "serverless-aws-documentation": "^1.1.0",
    "serverless-dynamodb-local": "^0.2.39",
    "serverless-iam-roles-per-function": "^2.0.2",
    "serverless-offline": "^6.4.0",
    "serverless-reqvalidator-plugin": "^1.0.3",
    "serverless-s3-local": "^0.6.2",
    "serverless-webpack": "^5.2.0",
    "ts-loader": "^5.3.3",
    "typescript": "^3.2.4",
    "webpack": "^4.29.0",
    "webpack-node-externals": "^1.7.2"

离线和云部署都可以。

于 2020-06-18T02:55:01.903 回答