简体   繁体   English

TypeScript中的Alias外部模块

[英]Alias External Module in TypeScript

I'm working on a project where I want to package my source in to multiple modules (in different files). 我正在一个项目中,我想将源打包到多个模块(在不同文件中)。 I do this so I can include only certain pieces on certain pages to reduce the overall weight. 我这样做是为了在某些页面上只添加某些部分,以减轻整体重量。

The problem I'm having is I can't figure out a syntax which will let me alias the external modules, and I really don't want to have to write out the full module every time (since it's usually something like ABCD for organization). 我遇到的问题是我无法弄清楚将使我为外部模块使用别名的语法,而且我真的不想每次都写出完整的模块(因为对于组织而言,它通常类似于ABCD) )。

When I compile, I have a script which grabs all the files, so I have the external definition file as the first parameter. 编译时,我有一个脚本,其中包含所有文件,因此我将外部定义文件作为第一个参数。

If I write out the whole module (ABCDMyClass) , it recognizes that. 如果我写出整个模块(ABCDMyClass) ,它将识别出来。 If I try to do: 如果我尝试这样做:

module MyModule {
    import ABCD = A.B.C.D;

    export MyClass {
        //...
        public myFunc(obj:ABCD.MyClass) {}
    }
}

It'll tell me that 'The name "ABCD" does not exist in the current scope."' 它会告诉我'The name "ABCD" does not exist in the current scope."'

If it matters, I export all of my classes, but don't export any modules. 如果有问题,我将导出所有类,但不导出任何模块。 I generate the definition file which is included (so the definition file which contains ABCD is first in my list when I compile). 我生成了包含的定义文件(因此,在编译时,包含ABCD的定义文件在列表中排在首位)。

Any ideas? 有任何想法吗?

Update 更新

To elaborate on my file structure, I have something like this: 为了详细说明我的文件结构,我有如下内容:

  • A 一种
    • B
      • a.ts a.ts
      • b.ts b.ts
    • C C
      • c.ts c.ts
      • d.ts d.ts

And then elsewhere I may also have: 然后我可能在其他地方也有:

  • A 一种
    • D d
      • e.ts e.ts
      • f.ts 英尺
      • E Ë
        • g.ts
        • h.ts 高度

In this case, I would build the first set into something like ABts by compiling them with something like: 在这种情况下,我可以将第一个集合编译成类似ABts的内容,方法是将它们编译成类似以下内容的代码:

tsc A/B/a.ts A/B/b.ts A/B/C/c.ts A/B/C/d.ts --out A.B.ts --declarations

Then, when I go to package the next I would do something like: 然后,当我打包下一个文件时,我将执行以下操作:

tsc /path/to/A.B.d.ts A/D/e.ts A/D/f.ts A/D/E/g.ts A/D/E/h.ts --out A.D.ts --declarations

I build those compiler commands dynamically by recursing through a set of files I specify so it gets those compiler things. 我通过遍历指定的一组文件来动态构建那些编译器命令,以便获取这些编译器的内容。

If you are packaging your modules in multiple files in order to load them using the AMD module loading pattern, you need to do the following... 如果要将模块打包为多个文件以便使用AMD模块加载模式加载它们,则需要执行以下操作...

The name of the file is the module name, you don't add module declarations 文件名是模块名称,您无需添加module声明

For example... 例如...

module MyModule {
    export class MyClass {
    }
}

Should actually be MyModule.ts and look like this: 实际上应该是MyModule.ts ,如下所示:

export class MyClass {
}

You would then load it using: 然后,您可以使用以下命令加载它:

import my = module('MyModule');

I think you are currently trying to mix a bundling style of TypeScript code with a module style of loading, which is incompatible. 我认为您当前正在尝试将TypeScript代码的捆绑样式与模块样式的加载混合使用,这是不兼容的。 When you are bundling, the file-names don't matter as you take the responsibility for getting the scripts on the page. 捆绑时,文件名无关紧要,因为您负责在页面上获取脚本。 When you are using AMD or CommonJS, you need to abide by the file naming rules (ie file-name equals module name) and leave out the enclosing module . 当您使用AMD或CommonJS时,您需要遵守文件命名规则(即,文件名等于模块名),并省略封闭的module

As a side note, there is a bug in 0.8.1.1 that affects this, so you may want to read through this if you are on 0.8.1.1: http://typescript.codeplex.com/discussions/405800 附带说明一下,0.8.1.1中存在一个会影响此错误的错误,因此,如果您使用的是0.8.1.1,则可能需要通读此错误: http ://typescript.codeplex.com/discussions/405800

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

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