简体   繁体   English

CommonJS 到 ES6 的迁移导出。 出口

[英]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 = {}));
  • If exports.OutputFormat is truthy, it'll be the parameter如果exports.OutputFormat是真实的,它将是参数
  • Otherwise, the following expression will be the parameter: exports.OutputFormat = {} , which will:否则,以下表达式将是参数: exports.OutputFormat = {} ,它将:
    • create an empty object创建一个空的 object
    • assign that empty object to exports.OutputFormat将那个空的 object 分配给exports.OutputFormat
    • evaluate to that empty object评估为那个空的 object

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.

相关问题 将 CommonJS 的导入和导出更新到 ES6 - Updating imports and exports from CommonJS to ES6 ES6导入/导出与CommonJS导出/需求如何配合 - How does ES6 import / export play along with CommonJS exports / require 如何正确使用ESJ“导出默认值”与CommonJS“require”? - How to correctly use ES6 “export default” with CommonJS “require”? 将commonjs模块与ES6模块混合以导出两个功能 - mixing commonjs module with ES6 module to export two functions 编写ES6模块并将其转换为CommonJS时,实际上未导出任何指定的导出 - When writing an ES6 module and converting to CommonJS, no specified exports are actually being exported ES6 - 有没有一种优雅的方式来导入所有命名的导出而不是默认的导出? - ES6 - is there an elegant way to import all named exports but not the default export? 如何在一个命名导出中收集多个命名的 ES6 导出? - How to collect multiple named ES6 exports in one named export? 当有 CommonJS 而不是 ES6 模块导出/导入时无法构建应用程序 - Cannot build an app when there is CommonJS instead of ES6 module exports/imports 如何在不使用module.exports的情况下以JavaScript导出ES6类 - How to export ES6 class in Javascript without module.exports 在ES6中导入/导出 - Import/ Exports in ES6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM