How to rewrite urls of images in vendor CSS files using Grunt

前端 未结 3 1873
渐次进展
渐次进展 2020-12-09 02:44

I am trying to move frontend dependencies out of the version control system. A combination of Bower.io and Grunt should be able to do this.

A problem however occurs

3条回答
  •  离开以前
    2020-12-09 03:24

    You probably wat to take a look at this grunt package https://github.com/Ideame/grunt-css-urls. This package seems to be intended to solve exactly your problem.

    Edit: after looking at this plugin I didn't like the idea of rewriting my markup in order to make my build process smoother. So I ended up writing my own tiny function which does the rewrite for me.

    I use grunt's concat plugin for bundling my css files. Good thing about this plugin is that it suports file processing function before concatenation. Now my gruntfile looks like this:

    grunt.initConfig({
    concat: {
        options: {
        separator: '\n',
        process: function (src, filepath) {
            var cssPatt = new RegExp('app(\/.*\/).*\.css$');
    
            //filter out everithing except css files
            var file = cssPatt.exec(filepath);
    
            if (file) {
                var urlPatt = /url\(\'(.*)\'\)/g;
    
            console.log('In file: ' + filepath);
    
            //replace every url(...) with its absolute path
            return src.replace(urlPatt, function (match, p1) {
                console.log(' * ' + match + ' -> ' + 'url(\'' + file[1] + p1 + '\')');
                return 'url(\'' + file[1] + p1 + '\')';
            });
            }
    
            return src;
        }
        },
    }
    

提交回复
热议问题