5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【忘備録】Gruntfile テンプレ

5
Posted at

記憶力クソすぎるので忘備録を残しておく。いつか自動生成したい。

npm_install

# grunt
npm install --save-dev grunt # grunt 実行用
npm install --save-dev grunt-contrib-clean # 不要なファイル/ディレクトリ削除用
npm install --save-dev grunt-contrib-copy # コピー用
npm install --save-dev load-grunt-tasks # いちいち loadNpmTask するのめんどい人用
npm install --save-dev time-grunt # grunt タスクの実行時間計測。任意。

# for coffee
npm install --save-dev coffee-script # コーヒー
npm install --save-dev grunt-contrib-coffee
npm install --save-dev coffeelint # コーディング規約チェック用
npm install --save-dev grunt-coffeelint
npm install --save-dev grunt-file-dependency # 拙著。ファイルの依存性管理用

# for test
npm install --save-dev karma # テスト実行環境
npm install --save-dev karma-jasmine # spec 系のテストフレームワーク
npm install --save-dev karma-coffee-preprocessor # コーヒーで書きたい
npm install --save-dev grunt-karma # grunt で実行したい

# for watch
npm install --save-dev grunt-contrib-watch # ファイル監視
npm install --save-dev grunt-newer # 各タスクの実行対象を変更があったファイルに絞る
Gruntfile.coffee
"use strict"

module.exports = (grunt) ->
  require("load-grunt-tasks") grunt
  require("time-grunt") grunt

  grunt.initConfig
    context:
      dir:
        src: ""
        test: ""
        tmp: ""
        compiled: ""
        dist: ""
        vendor: ""
    fileDeps: loadConfig "file-dependency.conf.coffee"
    clean:
      intermediate:
        files: [
          dot: true
          src: [
            "<%= context.dir.tmp %>"
          ]
        ]
      target:
        files: [
          dot: true
          src: [
            "<%= context.dir.dist %>"
          ]
        ]
    coffee:
      compile:
        options:
          sourceMap: true
          sourceRoot: ""
        files: [{
          expand: true
          cwd: "<%= context.dir.src %>"
          src: "**/*.coffee"
          dest: "<%= context.dir.compiled %>"
          ext: ".js"
        }]
    copy:
      scripts:
        files: [{
          expand: true
          cwd: "<%= context.dir.compiled %>"
          src: ["**/*.{js,map}"]
          dest: "<%= context.dir.dist %>"
        }]
    karma:
      unit:
        configFile: "karma.conf.coffee"
        singleRun: true
    coffeelint:
      options:
        configFile: "coffeelint.json"
      target:
        files:
          src: [
            "Gruntfile.coffee"
            "<%= context.dir.src %>/**/*.coffee"
            "<%= context.dir.test %>/**/*.coffee"
          ]
    makePriority:
      scripts:
        options:
          baseUrl: "<%= context.dir.dist %>"
        deps: "<%= fileDeps.main.deps.shim %>"
        done: (priority) ->
          # priority には js ファイルのリストが入っている。
    watch:
      options:
        spawn: false
      coffee:
        files: [
          "<%= context.dir.src %>/**/*.coffee"
        ]
        tasks: [
          "newer:coffee:compile"
          "newer:copy:scripts"
        ]
      coffeeTest:
        files: [
          "<%= context.dir.src %>/**/*.coffee"
          "<%= context.dir.test %>/**/*.coffee"
          "karma.conf.coffee"
        ]
        tasks: [
          "test"
        ]
  
# tasks for build
  grunt.registerTask "build", [
    "doBuild"
    "clean:intermediate"
  ]

  grunt.registerTask "watchBuild", [
    "doBuild"
    "watch"
  ]

  grunt.registerTask "doBuild", [
    "clean:intermediate"
    "clean:target"
    "coffee:compile"
    "copy:scripts"
  ]

# tasks for test
  grunt.registerTask "test", ["doTest"]

  grunt.registerTask "watchTest", [
    "doTest",
    "watch:coffeeTest"
  ]

  grunt.registerTask "doTest", [
    "cs"
    "karma:unit:start"
  ]

  grunt.registerTask "cs", [
    "coffeelint:target"
  ]

たまに忘備録を書いたことを忘れるから始末が悪い。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?