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

fix!: default build.cssMinify to 'esbuild' for SSR #15637

Merged

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Jan 18, 2024

Description

Even for SSR, there's no reason to skip minify for CSS files.

SvelteKit and Astro sets this.
https://github.com/sveltejs/kit/blob/6200a48befe538176b1f5c1cb9e32c51af84345b/packages/kit/src/exports/vite/index.js#L607
https://github.com/withastro/astro/blob/7e1db8b4ce2da9e044ea0393e533c6db2561ac90/packages/astro/src/core/build/static-build.ts#L178-L180

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added has workaround p2-nice-to-have Not breaking anything but nice to have (priority) breaking change labels Jan 18, 2024
Copy link

stackblitz bot commented Jan 18, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

patak-dev
patak-dev previously approved these changes Jan 18, 2024
@patak-dev patak-dev added this to the 5.1 milestone Jan 18, 2024
@patak-dev
Copy link
Member

@sapphi-red you marked this as a breaking change, would we still be able to merge it for Vite 5.1? Vite 6 is too far away right now.

@patak-dev
Copy link
Member

Ah, ok, I see:

I think we could still merge this one in Vite 5.1, but maybe I'm missing something here.

@patak-dev patak-dev removed this from the 5.1 milestone Jan 18, 2024
@sapphi-red
Copy link
Member Author

I think the change has a slightly big impact. Given that there's a workaround, I think it's better to wait for Vite 6.

@patak-dev patak-dev added this to the 6.0 milestone Jan 18, 2024
@bluwy
Copy link
Member

bluwy commented Jan 18, 2024

Should we also only minify only if ssrEmitAssets is true? Otherwise the minified result is thrown away.

@sapphi-red
Copy link
Member Author

Should we also only minify only if ssrEmitAssets is true? Otherwise the minified result is thrown away.

Even if ssrEmitAssets is false, the user wants the same file hash for CSS files (also for other assets). To get the same file name between client and server, the same content needs to be generated. So I think we need to enable minify.

@bluwy
Copy link
Member

bluwy commented Jan 18, 2024

I don't quite understand how one would compare the same file name between client and server if ssrEmitAssets is false? There won't be any assets in the first place. And the JS generated by client and server builds would already be different.

@bluwy
Copy link
Member

bluwy commented Jan 18, 2024

Ah I guess checking the Remix issue, it's because the hash is exposed for .css?url 🤔

@sapphi-red
Copy link
Member Author

Yep, the the hash is exposed for .css?url. The URL on the server points to nothing.
Example: https://stackblitz.com/edit/vitejs-vite-8zxxdh?file=main.js

@@ -451,7 +451,7 @@ export function resolveBuildEnvironmentOptions(
}

if (resolved.cssMinify == null) {
resolved.cssMinify = !!resolved.minify
resolved.cssMinify = raw.ssr ? 'esbuild' : !!resolved.minify
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using raw.ssr to align with minify: raw.ssr ? false : 'esbuild', above. Do we want to update that to consumer === 'server'? I guess we don't want to, because we would want to enable minify for edge runtimes.

Copy link
Member

@bluwy bluwy Oct 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand the question, would edge runtimes be consumer == 'server' true too? Checking consumer seems correct to me, unless we want external environments to decide on the default themselves.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would edge runtimes be consumer == 'server' true too?

Yes, it would. Just that enabling minify for edge runtimes are more sensible default as they commonly have a file size limit. (nitro does that: base-worker, netlify edge, cloudflare workers, vercel edge).
But on second thought, since we don't have a concept of edge runtime anymore, probably we can just disable minify for all consumer === 'server'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm even more confused now 😄 If we're talking about CSS minify, isn't this PR about enabling that for SSR (or server consumers) by default so that they match the client output?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct. The comments were talking about minify option (not the cssMinify option). It was suggesting to change minify: raw.ssr ? false : 'esbuild' to minify: consumer === 'server' ? false : 'esbuild' or minify: consumer === 'server' && !notWorker ? false : 'esbuild'. Why this is related to this PR is because the condition should be same between minify and cssMinify. For example, if minify is changed to minify: consumer === 'server' && !notWorker ? false : 'esbuild', then cssMinify should be cssMinify: consumer === 'server' && !notWorker ? 'esbuild' : !!resolved.minify. It is because we want to set a different default value from !!resolved.minify only when resolved.minify was implicitly set to false (= when the default value was used).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah everything makes sense now, I didn't realize you we're talking about changing to consumer === 'server' to the minify option.

My first intuition is that I agree with you at "I guess we don't want to, because we would want to enable minify for edge runtimes.", but then we're partial towards edge runtime being extended, e.g. what if it's a rsc environment.

notWorker check seems to be a better addition check to alleviate this case, would it be based on webCompatible? I guess my only fear with all these later is that it could be hard to explain in docs.

Would be good to have @patak-dev chime in too as this relates to fallback values for different environments.

Copy link
Member

@patak-dev patak-dev Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we missed this one. We should move to minify: consumer === 'server' ? false : 'esbuild' (for both).

About edge environments, they can set minify: false as their default:

function edgeEnvironment(userConfig) {
  return mergeConfig({
      build: {
        minify: false
      }
      dev: {
        createEnvironment: ...
      }
    },
    userConfig
  );
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with consumer === 'server' 👍

@patak-dev patak-dev merged commit f1d3bf7 into vitejs:main Oct 24, 2024
14 checks passed
@sapphi-red sapphi-red deleted the fix/default-build-css-minify-esbuild-ssr branch October 24, 2024 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change has workaround p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants