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

225
Vistas
How to replace global variable with relative import using Rollup?

Is it possible to use a global variable with rollup to result to a relative import of a file in the bundled dest? Something like

const file = window.MY_GLOBAL;

to

const file = import(`path-to-file-in-dest`);

I've seen the define plugin

define({
   replacements: {
      "window.MY_GLOBAL": `import ("${(path_to_file_in_dest`)}")`,
    },
}),

but get Error SyntaxError: Unexpected token (1:12)

Updated:

Trying also with the replace plugin:

// rollup.config.js
const externalFile = path.resolve("./myJsonFile.json");
plugins: [
 typescript(),
 json(),
 resolve(),
 commonjs(),
 replace({
            MY_JSON_FILE: externalFile,
            preventAssignment: true,
        }),
...],

//myCode.ts
export const MY_JSON_FILE = "";

import(MY_JSON_FILE)
 .then((data) => {
      window.X = data;
      console.debug(window.X);
 })
 .catch((error) => {
      console.error(`Unable to find file`,error);
      return;
 });

but still getting error when rollup -c

(!) Plugin typescript: @rollup/plugin-typescript: Rollup requires that TypeScript produces ES Modules. Unfortunately your configuration specifies a "module" other than "esnext". Unless you know what you're doing, please change "module" to "esnext" in the target tsconfig.json file or plugin options.

and when I change the "module" to 'ESNext' as it prompts me to do...

    typescript({ module: "ESNext" }),

it complains for:

 Error: Duplicate regular expression flag (Note that you need plugins to import files that are not JavaScript)
src/myCode.ts (512:0)
512:             import(MY_JSON_FILE)
about 4 years ago · Juan Pablo Isaza
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