electron-webpack icon indicating copy to clipboard operation
electron-webpack copied to clipboard

Can’t use __static on SCSS files

Open IGassmann opened this issue 7 years ago • 11 comments

SCSS' url('...') statements can't be resolved using electron-webpack's __static.

This works: src: url('../../../static/font/somewebfont.eot');

This doesn't: src: url(__static + 'font/somewebfont.eot'); neither this: src: url('#{$__static}font/somewebfont.eot');

IGassmann avatar Dec 18 '17 13:12 IGassmann

Any news on this? This is pretty breaking. I'm trying to figure this out as well.

MatthewFrench avatar Mar 08 '18 16:03 MatthewFrench

Okay I found a working solution - but only for scss files.. Turns out that sass-loader supports variables, which css-loader doesn't (not sure about less-loader) It works in development and in production, so that's good.

Usage is like this:

.my-div {
    background: url($__static + '/some-image.png');
} 

The change itself was this: image (just hacked into the compiled node_modules/electron-webpack/out/targets/RendererTarget.js)

Is it worth making a pull request for this? @develar what do you think of it? "Partial solution" better than no solution at all? If we document it, we need to state that this only works with scss files. We could not document it for now and strive for a more generic solution..

loopmode avatar Mar 09 '18 07:03 loopmode

I guess a "proper" solution would be using postcss. Will investigate.

loopmode avatar Mar 09 '18 07:03 loopmode

I'd appreciate a partial solution pull request while a better solution is in the works.

MatthewFrench avatar Mar 12 '18 13:03 MatthewFrench

I'm in the same boat here and don't like the idea of messing around with anything in node_modules/ as we have multiple devs that need this support

chriswetterman avatar Apr 03 '18 19:04 chriswetterman

@seanyesmunt

IGassmann avatar May 01 '18 14:05 IGassmann

@develar I think the sass support as shown in my screenie up there is good enough and should be implemented. It simply provides a $__static variable to all processed sass files. Would you mind just putting it in? I volunteer to update the docs and explain it. It's "only in sass" and "not in any stylesheets" but it's better than nothing. Plus this particular issue is only about sass..

loopmode avatar May 02 '18 16:05 loopmode

I've fixed it this way, no patching required:

  1. Add the following to your webpack configuration:
{
  resolve: {
    alias: {
      '@static': path.resolve ( __dirname, 'static' )
    }
}
  1. Write paths like this in your .scss files: ~@static/foo/bar

fabiospampinato avatar Sep 09 '18 01:09 fabiospampinato

I've fixed it this way, no patching required:

  1. Add the following to your webpack configuration:
{
  resolve: {
    alias: {
      '@static': path.resolve ( __dirname, 'static' )
    }
}
  1. Write paths like this in your .scss files: ~@static/foo/bar

This is not restricted to SCSS only but also works with plain .css files. However, we might want to add this info to the docs somewhere.

loopmode avatar Apr 13 '19 05:04 loopmode

Actually, I'd go one step further: I suggest we provide this alias out-of-the-box in the first place!

@develar Any objections? Currently, we have the @ and the common aliases. It would mean adding a third one: "@static": path.resolve(this.projectDir, "static"). I tested and it doesn't interfere with the @ alias. It works fine in the production build as well.

Of course it would be great if we could get rid of the tilde, but even with the tilde necessary we should provide this alias. It effectively allows using images from the static folder in stylesheets - otherwise a real issue that needs a custom webpack addition as a workaround.

UPDATE: We could also set a default resolve.modules = ['node_modules', 'static']. In that case, for using an image static/electron.png, you just write background-image: url('electron.png'); in the CSS. Not sure whether this will impact performance negatively in projects with large static/ contents, tho.

loopmode avatar Apr 13 '19 06:04 loopmode

https://github.com/loopmode/electron-webpack-static-examples

loopmode avatar Aug 17 '20 08:08 loopmode