我正在尝试创建一个目录结构如下的包:
src/
folder1/
index.ts
...
folder2/
index.ts
...
folder3/
index.ts
...
.
.
.
我想将其导入为 import { Class1 } from '@package/folder1'
、import { Class2 } from '@package/folder2'
等。
此时,我可以导入我的类,例如 import { Class1 } from '@package/lib/folder1'
。是否有可能实现我的预期行为?
tsconfig.json
:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./lib",
"rootDir": "./src",
"rootDirs": ["./src/folder1"],
"lib": [
"es2017",
"esnext.asynciterable"
],
"typeRoots": [
"./node_modules/@types"
],
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node",
"pretty": true,
"noEmit": false,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "lib"]
}
最佳答案
在我使用的 Typescript 版本(3.7.4)中,这个功能似乎并不是开箱即用的。包 module-alias 可能是一种解决方法,但看起来 Typescript 3.8 将允许向导出添加别名。
关于node.js - 保留导入包的目录结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60154110/