Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda