[英]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.