2

我正在尝试将图片填充插件用于响应式图像,但 usemin 插件似乎没有在<img src=标签之外获取任何 url。有没有办法强制它使用新的缩小/缓存爆破的原始图像更改每次出现的原始图像,如下所示:

在 grunt 构建之前

// This works
<img src="images/main.jpg" />

<picture>

    // This doesn't work
    <source srcset="images/main.jpg" media="(min-width: 1200px)" />
    <source srcset="images/main-lg.jpg" media="(min-width: 992px)" />
    <source srcset="images/main-md.jpg" media="(min-width: 768px)" />
    <source srcset="images/main-sm.jpg" media="(min-width: 1px)" />

    // This doesn't work
    <img srcset="images/main.jpg" alt="Epic punting">

</picture>

咕噜声构建后

// Updated
<img src="images/b1kajsmf.main.jpg" />

<picture>

    // Not updated
    <source srcset="images/main.jpg" media="(min-width: 1200px)" />
    <source srcset="images/main-lg.jpg" media="(min-width: 992px)" />
    <source srcset="images/main-md.jpg" media="(min-width: 768px)" />
    <source srcset="images/main-sm.jpg" media="(min-width: 1px)" />

    // Not updated
    <img srcset="images/main.jpg" alt="Epic punting">

</picture>

如果有帮助,这是我的 gruntfile 代码:

rev: {
    dist: {
        files: {
            src: [
                '<%= yeoman.dist %>/public/scripts/{,*/}*.js',
                '<%= yeoman.dist %>/public/styles/{,*/}*.css',
                '<%= yeoman.dist %>/public/images/{,*/}*.{png,jpg,jpeg,gif,webp,svg}'
            ]
        }
    }
},

useminPrepare: {
    html: ['<%= yeoman.app %>/views/index.html',
           '<%= yeoman.app %>/views/index.jade'],
    options: {
        dest: '<%= yeoman.dist %>/public'
    }
},

usemin: {
    html: ['<%= yeoman.dist %>/views/{,*/}*.html',
           '<%= yeoman.dist %>/views/{,*/}*.jade'],
    css: ['<%= yeoman.dist %>/public/styles/{,*/}*.css'],
    options: {
        assetsDirs: ['<%= yeoman.dist %>/public']
    }
},

// The following *-min tasks produce minified files in the dist folder
imagemin: {
    options : {
        cache: false
    },
    dist: {
        files: [{
            expand: true,
            cwd: '<%= yeoman.app %>/images',
            src: '{,*/}*.{png,jpg,jpeg,gif}',
            dest: '<%= yeoman.dist %>/public/images'
        }]
    }
}
4

1 回答 1

1

Usemin现在支持blockReplacements. 这允许您定义自己的自定义替换,例如srcset.

请参阅https://github.com/yeoman/grunt-usemin/pull/337Usemin自述文件。

于 2014-09-28T08:00:43.900 回答