skip to Main Content

I have just created a new React project w/ SASS and I wanted to use Vite instead of Webpack.
The project I used to previously work on would allow me to have named imports and omit .module from the imports like

import style from './styles/MyComponent.scss';

// applying a style in the jsx file
    <p className={style.myParagraph>

However vite returns this error and the styles won’t be applied
Default and named imports from CSS files are deprecated. Use the ?inline query instead. For example: import style from "./styles/MyComponent.scss?inline"

Here’s my vite.config.js and what I tried so far

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  css: {
    modules: {
      include: /.scss$/,
      scopeBehaviour: 'local',
      localsConvention: 'camelCase'
    },
    preprocessorOptions: {
      scss: {
        include: /.scss$/,
        localsConvention: 'camelCase'
      }
    },
  },
  build: {
    cssCodeSplit: false,
    minify: true,
  },
})

2

Answers


  1. Chosen as BEST ANSWER

    I did try the solution I got in the error right away and I thought it didn't work because it kinda reset all the styles and elements went from

    https://phpout.com/wp-content/uploads/2023/09/eIXGi.png

    To

    https://phpout.com/wp-content/uploads/2023/09/ZJojQ.png

    Also I read the docs https://vitejs.dev/guide/features.html#disabling-css-injection-into-the-page and thought ?inline query would simply disable the style.

    The automatic injection of CSS contents can be turned off via the ?inline query parameter. In this case, the processed CSS string is returned as the module's default export as usual, but the styles aren't injected to the page.
    

  2. You get the error Default and named imports from CSS files are deprecated. Use the ?inline query instead because its deprecated in Vite 4.

    Importing a css like:

    import thing from './global.css';

    Will fundamentally cause a double loading of CSS, since vite will emit a .css file, but it’s likely that the CSS will also be used by the application code, being most likely used by the framework runtime to inject the style.

    https://github.com/vitejs/vite/pull/10762#issue-1432916886

    So to fix it, change you import from:

    import style from './styles/MyComponent.scss';
    

    to:

    import style from './styles/MyComponent.scss?inline';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search