简体   繁体   English

我应该如何使用tsd类型来组织打字稿模块/软件包?

[英]How should i organize typescript modules/packages using tsd typings?

I've got quite a large TS-project with numbers of internal libraries that are linked to each other as npm packades. 我有一个相当大的TS项目,其中有许多内部库以npm packade彼此链接。 Sometimes in .ts i need to put a references to some ambient or external definitions, provided by TSD, like this: /// <reference path="../../typings/tsd.d.ts" /> Each package includes own .d.ts files referencing to ...root.../typings/tsd.d.ts . 有时在.ts中,我需要引用由TSD提供的一些环境或外部定义,例如: /// <reference path="../../typings/tsd.d.ts" />每个包包括自己的.d.ts文件,这些文件引用了...root.../typings/tsd.d.ts

So, when i install a dependency package inside of another one, there can be a multiple definition for some ambient stuff, such as ...root.../typings/node/node.dt . 因此,当我在另一个依赖包中安装一个依赖包时,对于某些环境文件,可能会有多个定义,例如...root.../typings/node/node.dt And it causes transpiling errors Duplicate identifier . 并导致转码错误Duplicate identifier

I think you all know that problem. 我想你们都知道这个问题。 But i've spent about a day googling and reading and trying numbers of solutions that didn't work fine. 但是我花了大约一天的时间进行谷歌搜索,阅读和尝试一些效果不佳的解决方案。 :( :(

Please, show me a way out of this. 拜托,给我看看解决的办法。 Tell me how you do it. 告诉我你怎么做。

Hmm... A weird idea has come. 嗯...一个奇怪的想法来了。 Put in all projects a postinstall hook script that changes the typings/tsd.d.ts and replaces paths of duplicating definition files to the parent poject's ones, something like this: /// <reference path="../../../../typings/node/node.d.ts" /> 在所有项目中放入一个postinstall钩子脚本,该脚本将更改typings/tsd.d.ts并将替换定义文件的路径替换为父项目的/// <reference path="../../../../typings/node/node.d.ts" /> ,例如: /// <reference path="../../../../typings/node/node.d.ts" />

It's dirty but could work fine 很脏但是可以正常工作

 var fs = require('fs'); var path = require('path'); function searchParentTypingPath(currentPath, moduleName, depth){ if (!depth) depth = 1; if (depth>4) return null; try{ var filePath = currentPath+'/typings/'+moduleName+'/'+moduleName+'.d.ts'; fs.accessSync(filePath); return filePath; } catch(e){ return searchParentTypingPath(currentPath+'/..', moduleName, depth+1); } } module.exports = function (tsdFilePath, verbose){ var tsdContent = fs.readFileSync(tsdFilePath).toString(); var resultContent = ''; tsdContent.split('\\n').forEach(function(line){ var match = line.match(/^\\/\\/\\/\\s+<reference path="([\\/a-zA-Z0-9_\\-.]+)"\\s*\\/>/); if (!match) { resultContent+=line+'\\n'; return; } var modulePath = match[1]; if (!modulePath.match(/^\\w+/)) {// it's not provided by TSD resultContent+=line+'\\n'; return; } var moduleName = path.basename(modulePath, '.d.ts'); var parentPath = searchParentTypingPath(path.dirname(tsdFilePath)+'/../..', moduleName); resultContent+='/// <reference path="'+(parentPath||modulePath)+'" />\\n'; }); if (verbose) { console.log(resultContent); return resultContent; } fs.writeFileSync(tsdFilePath, resultContent); }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM