mrsum / webpack-svgstore-plugin

Simple svg-sprite creating with webpack
https://www.npmjs.com/package/webpack-svgstore-plugin
200 stars 92 forks source link

Use environment variables in path #122

Closed iamrane closed 7 years ago

iamrane commented 7 years ago

Hi! It seems to be something wrong when i use environment variables in my path value. I guess its the order that webpack do things.

From my understanding webpack replaces the env. variable on build, but it seems to me that svg magick is happening before this?

My code looks like

const __svg__ = { path: `./assets/${ process.env.BRAND }/*.svg`, name: 'assets/sprite-[hash].svg' };
require('webpack-svgstore-plugin/src/helpers/svgxhr')(__svg__);

and plugins in config

new SvgStore({prefix: 'icon-'})
mrsum commented 7 years ago

@iamrane think that const isn't parsed cause when plugin was wrote, let/const not support i mean use var instead const

iamrane commented 7 years ago

I changed to var. Still not working. Its just stops at "90% optimize assets" when i start webpack. If i hard code the value of a variable and try to use it, it still doesn't work. I guess its somehow how the svg magic is being used.

var __svg__ = { path: '../../../styles/mybrand/images/svg-icons/**/*.svg', name: '[hash].icons.svg' };
require('webpack-svgstore-plugin/src/helpers/svgxhr')(__svg__);

works fine but not

var brand = 'mybrand';
var __svg__ = { path: '../../../styles/' + brand + '/images/svg-icons/**/*.svg', name: '[hash].icons.svg' };
require('webpack-svgstore-plugin/src/helpers/svgxhr')(__svg__);
iamrane commented 7 years ago

I made it work.

if (process.env.BRAND === 'mybrand') {
   var __svg__ = { path: '../../../styles/mybrand/images/svg-icons/**/*.svg', name: '[hash].icons.svg' };
}
if (process.env.BRAND === 'myotherbrand') {
   var __svg__ = { path: '../../../styles/myotherbrand/images/svg-icons/**/*.svg', name: '[hash].icons.svg' };
}
require('webpack-svgstore-plugin/src/helpers/svgxhr')(__svg__);