How should I configure grunt-usemin to work with relative path

前端 未结 9 1648
予麋鹿
予麋鹿 2021-01-30 23:13

I have a grunt project backed by a yeoman-generator that I\'ve built based on the generator-webapp, if it\'s of any help, you can find it on GitHub

The grunt project mak

9条回答
  •  情话喂你
    2021-01-31 00:01

    I believe that you can achieve what you need in this way:

    Html file:

        
        
        
    
        
        
    

    Gruntfile.js

            useminPrepare: {
                options: {
                    dest: '<%= yeoman.dist %>/'
                },
                html: ['<%= yeoman.app %>/snippets/head.html','<%= yeoman.app %>/snippets/tail.html']
            },
            usemin: {
                options: {
                    dirs: ['<%= yeoman.dist %>/'],
                    blockReplacements: {
                        css: function (block) {
                            return '';
                        },
                        js: function (block) {
                            return '';
                        }
                    }
                },
                html: ['<%= yeoman.dist %>/{,*/}*.html'],
                css: ['<%= yeoman.dist %>/styles/{,*/}*.css']
            }
    

    The key solution is in the blockReplacements option of the usemin task. Basically, the task will put your files under <%= yeoman.dist %>/styles/main.css, while your html will be under <%= yeoman.dist %>/en/somefileinEnglish.html and every instance of 'styles/main.css' in this file will be replaced with '../styles/main.css', adding the correct relative path.

    As an extra tip, if you are building a multilingual website, you may want to consider grunt-i18n to translate your file while building, so you won't need to maintain a different html file for every language.

提交回复
热议问题