Laravel-mix: How to pass env variable to scss file?

Created on 9 May 2019  路  6Comments  路  Source: JeffreyWay/laravel-mix

In webpack.mix.js I can access an env variable withprocess.env.MIX_URL, but how can I pass that variable to use is an scss file compiled via:

mix.sass('site.scss', 'public/css')

So we can call it like this:

@font-face {
  font-family: 'My-Font';
  src: url('http://www.site.com/fonts/My-Font.ttf'); // orig working
  src: url(process.env.MIX_URL+'/fonts/My-Font.ttf'); // var not working
}

Any idea how to make it work?

Most helpful comment

You are looking at wrong place:

https://github.com/webpack-contrib/sass-loader#environment-variables

All 6 comments

Anyone?

You are looking at wrong place:

https://github.com/webpack-contrib/sass-loader#environment-variables

Thank you so much for the link, it solves the issue!

For people coming here, I think the implied answer was to use the prependData feature of sass-loader?
https://github.com/webpack-contrib/sass-loader#prependdata

Haven't tried it myself yet, but it looks like it can do the job.

// webpack
{
  loader: 'sass-loader',
  options: {
    prependData: '$env: ' + process.env.NODE_ENV + ';',
  }
}

@MovingGifts can you plz explain how did you solve the issue?

How can we implement this in Next js ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jpmurray picture jpmurray  路  3Comments

stefensuhat picture stefensuhat  路  3Comments

Cheddam picture Cheddam  路  3Comments

Bomavi picture Bomavi  路  3Comments

rlewkowicz picture rlewkowicz  路  3Comments