how to get env variable in scss

天涯浪子 提交于 2020-08-04 14:12:46

问题


I want to get one env variable data in my scss file by using mix in Laravel. How to get that?

I have used something like this. But this one is a URL, so, it is not getting.

mix.sass('resources/assets/sass/main-v2.scss', 'public/css', {
    data: `awsUrl: "${(process.env.MIX_AWS_S3_CDN)}" ;`
})

回答1:


There is a simple way to do it. Follow steps as below,

Correct me if wrong, you want to pass an URL to main-v2.scss file as a variable.

  1. Add a variable in your .env file as below,

    MIX_AWS_URL='https://your-aws-endpoint.xyz?v1'

  2. In your webpack.mix.js file, add as below,

        mix.sass('resources/assets/sass/main-v2.scss', 'public/css', {
           data: '$awsUrl:\'' + process.env.MIX_AWS_URL + '\';'
           // single quotes needs to be added as your URL contains : (colon) so, it may create an issue. Better to escape.
        })
    
  3. Next, you can directly use $awsUrl variable in your main-v2.scss file as below.

        // Pass to function
        @import url($awsUrl);
    
       // Or assign to another variable
       $myVariable : $awsUrl;
    

That's it!




回答2:


I only got it working requiring dotenv myself inside webpack.mix.js.

This should work:

let mix = require('laravel-mix');
require('dotenv').config();

mix.sass('resources/assets/sass/main-v2.scss', 'public/css', {
    data: '$awsUrl:\'' + process.env.MIX_AWS_URL + '\';'
})


来源:https://stackoverflow.com/questions/57111930/how-to-get-env-variable-in-scss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!