LoginSignup
32
32

More than 5 years have passed since last update.

プラグイン毎にgrunt.loadNpmTasks()を追加する必要が無くなるload-grunt-tasksを紹介するよ

Posted at

load-grunt-tasksについて

grunt.loadNpmTasks()を、プラグインを読み込む毎に追加するのは面倒です。そういう時にload-grunt-tasksが便利です。

load-grunt-taksは、globbing patternを使って、gruntの複数のタスクを読む込むプラグインです。
globbing patternというのは、ワイルドカードを使ったパターンマッチングのことです。

実際にload-grunt-tasksを使うと、

Gruntfile.js
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-yuidoc');
grunt.loadNpmTasks('grunt-bower-task');

Gruntfile.js
require('load-grunt-tasks')(grunt);

になります。

Options

用意されているオプションは以下の3つです。

pattern

型: String|Array 初期値: grunt-*

特に指定が無ければ、globbing patternとしてgrunt-*が使われます。

config

型: String|Object 初期値: 最も近いpackage.jsonです。

※ 現状、型がStringだと期待したファイルを読み込まないと思うので、pull requestを送ってみました。

scope

型: String|Array 初期値: ['dependencies', 'devDependencies', 'peerDependencies']

Install

npmでインストールします。

$ npm i --save-dev load-grunt-tasks

もしくは、package.jsonに記述して、$ npm i --save-devでインストールします。

package.json
"load-grunt-taks": "*"

Usage

ほぼ公式ままですが、
全てのgruntタスクを読み込むには、

require('load-grunt-tasks')(grunt);

とします。パターンの初期値がgrunt-*なので、

require('load-grunt-tasks')(grunt, {pattern: 'grunt-*'});

と同じ意味になります。

grunt-contribのタスクだけを読み込みたいなら、

require('load-grunt-tasks')(grunt, {pattern: 'grunt-contrib-*'});

で出来ます。

全てのgrunt-contribのタスクとcontribじゃないタスクを読み込むには

Gruntfile.js
require('load-grunt-tasks')(grunt, {pattern: ['grunt-contrib-*', 'grunt-shell']});

とします。

一つだけを除外してcontribのタスクを読み込むには、否定のglobbing patternを使います。

Gruntfile.js
require('load-grunt-tasks')(grunt, {pattern: ['grunt-contrib-*', '!grunt-contrib-coffee']});

package.jsonをパス指定することも出来ます。

Gruntfile.js
require('load-grunt-tasks')(grunt, {config: require('../package')});

devDependenciesのみから読み込むことも出来ます。

Gruntfile.js
require('load-grunt-tasks')(grunt, {scope: 'devDependencies'});

devDependenciesdependenciesから読み込むことも出来ます。

Gruntfile.js
require('load-grunt-tasks')(grunt, {scope: ['devDependencies', 'dependencies']});

今後

このissueでのSindre Sorhus氏の回答から考えると、このプラグインは、恐らく将来的には不要になると思われます。
このプラグイン自体がGruntのコアに取り込まれることは無いようですが、より良い解決策をGruntに取り込む考えのようです。

Links

32
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
32