[英]Symfony 6 & webpack encore : how use a .env variable from scss files?
I make a webapp with the framework Symfony (version 6) and I use a common value from my js and scss files.我使用框架 Symfony(版本 6)创建了一个 Web 应用程序,并使用了我的 js 和 scss 文件中的公共值。 In the futurs, I would like change easly this value, so I set the value in a global variable in the.env file.在未来,我想轻松更改此值,因此我在 .env 文件中的全局变量中设置了该值。
Next, I tried to modify my webpack.config.js
file for pass the value of the global variable.接下来,我尝试修改我的webpack.config.js
文件以传递全局变量的值。 I guess I must modify the line .enableSassLoader()
, but I don't find a doc for show how do that...我想我必须修改行.enableSassLoader()
,但我没有找到显示如何做的文档......
How can I pass a variable to my scss?如何将变量传递给我的 scss?
Thanks for any help !谢谢你的帮助 !
The (very simple) answers:) (非常简单的)答案:)
In the webpack.config.js
file:在webpack.config.js
文件中:
let dotenv = require('dotenv');
const env = dotenv.config();
if (env.error) {
throw env.error;
}
const foo = env.parsed.foo;
// ...
Encore
// ...
.enableSassLoader((options)=>{
options.additionalData = `$faa:${foo};`
// don't forget the ;
// now, the var 'faa' can be used in scss files
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.