RequireJS 模块的 TypeScript 编译生成行 Object.defineProperty(exports, "__esModule", { value: true });如何摆脱它?

TypeScript compilation of RequireJS module generates line Object.defineProperty(exports, "__esModule", { value: true }); How to get rid of it?

这是我的 tsconfig.json 文件的样子:

{
    "compileOnSave": true,
    "compilerOptions": {
        "module": "amd",
        "noImplicitAny": false,
        "removeComments": false,
        "preserveConstEnums": true,
        "strictNullChecks": true,
        "sourceMap": false
    }
}

我有一个名为 a.ts 的打字稿文件,它是一个 AMD 模块(我正在使用 requirejs),它看起来像:

export function a() {
    var a = {
        b: 5
    };
    return a;
}

编译后的 Javascript 文件如下所示:

 define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function a() {
        var a = {
            b: 5
        };
        return a;
    }
    exports.a = a;
 });

我需要生成的 JavaScript 文件为:

define(function () {
    "use strict";
    var a = {
        b: 5
    };
    return a;
});

所以我需要
a) 删除 Object.defineProperty(exports, "__esModule", { value: true });行
b) 从 define
中移除 require 和 exports 依赖 c) 没有内部函数 "a",然后在导出时公开 "a",而只是 a.js 文件

中的 return "a" 对象

我需要对 tsconfig.json 和 a.ts 文件进行哪些更改才能获得所需的 Javascript 文件或更接近它的文件,当前 a.js 的任何改进朝着我需要的方向发展会很棒,甚至是 3 项要求中的 1 或 2 项。

一种方法是使a.ts完全像我想要的a.js文件然后编译,但由于另一个不相关的要求,我必须使用导出语句的方式来制作amd模块。感谢您阅读到这里。请帮忙。

您的导出问题可以使用 export = 语法轻松解决。如果你用这个编码你的模块:

var a = {
  b: 5
};

export = a;

转译为:

define(["require", "exports"], function (require, exports) {
    "use strict";
    var a = {
        b: 5
    };
    return a;
});

请注意,您还丢失了 __esModule 属性 的创建。

你的问题的其余部分重复 。简而言之,TypeScript 编译器没有提供避免发出 requireexports 依赖项的选项。如果你想删除它们,你必须自己处理发出的代码。