Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!