2016-03-30 11 views
1

GruntタスクでいくつかのHTMLファイルをコピーしていて、jQueryを使用して操作したいと思っています。 。 。GruntタスクでのjQueryの使用方法

copy: { 
    expand: true, 
    src: 'source/*.html', 
    dest: 'build/', 
    ext: '.html', 
    filter: 'isFile', 
    options: { 
     process: function (content, srcpath) { 
     // find all occurrences of <span class='foo'> in file and wrap in a <div class='bah'> 
     $('span.foo').wrap('<div class="bah"></div>'); 
     } 
    } 

私はこれに対処するためと思われるgrunt-jsdom-jqueryプラグインうなり声があるが、それは2年前に最後にコミットされた、と私はそれを動作させることはできません参照 - それはとにかく複雑すぎるようです。

私は、node jsdomいくつかの方法を使用する必要があると考えていますが(ドキュメントにはjQueryをスクリプトとしてロードする例があります)、どのようにしてGruntで、具体的にはコピーまたは連結?

感謝しています。

+1

['cheerio'](https://github.com/cheeriojs/cheerio)で十分ではないか、本当に完全な"ブラウザ "環境(jsdom)が必要ですか? – mscdex

+0

@mscdex cheerio tipをお返事ありがとうございます。私はそれのためのハラハラのラッパーがあることを参照してください - grunt-dom-massager :)それを行ってもらえます。 –

+0

@mscdex休日から戻って、ちょうどそのドンマッサージャーを試してみました。とても素早く簡単です。 cheerio tipをもう一度見てくれてありがとう。 –

答えて

0

カスタムブロックタイプの配列を追加することができますmethod to change blocks of contentがありgrunt-processhtml

をお試しください:

あなたのhtmlに以下のように変更されます
'use strict'; 

module.exports = function (processor) { 
    // This will allow to use this <!-- build:customBlock[:target] <value> --> syntax 
    processor.registerBlockType('customBlock', function (content, block, blockLine, blockContent) { 
    var title = '<h1>' + block.asset + '</h1>'; 
    var result = content.replace(blockLine, title); 

    return result; 
    }); 
}; 

<!-- build:customBlock myValue --> 
<p>This will be replaced with the result of the custom block above</p> 
<!-- /build --> 

結果

<h1>myValue</h1>