简体   繁体   English

NextJS - 使用 sass-loader 将变量添加到每个 scss 文件

[英]NextJS - Use sass-loader to add variables to every scss file

I'm trying to add a custom webpack configuration to my nextjs project.我正在尝试将自定义 webpack 配置添加到我的 nextjs 项目中。 The goal is to automatically import @import "src/styles/variables.scss";目标是自动导入@import "src/styles/variables.scss"; for every scss files in my app.对于我的应用程序中的每个 scss 文件。

I have a webpack configuration for storybook which works as expected, but I don't succeed to make it works for nextJS.我有一个故事书的 webpack 配置,它按预期工作,但我没有成功使它适用于 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有我的 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";', }, }, }, ], }, })

SOLVED解决了

We can directly add sassOptions in next.config.js without sass-loader...我们可以直接在 next.config.js 中添加 sassOptions 而无需 sass-loader...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM