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)
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)