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文件的外观:
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(导出, __esModule,{value:true}); line
b)从define中删除require和export依赖项
c)没有内部函数a然后在exports上公开a,而只是返回a对象在a.js文件中
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到我需要的改进将是很好的,即使是3个要求中的1个或2个。
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.ts完全像我想要的a.js文件然后编译,但我必须使用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 = $ c可以轻松修复导出问题$ c>语法。如果您使用以下代码对模块进行编码:
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 $ c $的创建c> property。
Note that you also lose the creation of the __esModule
property.
你的问题的其余部分重复另一个问题。简而言之,TypeScript编译器不提供任何选项来避免发出 require
和 exports
依赖项。如果要删除它们,则必须自己处理发出的代码。
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});如何摆脱它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!