TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

[Transforms] ES2015 Module to AMD module: Unused dependency not added to define.

Open anwalkers opened this issue 10 years ago • 4 comments

We have an Angular 1.x project that uses RequireJS for DI. We load angular modules wrapped in defines but don't use the dependency directly. We load the dependency with RequireJS then load the angular module. Is there a way to configure ES2016 modules to always get transformed, even if they are not used directly as a dependency?

define("main", ["dependency"], function(dependency) {
        // Angular application definition loading angular module defined in dependency...
        var app = angular.module("angularApp", ["angularModuleFromDependency"])
        app.run();
});

TypeScript Version:

1.8.9

Code

main.ts

import module1 from './module1';
import module2 from './module2';

let v = module1;

export default v;

module1.ts

let x, y, module1;

x = 1;
y = 1;

module1 = x + y;

export default module1;

module2.ts

let x, y, module2;

x = 1;
y = 1;

module2 = x + y;

export default module2;

Expected behavior:

define(["require", "exports", "./module1", "./module2"], function (require, exports, module1_1, module2_1) {
    "use strict";
    var v = module1_1.default;
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = v;
});

Actual behavior:

define(["require", "exports", "./module1"], function (require, exports, module1_1) {
    "use strict";
    var v = module1_1.default;
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = v;
});

anwalkers avatar Apr 09 '16 16:04 anwalkers

you can use <amd-dependency path="dependency"/> to force adding some dependency.

/// <amd-dependency path="./module2"/>
import module1 from './module1';
import module2 from './module2';

let v = module1;

export default v;

produces

define(["require", "exports", './module1', "./module2"], function (require, exports, module1_1) {
    "use strict";
    var v = module1_1["default"];
    exports.__esModule = true;
    exports["default"] = v;
});

Also we use [Transforms] prefix to distinguish items that were filed to track rewrite of the emitter so having it on this issue is a bit misleading.

vladima avatar Apr 09 '16 17:04 vladima

You can also use import without a name:

import './module2';

There are several advantages of this over :

  • Syntax is checked (if you get the syntax of wrong, it silently does nothing)
  • Can be anywhere in the source (if you put any non-comment code before , including imports, it silently does nothing)
  • Works with all kinds of modules, not just AMD

jeffreymorlan avatar Apr 09 '16 19:04 jeffreymorlan

+100 to @jeffreymorlan advise - this kind of ES6 import is exactly what you need. Was recently fixing something related to amd-dependency so it popped up in my head

vladima avatar Apr 09 '16 19:04 vladima

Please see FAQ entry at: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-imports-being-elided-in-my-emit

mhegazy avatar Apr 10 '16 06:04 mhegazy