最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NextJS - Use sass-loader to add variables to every scss file - Stack Overflow

programmeradmin3浏览0评论

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";',
            },
          },
        },
      ],
    },

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";',
            },
          },
        },
      ],
    },
  })

Share Improve this question asked Dec 30, 2021 at 10:40 Max G.Max G. 8501 gold badge5 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

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

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

发布评论

评论列表(0)

  1. 暂无评论