Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not generating asset for CSS in SSR #9602

Closed
7 tasks done
jaulz opened this issue Aug 9, 2022 · 7 comments
Closed
7 tasks done

Not generating asset for CSS in SSR #9602

jaulz opened this issue Aug 9, 2022 · 7 comments
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@jaulz
Copy link

jaulz commented Aug 9, 2022

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

@sapphi-red
Copy link
Member

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 sapphi-red added the p2-edge-case Bug, but has workaround or limited in scope (priority) label Aug 9, 2022
@jaulz
Copy link
Author

jaulz commented Aug 9, 2022

Nice trick, thanks a lot! 😊

@Shyam-Chen
Copy link
Contributor

Shyam-Chen commented Dec 20, 2022

#11429

vite: 4.1.0-beta.1

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

@Filipeue
Copy link

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.

@x6ax6b
Copy link

x6ax6b commented Mar 15, 2023

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

@kyuumeitai
Copy link

For what is worth, it works for me now.

@bluwy
Copy link
Member

bluwy commented May 31, 2023

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 bluwy closed this as completed May 31, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests

7 participants