2016-10-26 7 views
0

私のプロジェクトのindex.htmlファイルsrcを別のsrcに変更しようとしています。Gruntを使用してindex.html src属性値を別の値に変更する

私は今、私はこの

<script src="bower_components/jquery/dist/jquery.min.js"></script> 
<script src="bower_components/angular/angular.min.js"></script> 

私はうなり声を使用して、これをどのように行うことができますようにそれを変換したい現在

<script src="../bower_components/jquery/dist/jquery.min.js"></script> 
<script src="../bower_components/angular/angular.min.js"></script> 

持っています?

ありがとうございました

+0

重複質問http://stackoverflow.com/questions/20020981/grunt-how-to-replace-paths-in-html-file -using-grunt-task –

+0

@danywalls私はそれが同じ質問ではないと思います。それ以外の方法で行う必要があります –

+0

@ Kawsar-ahmed私はあなたが環境や場所ごとにパスを設定したいと思っています。このhttps://www.npmjs.com/package/grunt-processhtmlパッケージが役立ちます。 –

答えて

0

@danywallsの提案は正しいです。

プラグインgrunt-processhtmlは、あなたの要件を確実に満たすことができます。

まず、あなたのpackage.jsonにそのプラグインを追加します。

$ npm install grunt-processhtml --save-dev

次のように続いて、ソースindex.htmlであなたの<script>タグの前後special commentsを追加します。

<!--build:js bower_components/jquery/dist/jquery.min.js--> 
<script src="../bower_components/jquery/dist/jquery.min.js"></script> 
<!--/build--> 

<!--build:js bower_components/angular/angular.min.js--> 
<script src="../bower_components/angular/angular.min.js"></script> 
<!--/build--> 

注:<!--build:js ... -->コメント単にあなたの中結果のindex.htmlに含めるsrcパスを指定します。

次はあなたのGruntfile.jsにこれと同様のタスクを追加します。

module.exports = function (grunt) { 

    grunt.initConfig({ 

     pkg: grunt.file.readJSON('package.json'), 

     /* Update script src attribute in .html */ 
     processhtml: { 
      updateScriptLinks: { 
       files: { 
        // NOTE: Adjust the paths below according to your project directory structure 
        './dist/index.html': ['./src/index.html'] // destination : source 
       } 
      } 
     } 

    }); 

    grunt.loadNpmTasks('grunt-processhtml'); 

    grunt.registerTask('default', [ 
     'processhtml:updateScriptLinks' 
    ]); 

}; 
関連する問題