简体   繁体   English

Symfony 6 & webpack encore:如何使用 scss 文件中的 a.env 变量?

[英]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.

相关问题 symfony / webpack-encore:如何使 colors in.scss 来自 ENV var? 哪些缓存需要重建? - symfony / webpack-encore: How can I make colors in .scss to come from an ENV var? What caches have to be rebuilt? 生产环境中的Symfony 4和Webpack Encore,我的CSS和JS生成的文件未加载 - Symfony 4 & webpack encore in production env, my css and js generated files aren't loaded 如何在 Symfony 项目中使用带有 webpack-encore 的 jQuery? - How to use jQuery with webpack-encore in a Symfony project? symfony webpack用数据表重新添加了巨大的js文件 - symfony webpack encore huge js files with datatables 产品环境中的 Symfony 5 和 webpack 再来一次:避免在构建文件名中使用 hash 字符串 - Symfony 5 and webpack encore in prod env : avoid hash string in build filename 如何将 webpack 与 sylius 一起使用? - How to use webpack encore with sylius? 来自带有symfony 4的socket / io的webpack / encore - webpack/encore from symfony 4 with socket.io Symfony Webpack Encore:从捆绑包导入.js - Symfony Webpack Encore : Import .js from Bundle 如何解决Symfony 4 webpack / encore加载错误? - How to resolve Symfony 4 webpack/encore loading error? webpack-encore 如何与 symfony 5 配合使用? - How does webpack-encore works with symfony 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM