Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
23
Help us understand the problem. What is going on with this article?
@suin

tsconfigのmoduleの設定値の違い: commonjs, amd, system, umd, es6, es2015, esnext, none

More than 1 year has passed since last update.

本稿では、tsconfigのmoduleの設定値によってどのようなJavaScriptコードが生成されるかを確認するものである。次の設定値を試す: commonjs, amd, system, umd, es6, es2015, esnext, none。

コンパイルしてみるTypeScriptコード。

main.ts
export const a = 1;

未指定または、"commonjs"

main.js
"use strict";
exports.__esModule = true;
exports.a = 1;
  • CommonJSと呼ばれる規格。
  • Node.jsなどサーバサイドコードで一般的。

"amd"

main.js
define(["require", "exports"], function (require, exports) {
    "use strict";
    exports.__esModule = true;
    exports.a = 1;
});
  • Asynchronous Module Definition (AMD)と呼ばれる規格。
  • クライアントサイドコードで一般的。

"system"

main.js
System.register([], function (exports_1, context_1) {
    "use strict";
    var a;
    var __moduleName = context_1 && context_1.id;
    return {
        setters: [],
        execute: function () {
            exports_1("a", a = 1);
        }
    };
});

"umd"

main.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;
    exports.a = 1;
});
  • Universal Module Definition (UMD)と呼ばれる規格。
  • AMDとCommonJSの両方をサポートする。

"es6", "es2015", "esnext"

main.js
export var a = 1;

"none"

main.ts:1:1 - error TS1148: Cannot use imports, exports, or module augmentations when '--module' is 'none'.

1 export const a = 1;
  ~~~~~~~~~~~~~~~~~~~


Found 1 error.

参考

23
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
suin
Qiita 4位/TypeScript入門書執筆中/TypeScripterのための座談会「YYTypeScript」主催/『実践ドメイン駆動設計』書籍邦訳レビュア/分報Slack考案/YYPHP主催/CodeIQマガジン執筆/株式会社クラフトマンソフトウェア創設/Web自動テスト「ShouldBee」の開発/TypeScript/DDD/OOP
craftsman_software
「インフラの心配は、もうおしまい」 インフラ運用を自動化し、手作業を限りなくゼロにする会社

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
23
Help us understand the problem. What is going on with this article?