【发布时间】:2017-09-17 15:52:13
【问题描述】:
我刚开始使用打字稿并试图让模块解析工作。我的问题很简单(我认为),但我不确定我的tsconfig.json 做错了什么。
如果我的文件夹结构为:
+ release
+ definitions
+ js
> a.js
> main.js
+ src
> main.ts
> a.ts
使用 gulp,我正在做 incremental compilation 就像在 gulp 示例中一样。
而我的tsconfig.json 是:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"src/*": [
"./src/*"
]
}
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
我正在尝试允许将 src 目录别名为 src/module_name,因此分辨率对于像 import foo from 'src/la/ja' 这样的东西可以正常工作。因此,如果我在一个深度嵌套的文件中,例如 src/something/other/foo.ts,并且我想从 src 中的 utils 文件夹中导入一些东西,我可以避免混乱的路径 import bar from '../../../utils/bar',而只需执行 from 'src/utils/bar'。
在我的main.ts 中,我试图得到:
import { a } from 'src/a'
使用正确的相对目录进行编译。使用我当前的tsconfig.json,这可以解决(定义似乎很好)并且不会引发任何 linting 错误,但是当它编译时 - 它编译为:
const a_1 = require("src/a");
而不是
const a_1 = require("./a");
因此,当尝试运行它时,它会失败,因为在 release/js/ 中找不到 src/。有什么办法可以强制 typescript 编译成相对路径?
【问题讨论】:
-
你解决了吗?
-
不,这仍然是个问题
标签: typescript gulp