这是我的 tsconfig.json 文件的样子:
{
"compileOnSave": true,
"compilerOptions": {
"module": "amd",
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": false
}
}
我有一个名为 a.ts 的 typescript 文件,它是一个 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 文件中返回“a”对象
我需要对 tsconfig.json 和 a.ts 文件进行哪些更改才能获得所需的 Javascript 文件或更接近它的文件,从当前 a.js 到我需要的任何改进都会很棒,即使是 1 或 2在 3 个要求中。
一种方法是使 a.ts 完全像我想要的 a.js 文件然后编译,但由于另一个不相关的要求,我必须使用 export 语句方式来制作 amd 模块。感谢您阅读到这里。请帮忙。
最佳答案
使用 export =
语法可以轻松解决导出问题。如果你用这个编码你的模块:
var a = {
b: 5
};
export = a;
它被转译为:
define(["require", "exports"], function (require, exports) {
"use strict";
var a = {
b: 5
};
return a;
});
请注意,您还丢失了 __esModule
属性的创建。
您的问题的其余部分重复 another question .简而言之,TypeScript 编译器没有提供任何选项来避免发出 require
和 exports
依赖项。如果你想删除它们,你必须自己处理发出的代码。
关于javascript - RequireJS 模块的 TypeScript 编译生成行 Object.defineProperty(exports, "__esModule", { value : true }); How to get rid of it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47297768/