Mehran
Mehran

Reputation: 16831

Preventing tsc compiler from omitting the unnecessary external modules

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.

Upvotes: 1

Views: 295

Answers (2)

ZeroOne
ZeroOne

Reputation: 791

Another way to do it:

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

No need to create fictitious code

Upvotes: 5

Fenton
Fenton

Reputation: 250902

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.

Upvotes: 1

Related Questions