我有一个使用TypeScript的简单示例项目:https://github.com/unindented/ts-webpack-example
运行tsc -p .
(tsc
版本1.8.10)会引发以下问题:
app/index.ts(1,21): error TS2307: Cannot find module 'components/counter'.
components/button/index.ts(2,22): error TS2307: Cannot find module 'shared/backbone_base_view'.
components/button/index.ts(3,25): error TS2307: Cannot find module 'shared/backbone_with_default_render'.
components/counter/index.ts(2,22): error TS2307: Cannot find module 'shared/backbone_base_view'.
components/counter/index.ts(3,25): error TS2307: Cannot find module 'shared/backbone_with_default_render'.
components/counter/index.ts(4,27): error TS2307: Cannot find module 'shared/backbone_with_subviews'.
components/counter/index.ts(5,20): error TS2307: Cannot find module 'components/button'.
它抱怨所有本地文件的导入,如下所示:
import Counter from 'components/counter';
如果我将其更改为相对路径,它会起作用,但我不想这样做,因为在移动文件时,这会使我的生活更加困难:
import Counter from '../components/counter';
vscode
代码库不使用相对路径,但它们的所有功能都很好,所以我的项目中肯定缺少了一些东西:https://github.com/Microsoft/vscode/blob/0e81224179fbb8f6fda18ca7362d8500a263cfef/src/vs/languages/typescript/common/typescript.ts#L7-L14
你可以查看我的GitHub repo,但如果有帮助的话,下面是我正在使用的tsconfig.json
个文件:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules"
]
}
有趣的是,使用ts-loader
通过webpack
构建项目很好,所以我猜这只是一个配置问题...