CommonJS 到 ES6 的迁移导出。导出
CommonJS to ES6 migration exports. to export
我正在将 Nativescript 项目从 6.8 版迁移到 8.1 版。这涉及将模块从 CommonJS 转换为 ES6,这主要只是转换函数导出,因此
exports.foo = function(args) { ... }
变成
export function foo(args) { ... }
如果您从模块中调用该函数,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",
};
虽然您也可以导出一个空对象,然后 运行
OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
,这些代码行比它们需要的更混乱,所以我将它们重构为上面的代码。
(或者您可以遍历 [["PNG", 1], ["JPEG", 2]]
的数组并赋值)
我正在将 Nativescript 项目从 6.8 版迁移到 8.1 版。这涉及将模块从 CommonJS 转换为 ES6,这主要只是转换函数导出,因此
exports.foo = function(args) { ... }
变成
export function foo(args) { ... }
如果您从模块中调用该函数,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",
};
虽然您也可以导出一个空对象,然后 运行
OutputFormat[OutputFormat["PNG"] = 1] = "PNG";
OutputFormat[OutputFormat["JPEG"] = 2] = "JPEG";
,这些代码行比它们需要的更混乱,所以我将它们重构为上面的代码。
(或者您可以遍历 [["PNG", 1], ["JPEG", 2]]
的数组并赋值)