简体   繁体   English

如何包含/不包含打字稿文件

[英]How to include/NOT include a typescript file

We have a project which is an independent collection of javascript controls. 我们有一个项目,它是一个独立的javascript控件集合。 We use typecript in this project and have enabled "Combine JavaScript output into file". 我们在该项目中使用typecript,并启用了“将JavaScript输出合并到文件中”。 As you can imagine this eventually compiles to a rather large JS file. 可以想象,最终它将编译为一个相当大的JS文件。

We then have a separate project where our app resides - it references some of the controls in the above project via the JS file. 然后,我们在应用程序所在的地方有了一个单独的项目-它通过JS文件引用了上述项目中的某些控件。

My question is this: what is the best way to include only the controls we need in our project in such a way so as to contain the size of the JS file. 我的问题是:最好的方法是只包含我们项目中所需的控件,以便包含JS文件的大小。 We only want to include JS for controls we need. 我们只想为所需的控件包含JS。 Am I forced to "not combine" and include individual JS - or is there a better way to independently combine "specific" typescript files into one file. 我是否被迫“不合并”并包含单个JS-还是有更好的方法将“特定”打字稿文件独立合并为一个文件。

If you have a large number of optional components, consider switching to AMD modules. 如果您有大量可选组件,请考虑切换到AMD模块。 This allows you to load modules on demand and was invented to solve the problem you describe. 这使您可以按需加载模块,并且是为了解决您所描述的问题而发明的。

If you currently have internal modules, you can convert them to external modules by adding an export statement. 如果当前有内部模块,则可以通过添加export语句将其转换为外部模块。 For example, this internal module: 例如,此内部模块:

module MyInternalModule {
    export class Example {

    }
}

Can be converted into an external module by adding one line of code: 可以通过添加以下代码行将其转换为外部模块:

module MyInternalModule {
    export class Example {

    }
}

export = MyInternalModule;

You can then use an import statement to load it when you need it: 然后,可以在需要时使用import语句加载它:

import GiveItAnAlias = require('./MyInternalModule');

(You don't put .ts in that path). (您不要在该路径中放入.ts )。

You will need to supply a module flag to the compiler (or change the project settings if you are using Visual Studio): 您将需要向编译器提供模块标志(或者,如果您使用的是Visual Studio,请更改项目设置):

tsc --module amd app.ts

You are now able to build a collection of modules and load them asynchronously on demand. 现在,您可以构建模块的集合,并根据需要异步加载它们。

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

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