Image address in ember templates grunt build

前端 未结 4 2081
粉色の甜心
粉色の甜心 2021-01-01 01:51

I used Yeoman to create a web app in EmberJS. Everything works ok, but after using the grunt build command, if I view the built app in the browser (from dist di

相关标签:
4条回答
  • 2021-01-01 02:03

    FWIW, I believe that the author of rev is being opinionated about something here: they're asserting that images should be included as CSS backgrounds, not via img tags. So, if your templates and views get all their images that way, you won't have any trouble.

    IMHO, this is a pretty good convention to follow. Do everything with background images, and the problem should be solved across the application.

    0 讨论(0)
  • 2021-01-01 02:03

    You can transform the images url in the ember compiled template js... here's an example configuration of usemin that does this:

        usemin: {
            html: ['<%= yeoman.dist %>/{,*/}*.html'],
            css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
            options: {
                assetsDirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/css'],
                // The next pattern renames images within ember templates
                patterns: {
                  js: [[/src=['"]([^"']+)["']/gm, 'Update the ember templates JS to reference our revved images']]
                }
            },
            js: ['<%= yeoman.dist %>/scripts/*.templates.js']
        }
    
    0 讨论(0)
  • 2021-01-01 02:04

    Try this:

    usemin: {
                html: ['<%= yeoman.dist %>/**/*.html'],
                css: ['<%= yeoman.dist %>/styles/{,*/}*.css'],
                js: ['<%= yeoman.dist %>/scripts/**/*.js'],
                options: {
                    dirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/styles', '<%= yeoman.dist %>/scripts'],
                    assetsDirs: ['<%= yeoman.dist %>', '<%= yeoman.dist %>/styles', '<%= yeoman.dist %>/scripts'],
                    patterns: {
                        js: [
                            [/["']([^:"']+\.(?:png|gif|jpe?g))["']/img, 'Image replacement in js files']
                        ]
                    }
                }
            }
    

    From here: Grunt plugin for assets versioning

    The regex glob validates, as opposed to other solution shown here.

    Also rolled back to use-min 2.1.1 npm install grunt-usemin@2.1.1 --save-dev

    Although, I think 'assetsDirs' as opposed to just 'dirs' causes the undefined function error?

    0 讨论(0)
  • 2021-01-01 02:10

    Finally i got rid of this:

    all that is needed is to edit the Gruntfile.js in the project's root; the rev task is the one that manage image renaming; usually it is something like this:

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

    You just have to delete the row that tell him to process the images folder:

    rev: {
            dist: {
                files: {
                    src: [
                        '<%= yeoman.dist %>/scripts/{,*/}*.js',
                        '<%= yeoman.dist %>/styles/{,*/}*.css',
                        '<%= yeoman.dist %>/styles/fonts/*'
                    ]
                }
            }
        },
    

    And it is done; all the images will keep their original names and so no path will be updated in css, html or hbs files... Note that the rev task is only responsible for file renaming, not for compression (for images it is done by imagemin task) and so the images will be compressed in any case...

    0 讨论(0)
提交回复
热议问题