带引用的TypeScript项目
我用“前”和“后”来指“共享”项目 tsc-v:带引用的TypeScript项目,typescript,project-structure,Typescript,Project Structure,我用“前”和“后”来指“共享”项目 tsc-v:3.3.3版 项目结构: ./{MY_PROJECT}.code-workspace /* the only file in this level */ ./back ./back/tsconfig.json ./shared/src/ ./shared/ ./shared/tsconfig.json ./shared/src/ ./front ./front/tsconfig.json ./front/src { "compiler
3.3.3版
项目结构:
./{MY_PROJECT}.code-workspace /* the only file in this level */
./back
./back/tsconfig.json
./shared/src/
./shared/
./shared/tsconfig.json
./shared/src/
./front
./front/tsconfig.json
./front/src
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/front-bundle.js",
"paths": {"@shared/*" : ["../shared/*"]},
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
],
"references": [
{
"path": "../shared"
}
]
}
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"composite": true,
"declaration": true,
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/shared-bundle.js",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
]
}
我正在尝试从共享项目中将模块导入到/front/src/article view model.ts
:
import Article from "@shared/src/article"; // alias path
import Article from "../../shared/src/article"; // full relative path
export default class ArticleViewModel {
}
VS代码GUI中会立即显示以下错误:
对于别名路径:
找不到模块'@shared/src/article'。ts(2307)
对于完整相对路径:
输出文件“../../shared/src/article”不是从源文件“c://{SOMEWHERE_IN_MY_PC}/shared/src/article.ts”生成的。ts(6305)
Intellisense(VS代码)适用于别名和相对选项:
如果我尝试忽略错误并生成,则会失败:
C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:1296
投掷e;
^
错误:调试失败。错误的表达。
在mergeSymbol(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:25861:26)
在C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:25960:47
在Map.forEach()处
在mergeSymbolTable(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:25958:20)
在InitializePechecker(C:\Program Files\nodejs\node_modules\npm\bin\node_modules\typescript\lib\tsc.js:48653:21)
在Object.createTypeChecker(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:25711:9)
在getDiagnosticsProducingTypeChecker(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:71398:93)
在Object.getGlobalDiagnostics(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:71755:72)
在Object.getGlobalDiagnostics(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:73528:86)
在buildSingleProject(C:\Program Files\nodejs\node\u modules\npm\bin\node\u modules\typescript\lib\tsc.js:75803:127)
/front/tsconfig.json内容:
./{MY_PROJECT}.code-workspace /* the only file in this level */
./back
./back/tsconfig.json
./shared/src/
./shared/
./shared/tsconfig.json
./shared/src/
./front
./front/tsconfig.json
./front/src
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/front-bundle.js",
"paths": {"@shared/*" : ["../shared/*"]},
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
],
"references": [
{
"path": "../shared"
}
]
}
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"composite": true,
"declaration": true,
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/shared-bundle.js",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
]
}
/shared/tsconfig.json内容:
./{MY_PROJECT}.code-workspace /* the only file in this level */
./back
./back/tsconfig.json
./shared/src/
./shared/
./shared/tsconfig.json
./shared/src/
./front
./front/tsconfig.json
./front/src
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/front-bundle.js",
"paths": {"@shared/*" : ["../shared/*"]},
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
],
"references": [
{
"path": "../shared"
}
]
}
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"composite": true,
"declaration": true,
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/shared-bundle.js",
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
]
}
这完全不能直接回答你的问题,但我觉得提供一个替代方案可能仍然有用 以“更标准”的方式解决这一问题的一种方法是,将您的3个代码库都制作成一个NPM包 这样,您的导入将类似于
@vendor/shared/foo
,而不是。/../../../../shared/src/article
使用npm链接
很容易处理项目的交叉依赖关系
从技术上讲,您甚至不需要修改源代码结构(尽管您可能想这样做)。
shared
依赖项只是通过node\u模块进行软链接
如果我在包含front
、back
和shared
的目录中出现错误,我可以重现您得到的错误。在这种情况下,运行tsc-b
将在当前目录中拾取错误的tsconfig.json
,并且由于未使用正确的路径或任何其他配置,编译将失败,并出现与您相同的错误
否则,如果我使用您的文件并发出tsc-b front
而不是tsc-b
,它编译时不会出错
VSCode没有遇到问题的原因是为了提供完整性,TypeScript编辑器(通常)使用TypeScript提供的工具tsserver
。当编辑器给出tsserver
的文件路径时,tsserver
通过查找包含源文件的目录,然后向上搜索父目录,依此类推,直到找到tsconfig.json
文件,从而获得相关的tsconfig.json
。因此,当VSCode在front/src/foo.ts
上工作并要求tsserver
提供完成信息时,tsserver
查找没有匹配文件的front/src
,然后front
,并在那里找到tsconfig.json
。如果您是从根文件夹编译,尝试添加一个本地tsconfig.json
文件,其中包含所有引用设置,即引用:{path:'./shared'}
。否则,tsc将无法找到相关项目进行建设
此外,没有roottsconfig.json
将不允许VSCode GUI正常工作,因为它会查找roottsconfig.json
(这是我上次检查它时的状态,您可以在这里找到)
该错误似乎与丢失的文件有关。如果您能提供有关您所采取的构建步骤的更多详细信息,我可以尝试更好地检查它我在上次活动之后解决了它,但我不能100%确定这是否是由于以下更改而发生的。无论如何,我都会把它贴在这里,因为仍然有新的观点和投票,所以它可能对其他人很有价值:
这就是变化:
在/shared/tsconfig.json内容中:而不是:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"composite": true,
"declaration": true,
"module": "amd",
"noEmitOnError": true,
"noImplicitAny": false,
"out": "./lib/shared-bundle.js", <-------------
"preserveConstEnums": true,
"removeComments": true,
"sourceMap": true,
"target": "es2015",
"watch": true
},
"include": [
"./src/**/*.ts",
]
}
{
“编译器选项”:{
“allowSyntheticDefaultImports”:true,
“复合”:没错,
“宣言”:正确,
“模块”:“amd”,
“noemiton”:没错,
“noImplicitAny”:错,
“out”:“/lib/shared bundle.js”,我来到这里是为了解决“未从源文件构建”错误。结果是tsc
watch进程没有正确退出,所以我有两个实例在争夺文件。以防万一其他人在搜索时遇到同样的问题发现了这个问题“尚未从源文件生成typescript引用”。
我的错误是,当我:tsc--build tsconfig.json
时,我正在运行tsc-p tsconfig.json
。如果使用-p
TypeScript运行,则不会生成引用的项目。只有在使用--build
运行时,我才没有tsconfig.json