我关注了这篇文章How to use SCSS variables into my React components or this one React and SCSS export a variable from scss to react to get scss variable in my react app 但它不起作用。
myvar.scss文件:
$color: red;
:export {
color: $color;
}
.myclass {
background-color: $color;
}
App.js文件:
import variables from './myvar.scss';
const App = () => {
console.log(variables);
return <div className="myclass">Hello</div>
}
export default App;
div 背景是红色的,所以 myvar.scss 正在工作。但是variables
是一个空对象。
(react版本:17.0.1)
node_modules\react-scripts\config\webpack.config.js
module: {
strictExportPresence: true,
rules: [
{ parser: { requireEnsure: false } },
{
oneOf: [
...
{
test: sassRegex,
exclude: sassModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
},
'sass-loader'
),
sideEffects: true,
},