A Rollup plugin that transforms external import statements (static and dynamic) into global variable references, similar to Rollup's built-in output.globals option but with broader support. Version 0.13.0 requires Rollup ^2.25.0 || ^3.3.0 || ^4.1.4. It uses a moduleId-to-variableName map (object or function) to rewrite imports to direct globals, helping in scenarios like bundling for browser environments where dependencies are loaded via script tags. Key differentiators: it works with dynamic imports, supports include/exclude patterns via picomatch, provides a custom dynamic wrapper, and can use const bindings via the constBindings option. It ships TypeScript types.
npm install rollup-plugin-external-globalsVerified import paths — ran on the pinned version, not inferred.
Shows basic usage: map jquery import to global $ variable in a Rollup configuration.
If you rely on variable hoisting, the default `var` might be fine; if you need `const`, set `constBindings: true`. Avoid `export * from` with external globals.
Update Rollup to version 4 or later.
Place this plugin after rollup-plugin-commonjs or other CJS transformers.
Ensure the global variable returns the expected default or shape; consider setting `dynamicWrapper` to adjust.
Update include/exclude patterns to picomatch syntax if you were using older patterns.
Add the module to `output.globals` or ensure it's listed in `external` options.
Set `dynamicWrapper` to a function that returns the global variable directly, e.g., `() => 'Promise.resolve({ default: myGlobal })'`.Upgrade Rollup to 3.x or 4.x, or downgrade plugin to 0.12.0.