[英]CommonJS to ES6 migration exports. to export
我正在将 Nativescript 项目从 6.8 版迁移到 8.1 版。 这涉及将模块从 CommonJS 转换为 ES6,这主要是转换 function 导出,因此
exports.foo = function(args) { ... }
变成
export function foo(args) { ... }
如果您从模块中调用 function ,则 exports.foo exports.foo()
变为foo()
。
除了我自己的代码之外,我发现我必须迁移一些我使用的插件,因为新版本不可用。 到目前为止一切顺利,除了这段代码:
/**
* 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;
我很难理解它的作用,更不用说将其转换为 ES6 语法了。 对于上下文,这是类型定义:
export declare enum OutputFormat {
/**
* PNG
*/
PNG = 1,
/**
* JPEG
*/
JPEG = 2,
}
我欢迎任何有关如何转换它的建议。
首先看看它是用什么调用的:
(exports.OutputFormat || (exports.OutputFormat = {}));
exports.OutputFormat
是真实的,它将是参数exports.OutputFormat = {}
,它将:
exports.OutputFormat
除非在此模块的其他地方引用了OutputFormat
(这似乎不太可能),否则您可以将其转换为 ES6 模块语法:
export const OutputFormat = {
PNG: 1,
1: "PNG",
JPEG: 2,
2: "JPEG",
};
虽然您也可以导出一个空的 object 然后运行
OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
,这些代码行比它们需要的更令人困惑,所以我将它们重构为上述内容。
(或者您可以遍历[["PNG", 1], ["JPEG", 2]]
的数组并分配)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.