Max G.
Max G.

Reputation: 850

NextJS - Use sass-loader to add variables to every scss file

I'm trying to add a custom webpack configuration to my nextjs project. The goal is to automatically import @import "src/styles/variables.scss"; for every scss files in my app.

I have a webpack configuration for storybook which works as expected, but I don't succeed to make it works for nextJS.

There is my Storybook config:

webpackFinal: async (config, { configType }) => ({
    ...config,
    module: {
      ...config.module,
      rules: [
        ...config.module.rules,
        {
          test: /\.scss$/,
          use: ['style-loader', 'css-loader', 'sass-loader'],
          include: path.resolve(__dirname, '../'),
        },
        {
          test: /\.s[ac]ss$/i,
          use: {
            loader: 'sass-loader',
            options: {
              additionalData: '@import "src/styles/variables.scss";',
            },
          },
        },
      ],
    },

There is my next.config.js

webpack: (config) => ({
    ...config,
    module: {
      ...config.module,
      rules: [
        ...config.module.rules,
        {
          test: /\.scss$/,
          use: ['style-loader', 'css-loader', 'sass-loader'],
          include: path.resolve(__dirname, './'),
        },
        {
          test: /\.s[ac]ss$/i,
          use: {
            loader: 'sass-loader',
            options: {
              additionalData: '@import "src/styles/variables.scss";',
            },
          },
        },
      ],
    },
  })

Upvotes: 2

Views: 10633

Answers (1)

Max G.
Max G.

Reputation: 850

We can directly add sassOptions in next.config.js without sass-loader.

sassOptions: {
    additionalData: `@import "src/styles/variables.scss"; @import "src/styles/mixins.scss";`,
 },

Upvotes: 14

Related Questions