Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
CommonJS to ES6 migration exports. to export

I'm migrating a Nativescript project from version 6.8 to 8.1. This involves converting modules from CommonJS to ES6, which is mostly just converting function exports, so that

exports.foo = function(args) { ... }

becomes

export function foo(args) { ... }

and if you invoke the function from within the module, exports.foo() becomes just 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. 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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First look at what it's invoked with:

(exports.OutputFormat || (exports.OutputFormat = {}));
  • If exports.OutputFormat is truthy, it'll be the parameter
  • Otherwise, the following expression will be the parameter: exports.OutputFormat = {}, which will:
    • create an empty object
    • assign that empty object to exports.OutputFormat
    • evaluate to that empty object

Unless OutputFormat is referenced elsewhere in this module, which seems unlikely, you can turn it into ES6 module syntax with:

export const OutputFormat = {
  PNG: 1,
  1: "PNG",
  JPEG: 2,
  2: "JPEG",
};

While you can also export an empty object and then run

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)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda