9

我已经按照 webpack4 示例来设置配置:https ://github.com/serverless-heaven/serverless-webpack/tree/master/examples/babel-webpack-4 因为我收到错误“找不到模块源-地图支持/注册”。

我已经调查了已经创建的问题:

https://github.com/serverless-heaven/serverless-webpack/issues/357

https://github.com/serverless-heaven/serverless-webpack/issues/228

现在我的配置是:

.babelrc
{
"comments": false,
"sourceMaps": "both",
"presets": ["env","stage-2"],
"plugins": ["source-map-support"]
}
webpack.config.js:
const path = require('path');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');

module.exports = {
  entry: slsw.lib.entries,
  mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
    sourceMapFilename: '[file].map',

  },
  optimization: {
    // We no not want to minimize our code.
    minimize: false,
  },
  performance: {
    // Turn off size warnings for entry points
    hints: false,
  },
  target: 'node',
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [path.join(__dirname, 'src')],
        exclude: /node_modules/,
      },
      {
        test: /\.json$/,
        loader: 'json',
        include: path.join(__dirname, 'src'),
      },
    ],
  },
  externals: [nodeExternals()],
  devtool: 'source-map',


};
包.json
{
  "name": "someservice",
  "version": "2.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --config --colors",
    "lint": "eslint . --max-warnings=0 --report-unused-disable-directives && echo '✔  Your .js files look good.'"
  },
  "author": "abc@gmail.com",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-jest": "^23.4.2",
    "babel-loader": "^7.1.5",
    "babel-plugin-source-map-support": "^2.0.1",
    "babel-plugin-transform-class-properties": "^6.22.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
    "babel-plugin-transform-inline-environment-variables": "^6.8.0",
    "babel-plugin-transform-object-rest-spread": "^6.22.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-latest": "^6.22.0",
    "babel-preset-stage-2": "^6.24.1",
    "eslint": "^5.4.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-airbnb-base": "13.1.0",
    "eslint-loader": "^2.1.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jest": "^21.22.0",
    "eslint-plugin-jsx-a11y": "^6.1.1",
    "eslint-plugin-react": "^7.1.0",
    "eslint-plugin-security": "^1.4.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^23.5.0",
    "jest-transform-stub": "^1.0.0",
    "lint-staged": "^7.2.2",
    "prettier-eslint": "^8.8.2",
    "prettier-eslint-cli": "^4.3.2",
    "serverless-jest-plugin": "^0.1.6",
    "serverless-offline": "^3.25.11",
    "serverless-webpack": "^5.2.0",
    "standard": "^12.0.1",
    "webpack": "^4.17.3",
    "webpack-node-externals": "^1.7.2",
    "babel-runtime": "^6.22.0"
  },
  "dependencies": {
    "aws-sdk": "^2.11.0",
    "source-map-support": "^0.5.9"
  }
}
无服务器.yml
service: someservice
plugins:
  - serverless-offline
  - serverless-webpack
provider:
  name: aws
  runtime: nodejs6.10
  region: us-east-1
  stage: ${env:STAGE}

custom:
  webpackConfig: ./webpack.config.js
  includeModules: true

functions:
  getUser:
     handler: src/user/UserHandler.getUser
     memory: 512
     timeout: 60
     events:
       - http:
            method: get
            path: user
            cors: true
            integration: lambda

  createUser:
     handler: src/user/UserHandler.createUser
     memory: 512
     timeout: 60
     events:
       - http:
            method: post
            path: user
            cors: true
            integration: lambda

我仍然面临同样的问题。它适用于 sls invoke local -f但是当使用sls deploy部署到 aws 时,它会在调用 API Url 时显示错误。

npm 版本:6.4.1

节点版本:v10.10.0

无服务器:1.30.3

4

2 回答 2

2

我也遇到了同样的问题,但安装后它现在可以工作了source-map-support
yarn add source-map-support
或者
npm install source-map-support

于 2021-08-06T07:17:55.373 回答
0

如果Cannot find module 'source-map-support/register'使用 webpack 版本 5 的 Serverless Framework v2 项目出现错误,请检查concatenateModulesin的设置webpack.config.js。禁用它,我的错误就消失了。

module.exports = {
  //...
  optimization: {
    concatenateModules: false
  }
};

请参阅此问题External modules are not packaged with webpack 5

这是我的package.json仅供参考:

{
  //...
  "dependencies": {
    "source-map-support": "^0.5.19"
  },
  "devDependencies": {
    "@serverless/typescript": "^2.12.0",
    "@types/aws-lambda": "^8.10.64",
    "@types/node": "^14.14.6",
    "@types/serverless": "^1.78.11",
    "fork-ts-checker-webpack-plugin": "^6.0.0",
    "serverless": "^2.13.0",
    "serverless-webpack": "^5.2.0",
    "ts-loader": "^8.0.10",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.5",
    "webpack": "^5.4.0",
    "webpack-node-externals": "^2.5.2"
  },
}
于 2020-11-30T14:56:21.677 回答