[英]CommonJS to ES6 migration exports. to export
I'm migrating a Nativescript project from version 6.8 to 8.1.我正在将 Nativescript 项目从 6.8 版迁移到 8.1 版。 This involves converting modules from CommonJS to ES6, which is mostly just converting function exports, so that
这涉及将模块从 CommonJS 转换为 ES6,这主要是转换 function 导出,因此
exports.foo = function(args) { ... }
becomes变成
export function foo(args) { ... }
and if you invoke the function from within the module, exports.foo()
becomes just foo()
.如果您从模块中调用 function ,则 exports.foo
exports.foo()
变为foo()
。
In addition to my own code I'm finding I'm having to migrate some of the plugins I use as newer versions aren't available.除了我自己的代码之外,我发现我必须迁移一些我使用的插件,因为新版本不可用。 So far so good, except for this block of code:
到目前为止一切顺利,除了这段代码:
/**
* List of outout formats.
*/
(function (OutputFormat) {
/**
* PNG
*/
OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
/**
* JPEG
*/
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
})(exports.OutputFormat || (exports.OutputFormat = {}));
var OutputFormat = exports.OutputFormat;
I'm having a hard time following what this does, much less converting it to ES6 syntax.我很难理解它的作用,更不用说将其转换为 ES6 语法了。 For context, here's the type definition:
对于上下文,这是类型定义:
export declare enum OutputFormat {
/**
* PNG
*/
PNG = 1,
/**
* JPEG
*/
JPEG = 2,
}
I'd welcome any suggestions on how to convert this.我欢迎任何有关如何转换它的建议。
First look at what it's invoked with:首先看看它是用什么调用的:
(exports.OutputFormat || (exports.OutputFormat = {}));
exports.OutputFormat
is truthy, it'll be the parameterexports.OutputFormat
是真实的,它将是参数exports.OutputFormat = {}
, which will:exports.OutputFormat = {}
,它将:
exports.OutputFormat
exports.OutputFormat
Unless OutputFormat
is referenced elsewhere in this module, which seems unlikely, you can turn it into ES6 module syntax with:除非在此模块的其他地方引用了
OutputFormat
(这似乎不太可能),否则您可以将其转换为 ES6 模块语法:
export const OutputFormat = {
PNG: 1,
1: "PNG",
JPEG: 2,
2: "JPEG",
};
While you can also export an empty object and then run虽然您也可以导出一个空的 object 然后运行
OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
, those lines of code are much more confusing than they need to be, so I'd refactor them to the above. ,这些代码行比它们需要的更令人困惑,所以我将它们重构为上述内容。
(or you could iterate over an array of [["PNG", 1], ["JPEG", 2]]
and assign) (或者您可以遍历
[["PNG", 1], ["JPEG", 2]]
的数组并分配)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.