Preventing tsc compiler from omitting the unnecessary external modules

纵然是瞬间 提交于 2019-12-19 11:49:16

问题


There's a feature in TypeScript which I like so much and that's external modules using RequireJs and that the fact that the compiler won't include imported modules unless they are actually needed in the code. Here's an example:

import A = require('./A');
import B = require('./B');

var a = new A();

When you compile the above code using tsc --module amd example.ts it will transcompile to:

define(["require", "exports", './A'], function(require, exports, A) {
    var a = new A();
});

As you can see there's no sign of B in the generated code. That's because B was not actually used. As I said this feature is great but now I've got a scenario in which I need to include some of the external modules even though they are not actually used anywhere in the code.

Does anyone have any idea how to do that? To prevent any misunderstanding, I'm not looking for a way to disable this feature completely, just for some specific modules.


回答1:


Another way to do it:

/// <amd-dependency path="./B" />
import A = require('./A');

No need to create fictitious code




回答2:


There is a simply fiddle you can do to get the compiler to include both:

import A = require('./A');
import B = require('./B');

var a = new A();
var b = B;

The variable b becomes noise in your program, so I wouldn't use this technique too much, but if the B module is doing polyfills or something like that which means you'll never want to directly instantiate it, this gets it loaded for you.



来源:https://stackoverflow.com/questions/26327306/preventing-tsc-compiler-from-omitting-the-unnecessary-external-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!