sassvite

How to replace includePaths in vite config when using the modern sass compiler


I'm migrating a project to the new sass compiler, but it seems that vite is not passing the includePaths

{
css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        includePaths: [ // This needs to be replaced
          path.resolve("views"),
          path.resolve("views/scss/theme"),
        ],
      },
    },
  },
}

I get this error when building

[sass] Can't find stylesheet to import.
@import "views/scss/front/product/common";
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

What is the correct way to add include paths to the sass config?

Here's what I tried

{
  css: {
    preprocessorOptions: {
      scss: {
        api: 'modern-compiler',
        importers: [
        {
          findFileUrl(url) {
            console.log(url); // this was not logged, so the importer was not called
            // if (!url.startsWith('~')) return null;
            // return new URL(url.substring(1), pathToFileURL('node_modules'));
          }
        }
      ]
      },
    },
  }
}

Solution

  • I bumped into the same issue after upgrading vite to v6.0.0. As far as I understand, includePaths is renamed to loadPaths in new modern API.

    Docs: https://sass-lang.com/documentation/js-api/interfaces/options/#loadPaths

    So this is the relevant part in my vite.config.ts now:

    export default defineConfig({
      css: {
        preprocessorOptions: {
          scss: {
            loadPaths: ["./src/styles"],
          },
        },
      },
    });