A Vite plugin that converts import statements into global variable lookups (window['Lib']), replicating Webpack's externals functionality for production builds. Version 0.6.2 requires Vite >=2.0.0 and Node ^14.18.0 || >=16.0.0. Unlike manual Rollup externals, this plugin transforms source code at the JS level, preserving static analysis. Released under MIT, with occasional updates. Key differentiator: supports deeply chained global paths (e.g., window['React']['lazy']) and works on JSX/TSX files after SFC transformation. Does not affect SSR by default; CJS/SSR users need separate handling.
npm install vite-plugin-externalsVerified import paths — ran on the pinned version, not inferred.
Configures vite-plugin-externals to replace Vue, React, ReactDOM, and nested lazy imports with global window lookups, disabling in dev mode to preserve HMR.
Ensure viteExternalsPlugin is listed after Vue/Svelte/etc. plugins in the plugins array.
Override the filter option to return true for node_modules paths.
Set disableInServe: true or load development versions during serve.
Keep useWindow: true (default) or avoid modules with slashes.
Upgrade to >=0.5.0 or adjust filter to ignore the ssr argument.
For SSR, use separate configuration (e.g., disable the plugin or handle externals via Rollup options).
Run 'npm install vite-plugin-externals --save-dev' and ensure your project is ESM (package.json has "type": "module" or use .mjs files).
Ensure you only call viteExternalsPlugin() once in the plugins list.
Reorder plugins so that viteExternalsPlugin comes after @vitejs/plugin-vue.
Disable the plugin for SSR or set useWindow: false (but only if module names have no special characters).
Check that the keys in viteExternalsPlugin options exactly match the import source string (e.g., 'react' not 'React').