本文介绍了RequireJS 模块的 TypeScript 编译生成一行 Object.defineProperty(exports, “__esModule", { value: true });如何摆脱它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 tsconfig.json 文件的外观:

This is how my tsconfig.json file looks:

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

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

I have a typescript file named a.ts which is an AMD module (I'm using requirejs), which looks like:

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

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

The compiled Javascript files looks like:

 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 文件:

I need my generated JavaScript file to be:

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 文件中返回a"对象

So I need to
a) Remove the Object.defineProperty(exports, "__esModule", { value: true }); line
b) Remove the require and exports dependencies from define
c) Not having an internal function "a" and then expose "a" on exports, but rather simply return "a" object in the a.js file

我需要对 tsconfig.json 和 a.ts 文件进行哪些更改才能获得所需的 Javascript 文件或更接近它的文件,从当前的 a.js 到我需要的任何改进都会很棒,甚至 1 或 2在 3 个要求中.

What changes do I need to make to tsconfig.json and a.ts files to get the desired Javascript file or something closer to it, any improvements from current a.js towards what I need would be great, even 1 or 2 out of 3 requirements.

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

One way is to make a.ts exactly like my desired a.js file and then compile, but I must use export statement way of making amd module due to another unrelated requirement. Thanks for reading till here. Please help.

推荐答案

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

Your export issue can easily be fixed by using the export = syntax. If you code your module with this:

var a = {
  b: 5
};

export = a;

转译为:

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

请注意,您还会丢失 __esModule 属性的创建.

Note that you also lose the creation of the __esModule property.

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

The rest of your question duplicates another question. In brief, the TypeScript compiler provides no option to avoid emitting the require and exports dependencies. You have to process the emitted code on your own if you want to remove them.

这篇关于RequireJS 模块的 TypeScript 编译生成一行 Object.defineProperty(exports, “__esModule", { value: true });如何摆脱它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 10:13