Avoid remove import statements.

Topics: General
Jul 24, 2013 at 5:16 AM
Edited Jul 24, 2013 at 5:22 AM
I want use AMD (require.js) to loading all module async, even third party libraries. (e.g. jquery-ui).

I do some works as following to made it working:
  • Made a requirejs config as following:
    paths: {
        jquery: '../lib/jquery-2.0.2.min',
        jqueryui: '../lib/jquery-ui/js/jquery-ui-1.10.3.custom.min',
/// <reference path="require.d.ts" />
/// <reference path="jquery.d.ts" />
/// <reference path="jqueryui.d.ts" />
declare module 'jquery' {
    export = jQuery;
declare module 'jqueryui' {
    export = jQuery;
you known, jquery-ui has no exports anymore. it just extend the interface 'JQuery'. so, I export jQuery.
  • After these all, I can use following code to import jquery easily.
import $ = module('jquery');
TS compiler will generate js code just like following:
define(["require", "exports", 'jquery'], function (require, exports, __$__) {
    var $ = __$__;
    // ...
But now, if I import JQuery-UI just like jquery.
import jqueryui = module('jqueryui');
I found the compiler will do not add dependency to jqueryui anymore. I guess that is caused by compiler optimization, maybe compiler consider it as a unused variable, so remove this dependency away.

I have a workaround for this issues. But I think it is not good. Use import a module explicit, compiler should not remove it.
Jul 24, 2013 at 3:24 PM
If I remember correctly, the import statements are removed when importing modules that only have interfaces. If they are declare modules or export values, I believe the import statement should stay. If they don't, it's probably a bug.

That said, this line will likely not work:
declare module 'jqueryui' {
    export = jQuery;
Since you're trying to export a value that's not defined inside of the module. I know other people on the forums have worked with this more than I have, so there may be a trick to it.
Jul 24, 2013 at 4:42 PM
Thank you, jonturner,

Yes, you are right. the issue is caused by the trick. But it is only way I can found to make third-party library working fine to imported by import statements. Does there have another way to make jqueryui importable?

Thank you