在 TypeScript 中,构建 dist 文件夹时使模块别名工作的正确配置是什么?

Wop*_*ppi 5 javascript node.js typescript

我尝试在模块别名打字稿,并在它的工作原理dev,但是当我尝试建立它,我总是得到,因为它指向找不到模块src文件甚至上构建。

我尝试替换distmoduleAliasesin package.json。然而,当我运行时yarn run dev,它编译了,它使用dist文件夹源代码而不是我的src. 我也尝试过介绍tsconfig-paths我的deveg,"dev": "NODE_ENV=development ts-node -r tsconfig-paths/register ./src/index.ts",但系统仍在读取moduleAliases. 也许这里有人在做模块路径时遇到过类似的问题。

这是我的项目结构:

|-project
|---src
|-------index.ts  // (where I put require('module-alias/register');)
|-------app (business logic) // where I start using modules e.g import config from '@config/index.config';
|-----------index.ts
|-----------routes
|-------config
|---dist
|---node_modules
|---.env
|---.eslintrc
|---package.json
|---tsconfig.json
|---yarn.lock
Run Code Online (Sandbox Code Playgroud)

我的片段 package.json

"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
  "dev": "NODE_ENV=development ts-node-dev --respawn --transpileOnly ./src/index.ts",
  "prod": "yarn run build && node -r ./dist/index.js",
  "build": "NODE_ENV=production yarn run build-ts",
  "build-ts": "tsc -p .",
},  
"_moduleAliases": {
  "@config": "./src/config",
  "@routes": "./src/app/routes",
},
Run Code Online (Sandbox Code Playgroud)

我的 tsconfig.json

{
  "compilerOptions": {
    "incremental": true,
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": true,
    "outDir": "./dist",
    "removeComments": true,
    "strict": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "suppressImplicitAnyIndexErrors": true,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "paths": {
      "@config/*": ["config/*"],
      "@routes/*": ["app/routes/*"],
    },
    "allowSyntheticDefaultImports": true, 
    "esModuleInterop": true 
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}
Run Code Online (Sandbox Code Playgroud)

我的.eslintrc(不确定它是否与这个问题有关或相关)

{
  "extends": ["airbnb-typescript/base"],
  "rules": {
    "import/prefer-default-export": 0
  },
  "settings": {
    "import/resolver": {
      "babel-module": {}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我如何使用它

import config from '@config/index.config';
Run Code Online (Sandbox Code Playgroud)

非常感谢。