问题
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.
Add a variable in your
.envfile as below,MIX_AWS_URL='https://your-aws-endpoint.xyz?v1'In your
webpack.mix.jsfile, 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. })Next, you can directly use
$awsUrlvariable in yourmain-v2.scssfile 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