2016-03-05 36 views
0

我試圖用/ mydir /替換HTML基本標記的href屬性值。已經嘗試過使用grunt-usemin的blockReplacements。不能用grunt-usemin的自定義塊替換HTML基本標記替換

下面是配置

// Reads HTML for usemin blocks to enable smart builds that automatically 
// concat, minify and revision files. Creates configurations in memory so 
// additional tasks can operate on them 
useminPrepare: { 
    html: ['<%= yeoman.client %>/index.html'], 
    options: { 
    dest: '<%= yeoman.dist %>/public' 
    } 
}, 

// Performs rewrites based on rev and the useminPrepare configuration 
usemin: { 

    html: ['<%= yeoman.dist %>/public/{,*/}*.html'], 
    css: ['<%= yeoman.dist %>/public/{,*/}*.css'], 
    js: ['<%= yeoman.dist %>/public/{,*/}*.js'], 
    options: { 
    assetsDirs: [ 
     '<%= yeoman.dist %>/public', 
     '<%= yeoman.dist %>/public/assets/images' 
    ], 
    blockReplacements: { 
     baseUrl: function (block) { 
     grunt.log.debug("******************* blockReplacements *******************"); 
     return '<base href="/mydir/">'; 
     } 
    }, 

    // This is so we update image references in our ng-templates 
    patterns: { 
     js: [ 
     [/(assets\/images\/.*?\.(?:gif|jpeg|jpg|png|webp|svg))/gm, 'Update the JS to reference our revved images'] 
     ] 
    } 

    } 
} 

我的HTML正在調用usemin:

<!-- build:baseUrl /mydir/ --> 
<base href="/"> 
<!-- endbuild --> 

我運行:

$ grunt serve:dist 

但這只是去掉了基本標記,並且不更換。 我有一個懷疑,我需要在Gruntfile中配置一個baseUrl屬性,但我不試圖修改/ uglify文件,並相信這個屬性會指向需要改變的文件等。

另外我試過運行調試發送grunt.log.debug調用buildReplace函數:

$ grunt --debug serve:dist 

但是沒有控制檯輸出。

注意:我的應用程序基於角度全堆生成器應用程序。 https://github.com/angular-fullstack/generator-angular-fullstack

任何幫助或建議表示讚賞。

+0

是不是'base'而不是'baseUrl'? – mika

回答

0

沒有得到任何地方後,我還沒有回過這個問題了一會兒。剛剛重新訪問,似乎從grunt-usemin「:」〜「2.1.1」升級到grunt-usemin「:」3.0.0「修復了這個問題。如果仍然無法確定具體的錯誤是地址在3.0.0

還要注意解決這個問題:的baseUrl在Gruntfile被分配第一個參數是塊函數:

baseUrl: function (block) { 
    grunt.log.debug("******************* blockReplacements *******************"); 
    return '<base href="/mydir/">'; 
} 

此參數命名塊不實際上在函數中使用,href的返回值是一個字符串(我知道這是不好的,不應該用這種方式,而只是指出一些東西)。在客戶對此函數的調用/ index.html的必須傳遞給它的參數:

<!-- build:baseUrl /mydir/ --> 
<base href="/"> 
<!-- endbuild --> 

如果你拿走/ MYDIR/URL部分以下將無法創建基本標籤:

<!-- build:baseUrl --> 
<base href="/"> 
<!-- endbuild --> 

所以實際上我們可以添加/ some-rubbish-url-portion /並且它不會失敗,並且不會對基本標記的結果產生任何影響。

0

您必須使用此命令: 咕嚕打造