Dynamic css import not working
Describe the bug
Here is a reproduced issue. I am trying to load css dynamiclally https://github.com/spamshaker/vite-css-bundling-issue
this says that it should work https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#import-assertions
I am getting
npm run build && npm run preview
> [email protected] build
> tsc && vite build
vite v5.2.11 building for production...
[plugin:vite:dynamic-import-vars] [plugin vite:dynamic-import-vars] src/themes/theme-loader.ts: invalid import "./${name}.css`, { with: { type: "css" } ". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. For example: import(`./foo/${bar}.js`).
✓ 36 modules transformed.
dist/index.html 0.46 kB │ gzip: 0.30 kB
dist/assets/react-CHdo91hT.svg 4.13 kB │ gzip: 2.05 kB
(HERE SHOULD BE TWO MORE CSS BUNDLES FOR GIVEN THEMES)
dist/assets/index-DiwrgTda.css 1.39 kB │ gzip: 0.72 kB
dist/assets/index-CAL_m0jK.js 144.33 kB │ gzip: 46.61 kB
✓ built in 329ms
> [email protected] preview
> vite preview
➜ Local: http://localhost:4173/
➜ Network: use --host to expose
➜ press h + enter to show help
Reproduction
https://github.com/spamshaker/vite-css-bundling-issue
Steps to reproduce
npm i && npm run build && npm run preview
System Info
System:
OS: macOS 14.5
CPU: (10) arm64 Apple M1 Max
Memory: 2.85 GB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.12.1 - ~/.nvm/versions/node/v20.12.1/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v20.12.1/bin/yarn
npm: 10.7.0 - ~/.nvm/versions/node/v20.12.1/bin/npm
pnpm: 9.1.1 - /opt/homebrew/bin/pnpm
bun: 1.1.8 - /opt/homebrew/bin/bun
Browsers:
Chrome: 124.0.6367.208
Safari: 17.5
npmPackages:
@vitejs/plugin-react: ^4.2.1 => 4.2.1
vite: ^5.2.11 => 5.2.11
Used Package Manager
npm
Logs
user@mbp > npx vite build --debug
vite:config bundled config file loaded in 14.73ms +0ms
vite:config using resolved config: {
vite:config build: {
vite:config target: [ 'chrome125' ],
vite:config cssTarget: [ 'chrome125' ],
vite:config outDir: 'dist',
vite:config assetsDir: 'assets',
vite:config assetsInlineLimit: 4096,
vite:config cssCodeSplit: true,
vite:config sourcemap: false,
vite:config rollupOptions: { onwarn: [Function: onwarn] },
vite:config minify: 'esbuild',
vite:config terserOptions: {},
vite:config write: true,
vite:config emptyOutDir: null,
vite:config copyPublicDir: true,
vite:config manifest: false,
vite:config lib: false,
vite:config ssr: false,
vite:config ssrManifest: false,
vite:config ssrEmitAssets: false,
vite:config reportCompressedSize: true,
vite:config chunkSizeWarningLimit: 500,
vite:config watch: null,
vite:config commonjsOptions: { include: [Array], extensions: [Array] },
vite:config dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
vite:config modulePreload: { polyfill: true },
vite:config cssMinify: true
vite:config },
vite:config plugins: [
vite:config 'vite:build-metadata',
vite:config 'vite:watch-package-data',
vite:config 'vite:pre-alias',
vite:config 'alias',
vite:config 'vite:react-babel',
vite:config 'vite:react-refresh',
vite:config 'vite:modulepreload-polyfill',
vite:config 'vite:resolve',
vite:config 'vite:html-inline-proxy',
vite:config 'vite:css',
vite:config 'vite:esbuild',
vite:config 'vite:json',
vite:config 'vite:wasm-helper',
vite:config 'vite:worker',
vite:config 'vite:asset',
vite:config 'vite:wasm-fallback',
vite:config 'vite:define',
vite:config 'vite:css-post',
vite:config 'vite:build-html',
vite:config 'vite:worker-import-meta-url',
vite:config 'vite:asset-import-meta-url',
vite:config 'vite:force-systemjs-wrap-complete',
vite:config 'commonjs',
vite:config 'vite:data-uri',
vite:config 'vite:dynamic-import-vars',
vite:config 'vite:import-glob',
vite:config 'vite:build-import-analysis',
vite:config 'vite:esbuild-transpile',
vite:config 'vite:terser',
vite:config 'vite:reporter',
vite:config 'vite:load-fallback'
vite:config ],
vite:config esbuild: { jsxDev: false, jsx: 'automatic', jsxImportSource: undefined },
vite:config optimizeDeps: {
vite:config holdUntilCrawlEnd: true,
vite:config esbuildOptions: { preserveSymlinks: false, jsx: 'automatic' },
vite:config include: [ 'react', 'react/jsx-dev-runtime', 'react/jsx-runtime' ]
vite:config },
vite:config resolve: {
vite:config mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ],
vite:config conditions: [],
vite:config extensions: [
vite:config '.mjs', '.js',
vite:config '.mts', '.ts',
vite:config '.jsx', '.tsx',
vite:config '.json'
vite:config ],
vite:config dedupe: [ 'react', 'react-dom' ],
vite:config preserveSymlinks: false,
vite:config alias: [ [Object], [Object] ]
vite:config },
vite:config configFile: '/Projects/vite-css-bundling-issue/vite.config.ts',
vite:config configFileDependencies: [
vite:config '/Projects/vite-css-bundling-issue/vite.config.ts'
vite:config ],
vite:config inlineConfig: {
vite:config root: undefined,
vite:config base: undefined,
vite:config mode: undefined,
vite:config configFile: undefined,
vite:config logLevel: undefined,
vite:config clearScreen: undefined,
vite:config build: {}
vite:config },
vite:config root: '/Projects/vite-css-bundling-issue',
vite:config base: '/',
vite:config rawBase: '/',
vite:config publicDir: '/Projects/vite-css-bundling-issue/public',
vite:config cacheDir: '/Projects/vite-css-bundling-issue/node_modules/.vite',
vite:config command: 'build',
vite:config mode: 'production',
vite:config ssr: {
vite:config target: 'node',
vite:config optimizeDeps: { noDiscovery: true, esbuildOptions: [Object] }
vite:config },
vite:config isWorker: false,
vite:config mainConfig: null,
vite:config bundleChain: [],
vite:config isProduction: true,
vite:config css: { lightningcss: undefined },
vite:config server: {
vite:config preTransformRequests: true,
vite:config sourcemapIgnoreList: [Function: isInNodeModules$1],
vite:config middlewareMode: false,
vite:config fs: {
vite:config strict: true,
vite:config allow: [Array],
vite:config deny: [Array],
vite:config cachedChecks: undefined
vite:config }
vite:config },
vite:config preview: {
vite:config port: undefined,
vite:config strictPort: undefined,
vite:config host: undefined,
vite:config https: undefined,
vite:config open: undefined,
vite:config proxy: undefined,
vite:config cors: undefined,
vite:config headers: undefined
vite:config },
vite:config envDir: '/Projects/vite-css-bundling-issue',
vite:config env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
vite:config assetsInclude: [Function: assetsInclude],
vite:config logger: {
vite:config hasWarned: false,
vite:config info: [Function: info],
vite:config warn: [Function: warn],
vite:config warnOnce: [Function: warnOnce],
vite:config error: [Function: error],
vite:config clearScreen: [Function: clearScreen],
vite:config hasErrorLogged: [Function: hasErrorLogged]
vite:config },
vite:config packageCache: Map(1) {
vite:config 'fnpd_/Projects/vite-css-bundling-issue' => {
vite:config dir: '/Projects/vite-css-bundling-issue',
vite:config data: [Object],
vite:config hasSideEffects: [Function: hasSideEffects],
vite:config webResolvedImports: {},
vite:config nodeResolvedImports: {},
vite:config setResolvedCache: [Function: setResolvedCache],
vite:config getResolvedCache: [Function: getResolvedCache]
vite:config },
vite:config set: [Function (anonymous)]
vite:config },
vite:config createResolver: [Function: createResolver],
vite:config worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} },
vite:config appType: 'spa',
vite:config experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
vite:config getSortedPlugins: [Function: getSortedPlugins],
vite:config getSortedPluginHooks: [Function: getSortedPluginHooks]
vite:config } +15ms
vite v5.2.11 building for production...
[plugin:vite:dynamic-import-vars] [plugin vite:dynamic-import-vars] src/themes/theme-loader.ts: invalid import "./${name}.css`, { with: { type: "css" } ". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. For example: import(`./foo/${bar}.js`).
✓ 36 modules transformed.
dist/index.html 0.46 kB │ gzip: 0.30 kB
dist/assets/react-CHdo91hT.svg 4.13 kB │ gzip: 2.05 kB
dist/assets/index-DiwrgTda.css 1.39 kB │ gzip: 0.72 kB
dist/assets/index-CAL_m0jK.js 144.33 kB │ gzip: 46.61 kB
✓ built in 347ms
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I also have an issue with the import-analysis. In my project i get this warning:
[vite] warning: [ng] C:/[project path]/src/.angular/cache/17.3.7/vite/deps/chunk-CK32LNO7.js [ng] 2757| return i[n]; [ng] 2758| } [ng] 2759| return import("./".concat(a, ".entry.js").concat("")).then(function(e2) { [ng] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [ng] 2760| { [ng] 2761| cmpModules.set(a, e2); [ng] The above dynamic import cannot be analyzed by Vite. [ng] See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning. [ng] [ng] Plugin: vite:import-analysis [ng] File: C:/[project path]/src/.angular/cache/17.3.7/vite/deps/chunk-CK32LNO7.js?v=10cb471d
This import should be legit while starting with "./" I do not think there is much I can do here while that code is automatically generated. I can do as suggested to add the "/* @vite-ignore */" part. And that does remove the warning but only untill angular decides to update its cache or i install the project on another machine.
I was on vite 5.1.4 and now tried to update it to 5.2.11 with the same result.
I also have an issue with the import-analysis. In my project i get this warning:
[vite] warning: [ng] C:/[project path]/src/.angular/cache/17.3.7/vite/deps/chunk-CK32LNO7.js [ng] 2757| return i[n]; [ng] 2758| } [ng] 2759| return import("./".concat(a, ".entry.js").concat("")).then(function(e2) { [ng] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [ng] 2760| { [ng] 2761| cmpModules.set(a, e2); [ng] The above dynamic import cannot be analyzed by Vite. [ng] See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning. [ng] [ng] Plugin: vite:import-analysis [ng] File: C:/[project path]/src/.angular/cache/17.3.7/vite/deps/chunk-CK32LNO7.js?v=10cb471d
This import should be legit while starting with "./" I do not think there is much I can do here while that code is automatically generated. I can do as suggested to add the "/* @vite-ignore */" part. And that does remove the warning but only untill angular decides to update its cache or i install the project on another machine.
I was on vite 5.1.4 and now tried to update it to 5.2.11 with the same result.
ignoring it is not a resolution, I need to bundle them correctly and chunk for each css should be created
I updated the issue to clarify expectations
might be related https://github.com/vitejs/vite/issues/14102
ooo please.... seems its only works this way
https://github.com/spamshaker/vite-css-bundling-issue/blob/working/src/theme-loader.ts
Now those two chunks have been created and finally can be reached in runtime:
dist/assets/theme-dark-6jg3YJQ8.css 0.03 kB │ gzip: 0.04 kB dist/assets/theme-light-DdXo6UAR.css 0.03 kB │ gzip: 0.04 kB
full output I got:
npm run build && npm run preview
> [email protected] build
> tsc && vite build
vite v5.2.11 building for production...
✓ 39 modules transformed.
dist/index.html 0.44 kB │ gzip: 0.29 kB
dist/assets/react-CHdo91hT.svg 4.13 kB │ gzip: 2.05 kB
dist/assets/theme-dark-6jg3YJQ8.css 0.03 kB │ gzip: 0.04 kB
dist/assets/theme-light-DdXo6UAR.css 0.03 kB │ gzip: 0.04 kB
dist/assets/index-DiwrgTda.css 1.39 kB │ gzip: 0.72 kB
dist/assets/index-Bu5yqxi-.js 144.72 kB │ gzip: 46.79 kB
✓ built in 345ms
> [email protected] preview
> vite preview
➜ Local: http://localhost:4173/
➜ Network: use --host to expose
➜ press h + enter to show help
Does anyone know if this is gonna be like that or it it's going to be fixed?
here is exactly what I needed and it seems to be working
https://github.com/spamshaker/vite-css-bundling-issue/blob/b6cac1beb197910ad50006a5497ae211e15f492f/node_modules/themes/loader.js?plain=1#L1-L13
this means that it loads it in appropriate way - switch/case is enough in my case. I have put it into node_modules to ensure that it can be bundled when the css is provided as a library
as for the import.meta.glob - it works, however it is not part of any ECMA or tc39 proposals (or I just didn't find it) so I would treat it as a workaround.
At this stage if no objection the Issue could be closed, or the documentation would need to be updated as I saw there are opened couple of similar issues like
- https://github.com/vitejs/vite/issues/14102
- https://github.com/vitejs/vite/issues/15864
I'm having a similar issue (I think) with Vue single file components dynamically loaded via Vite import.meta.glob.
In production, dynamically loaded components are missing a lot of CSS styles. There is no issue when running vite in dev mode.
As suggested in https://github.com/vitejs/vite/issues/10630, this seems to be triggered by the cssCodeSplit: true build option, so cssCodeSplit: false might be a workaround.
Vite does not support CSS import attributes yet (#17700).