histoire icon indicating copy to clipboard operation
histoire copied to clipboard

Error: Could not parse CSS stylesheet

Open teleskop150750 opened this issue 2 years ago • 2 comments

Describe the bug

JSDOM doesn't understand CSS well

Error: Could not parse CSS stylesheet

image

image

Similar error

Reproduction

https://github.com/teleskop150750/Histoire

System Info

Windows 11, Google Chrome 114

Used Package Manager

pnpm

Validations

teleskop150750 avatar Jul 01 '23 12:07 teleskop150750

Probably need to find a way to mock all CSS files

Akryum avatar Aug 13 '23 20:08 Akryum

For the time being a came up with the following vite plugin, that needs to be added as a plugin in histoire.config.ts. This will clean all the CSS errors that pollute the console.

The vite plugin :

import type { Plugin } from 'vite'

/**
 * Custom Vite plugin to filter out CSS errors from the console
 * https://github.com/histoire-dev/histoire/issues/501
 */

// Custom Vite plugin to modify console behavior
export async function cleanCssErrors(): Promise<Plugin> {
  return {
    name: 'console-error-filter',
    async transform(code, id) {
      if (id.includes('histoire-setup')) {
        const consoleFilterCode = `
          const originalConsoleError = console.error
          const jsDomCssError = 'Error: Could not parse CSS stylesheet'
          console.error = (...params) => {
            if (!params.find(p => p.toString().includes(jsDomCssError))) {
              originalConsoleError(...params)
            }
          }
        `
        return {
          code: consoleFilterCode + code,
          map: null,
        }
      }
    },
  }
}

Changes to histoire.config.ts :

export default defineConfig({
  // ... other settings
  vite: {
    plugins: [
      cleanCssErrors(),
    ],
  },
})

This solution has been inspired by https://github.com/jsdom/jsdom/issues/2177#issuecomment-1724971596

nikosgpet avatar Oct 12 '24 13:10 nikosgpet