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: opt out of direct import in SSR when the file is not JS #8454

Closed
wants to merge 1 commit into from

Conversation

cyco130
Copy link
Contributor

@cyco130 cyco130 commented Jun 3, 2022

Description

Currently importing external CSS files in SSR throws ERR_UNKNOWN_FILE_EXTENSION unless those modules are marked as external (reproduction, StackBlitz). This PR solves it by opting out of direct Node import if the resolved file has a non-JS extension.

Additional context

The regex testing if a file is a non-JS file (which I copied from the optimizer code) may require some more thinking.


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.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • 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).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@@ -291,6 +294,10 @@ async function nodeImport(
? { ...resolveOptions, tryEsmOnly: true }
: resolveOptions
)
// These cannot be imported directly
if (url.match(/\.(css|less|sass|scss|styl|stylus|pcss|postcss|json)$/)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

duplicating regex like these over the codebase can lead to errors down the road where it is changed in one place but forgotten in others. Is there a way to put it into a common place like with known js extensions?

export const isJSRequest = (url: string): boolean => {

Copy link
Contributor Author

@cyco130 cyco130 Jun 8, 2022

Choose a reason for hiding this comment

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

I agree but I don't think marko, svelte, or astro files can be directly imported by Node, can they? If we use isJSRequest nodeImport will try a direct import. It would also miss wasm files (I don't know whether it's desirable or not). I couldn't find a good regex for this anywhere in the code.

How about actually trying to import directly and catching ERR_UNKNOWN_FILE_EXTENSION to try again with Vite's loaders? This way we could support custom Node loaders.

@netlify
Copy link

netlify bot commented Jun 28, 2022

Deploy Preview for vite-docs-main canceled.

Name Link
🔨 Latest commit fba2ba8
🔍 Latest deploy log https://app.netlify.com/sites/vite-docs-main/deploys/62bb61fb3227360009423ba5

@@ -690,7 +690,7 @@ export function tryNodeResolve(
// otherwise we may introduce duplicated modules for externalized files
// from pre-bundled deps.
if (!isBuild) {
const versionHash = depsOptimizer.metadata({ ssr }).browserHash
const versionHash = depsOptimizer.metadata({ ssr })?.browserHash
Copy link
Member

Choose a reason for hiding this comment

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

This is always defined, why was this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It isn't defined when the module is CSS (tried with sanitize.css) maybe because it can't be optimized? This is new by the way, the previous version didn't need this.

Copy link
Member

Choose a reason for hiding this comment

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

I tried this repro https://github.com/cyco130/vite-ssr-react/tree/ssr-css-bug, against beta.5 and it is working 🤔
Could you create a failing repro so I can check what is going on here?

Copy link
Member

Choose a reason for hiding this comment

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

I found why you need this, I'll send a PR tomorrow to fix it. Still a repro of the CSS issue against latest would be good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you create a failing repro so I can check what is going on here?

I updated the repro now and it is failing for me. Maybe try rm -rf node_modules/.vite?

@patak-dev patak-dev mentioned this pull request Jun 30, 2022
7 tasks
@patak-dev patak-dev added the p3-significant High priority enhancement (priority) label Jul 1, 2022
@bluwy
Copy link
Member

bluwy commented Jul 1, 2022

I tested the repro with Vite 2.9.13 and plugin-react 1.3.2, and it seems to work fine. Stackblitz. So this might be a regression in Vite 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p3-significant High priority enhancement (priority)
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Unknown file extension ".css"
5 participants