Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AMD modules in --module=umd are named at usage site but not at declaration site #18454

Closed
alexeagle opened this issue Sep 13, 2017 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@alexeagle
Copy link
Contributor

alexeagle commented Sep 13, 2017

$ ./node_modules/.bin/tsc -v
Version 2.5.2

$ cat a.ts
/// <amd-module name="a"/>
export const a = 1;

$ cat b.ts
/// <amd-module name="b"/>
import {a} from './a';
console.log(a);

$ cat tsconfig.json
{
  "compilerOptions": {
    "module": "umd"
  }
}

$ ./node_modules/.bin/tsc

$ cat a.js
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    exports.__esModule = true;
    /// <amd-module name="a"/>
    exports.a = 1;
});

$ cat b.js
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "a"], factory);
    }
})(function (require, exports) {
    "use strict";
    exports.__esModule = true;
    /// <amd-module name="b"/>
    var a_1 = require("a");
    console.log(a_1.a);
});

The AMD output embedded in these UMD modules doesn't work, because the modules are declared anonymous (define(["require", "exports"], factory);), but require'd by name (var a_1 = require("a");).

Require.js gives error: Uncaught Error: Mismatched anonymous define() module

Changing the --module flag to amd makes the code work.

I'm aware that /// <amd-module/> directive is intended for --module=amd but since umd modules wrap an amd module, I think this ought to be supported.

Note: internally at Google, we use goog.module as a format that can be concatenated onto a webpage, since modules are named. This named umd proposal allows us to do the same thing for external users, part of http://g.co/ng/abc .

@alexeagle
Copy link
Contributor Author

Note, it's fine that the commonjs modules embedded in the umd modules are unnamed, we'll still load those from individual paths.
It's just for Require.js that we want to be able to concatenate the sources together.

@alexeagle
Copy link
Contributor Author

FYI, as a workaround I use this AfterTransformer
alexeagle/rules_typescript@adaf532#diff-a35a66f3760905e2e8a6f1f168d11f05

@kitsonk
Copy link
Contributor

kitsonk commented Sep 14, 2017

While I don't need it, it does seem appropriate that if:

/// <amd-module name="a"/>
export const a = 1;

Emits the following when targeting amd:

define("a", ["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /// <amd-module name="a"/>
    exports.a = 1;
});

That the following would be emitted when targeting umd:

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
-        define(["require", "exports"], factory);
+        define("a", ["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    exports.__esModule = true;
    /// <amd-module name="a"/>
    exports.a = 1;
});

Semi related is good ole #8436 which is to have a global fallback to UMD modules.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 14, 2017

should be in typescript@next tomorrow.

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Sep 14, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants