Бессерверная структура не распознает пути tsconfig

Я пытаюсь развернуть свое приложение Node.js в Lambda с помощью бессерверной инфраструктуры, однако мой код узла использует пути из tsconfig.json для ссылки на импорт, но процесс бессерверного развертывания не выполняется. Как подключить serverless.yml для подтверждения и использования путей, определенных в tsconfig.json?

Я установил serverless-plugin-typescript, но похоже, что он не распознает пути из tsconfig.

serverless.yml

service:
  name: app-name

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

plugins:
  - serverless-webpack
  - serverless-plugin-typescript

...

tsconfig.ts

{
  "compileOnSave": true,
  "compilerOptions": {
    "lib": [
      "es2017"
    ],
    "baseUrl": "./src",
    "declaration": true,
    "downlevelIteration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist",
    "paths": {
      "@config/*": [
        "config/*"
      ],
      "@core/*": [
        "core/*"
      ],
      "@infra/*": [
        "infra/*"
      ],
      "@modules/*": [
        "modules/*"
      ],
      "@shared/*": [
        "shared/*"
      ]
    },
    "preserveConstEnums": true,
    "removeComments": true,
    "rootDir": "./src",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictNullChecks": false,
    "target": "es2017",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": [
      "node"
    ]
  },
  "include": [
    "./**/*.ts"
  ],
  "exclude": [
    "node_modules/**/*",
    ".serverless/**/*",
    ".webpack/**/*",
    "_warmup/**/*",
    ".vscode/**/*"
  ]
}

1 ответ

Я нашел ответ. Оказывается, вам нужно установить https://www.npmjs.com/package/tsconfig-paths-webpack-plugin. Затем в webpack.config.js вы добавляете следующее:

npm install --save-dev tsconfig-paths-webpack-plugin

внутри webpack.config.js:

const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');


module.exports = {
  ...
  resolve: {
    plugins: [new TsconfigPathsPlugin({ configFile: "./tsconfig.json" })]
  },
  ...
};

ПРИМЕЧАНИЕ: обязательно используйте плагины в разделе разрешения. В корневом каталоге есть плагины, однако TsconfigPathsPlugin работает только в разрешении /plugins.

Надеюсь, это поможет вам, если у вас возникнет такая же проблема.

Другие вопросы по тегам