vite icon indicating copy to clipboard operation
vite copied to clipboard

Not generating asset for CSS in SSR

Open jaulz opened this issue 2 years ago • 3 comments

Describe the bug

Vite does not output an asset in SSR mode for dynamically imported CSS files. You can reproduce the issue by opening the stackblitz link and run vite build --outDir ssr --ssr server.js. You will see that the generated server.js in ssr folder refers to ./assets/style2.b4683fb7.js (or similar) but the actual file is not generated.

Reproduction

https://stackblitz.com/edit/vitejs-vite-774ugv?file=server.js,style2.css,ssr%2Fserver.js&terminal=dev

System Info

See stackblitz

Used Package Manager

npm

Logs

Click to expand! ```shell ❯ vite build --outDir ssr --ssr server.js --debug vite:config no config file found. +0ms vite:config using resolved config: { vite:config root: '/home/projects/vitejs-vite-774ugv', vite:config base: '/', vite:config mode: 'production', vite:config configFile: undefined, vite:config logLevel: undefined, vite:config clearScreen: undefined, vite:config optimizeDeps: { vite:config disabled: 'build', vite:config force: undefined, vite:config esbuildOptions: { preserveSymlinks: undefined } vite:config }, vite:config build: { vite:config target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ], vite:config polyfillModulePreload: true, vite:config outDir: 'ssr', vite:config assetsDir: 'assets', vite:config assetsInlineLimit: 4096, vite:config cssCodeSplit: true, vite:config cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ], vite:config sourcemap: false, vite:config rollupOptions: {}, vite:config minify: false, vite:config terserOptions: {}, vite:config write: true, vite:config emptyOutDir: null, vite:config manifest: false, vite:config lib: false, vite:config ssr: 'server.js', vite:config ssrManifest: 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 }, vite:config configFileDependencies: [], 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 optimizeDeps: { force: undefined }, vite:config build: { outDir: 'ssr', ssr: 'server.js' } vite:config }, vite:config resolve: { alias: [ [Object], [Object] ] }, vite:config publicDir: '/home/projects/vitejs-vite-774ugv/public', vite:config cacheDir: '/home/projects/vitejs-vite-774ugv/node_modules/.vite', vite:config command: 'build', vite:config ssr: { vite:config format: 'esm', vite:config target: 'node', vite:config optimizeDeps: { disabled: true, esbuildOptions: [Object] } vite:config }, vite:config isWorker: false, vite:config mainConfig: null, vite:config isProduction: true, vite:config plugins: [ vite:config 'vite:build-metadata', vite:config 'vite:pre-alias', vite:config 'alias', 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:watch-package-data', vite:config 'commonjs', vite:config 'vite:data-uri', vite:config 'vite:asset-import-meta-url', 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:reporter', vite:config 'vite:load-fallback' vite:config ], vite:config server: { vite:config preTransformRequests: true, vite:config middlewareMode: false, vite:config fs: { strict: true, allow: [Array], deny: [Array] } 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 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(0) { set: [Function (anonymous)] }, vite:config createResolver: [Function: createResolver], vite:config worker: { vite:config format: 'iife', vite:config plugins: [ vite:config 'vite:build-metadata', vite:config 'vite:pre-alias', vite:config 'alias', 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:watch-package-data', vite:config 'commonjs', vite:config 'vite:data-uri', vite:config 'vite:asset-import-meta-url', 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:reporter', vite:config 'vite:load-fallback' vite:config ], vite:config rollupOptions: {} vite:config }, vite:config appType: 'spa', vite:config experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false } vite:config } +4ms vite v3.0.5 building SSR bundle for production... ✓ 3 modules transformed. ssr/server.js 1.79 KiB ```

Validations

jaulz avatar Aug 09 '22 14:08 jaulz

It seems #9401 is affecting this.

For a workaround, create a facade module.

// server.js
const style2 = await import('./style2.js');
// style2.js
export { default } from './style2.css'
/* style2.css */
body {
  background: red;
}

sapphi-red avatar Aug 09 '22 15:08 sapphi-red

Nice trick, thanks a lot! 😊

jaulz avatar Aug 09 '22 15:08 jaulz

#11429

vite: 4.1.0-beta.1

  build: {
    // ...
+   ssrEmitAssets: true,
    // ...
  },

Shyam-Chen avatar Dec 20 '22 03:12 Shyam-Chen

ssrEmitAssets
export { default } from './style2.css'

Just tried it. It doesn't seem to make a difference. I am using:

  const vite = await createViteServer({
    root: __dirname,
    server: { middlewareMode: true },
    appType: 'custom',
    build: {
      ssr: true,
    },
  })

and css files are still not present in the ssr app on localhost.

Filipeue avatar Feb 20 '23 13:02 Filipeue

Thumbs up. Is ssr an uncommon option in Vite? I was following the documentation today and got stuck at the beginning. (vite + react + fastify..)

x6ax6b avatar Mar 15 '23 12:03 x6ax6b

For what is worth, it works for me now.

kyuumeitai avatar May 30 '23 22:05 kyuumeitai

build.ssrEmitAssets needs to be enabled for this to work. I think this if fixed now, but if not, feel free to open a new issue with a repro.

bluwy avatar May 31 '23 05:05 bluwy