admin管理员组文章数量:1287869
I am moving my build system from Grunt with custom tasks to Webpack. As for JavaScript modules it works great, but I'm not so sure what to do with my Sass stylesheets.
I have dependencies on Sass files in my AMD modules, which Webpack can read and generate bundle.css from. But I would ideally like to have my build pipeline generate sprites using spritesmith, then copy the images to the build dir and use the Sass mixins to generate correct CSS rules.
I have researched this a lot both on SO and Google, but haven't found anyone doing similar scenario. Should I use solely webpack? Or should I maybe have separate Grunt task watching the images, generating sprites and then run Webpack over that?
I am moving my build system from Grunt with custom tasks to Webpack. As for JavaScript modules it works great, but I'm not so sure what to do with my Sass stylesheets.
I have dependencies on Sass files in my AMD modules, which Webpack can read and generate bundle.css from. But I would ideally like to have my build pipeline generate sprites using spritesmith, then copy the images to the build dir and use the Sass mixins to generate correct CSS rules.
I have researched this a lot both on SO and Google, but haven't found anyone doing similar scenario. Should I use solely webpack? Or should I maybe have separate Grunt task watching the images, generating sprites and then run Webpack over that?
Share Improve this question asked Mar 1, 2015 at 20:03 VoYVoY 5,6992 gold badges40 silver badges47 bronze badges2 Answers
Reset to default 5I have a similar problem, I have a directory full of png files that I need to convert to CSS so that a div with the correct class name will load the image. I would like to use url-loader so that small files are inlined.
The problem is of course that you can't specify a directory as something a loader should load, only existing files.
My solution is to create a custom loader in the png dir and give it itself as input, so it can glob the files in its own dir and export the CSS with all the require statements for the png files.
This is the webpack entry for it:
'!!style!css!./resources/images/images-as-css-loader.coffee!./resources/images/images-as-css-loader'
The loader code (coffeescript but you get the idea):
glob = require 'glob'
path = require 'path'
sizeOf = require 'image-size'
module.exports = (dummy) ->
this.cacheable true
dir = path.dirname this.resourcePath
entries = for file in glob.sync "#{dir}/*.png"
@addDependency file
className = (path.basename file, '.png')
imgDim = sizeOf file
"""
.#{className} {
width: #{imgDim.width}px;
height: #{imgDim.height}px;
background-repeat: no-repeat;
background-image: url('~!!url?limit=1024!#{file}');
}
"""
# Return CSS text
return entries.join ''
You can then use the webpack text extract plugin to move the CSS to a file, which I do for the production build. For dev I just let the style loader inject it.
I had similar problem, the only solution I've been able to e up with was to write my own plugin.
本文标签: javascriptHow to use spriting with Webpack and SassStack Overflow
版权声明:本文标题:javascript - How to use spriting with Webpack and Sass? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741329964a2372713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论