8
6

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.

シンプルなWebサーバ+LivereloadだけのGruntfile

Last updated at Posted at 2014-04-04
  • 本当にHTML配るだけのウェブサーバたてたい
  • Pythonとかのワンライナーだと微妙に心もとない
  • 将来的に複雑なビルドも拡張できたらいいな

というかんじのGruntfile.coffeeスニペット。Yeoman使うほどじゃないとき用。

generator-webappで生成したプロジェクトのGruntfile.jsから余分な部分を除去しただけ。

プリプロセッサの例としてgrunt-typescriptのタスクを入れる(TypeScriptで遊ぼうとしたとき作ったので)。

package.json

{
  "name": "greeter",
  "version": "0.0.0",
  "description": "",
  "main": "greeter.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "cu39",
  "license": "MIT",
  "devDependencies": {
    "grunt": "^0.4.4",
    "grunt-concurrent": "^0.5.0",
    "grunt-contrib-connect": "^0.7.1",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-typescript": "^0.3.0",
    "load-grunt-tasks": "^0.4.0",
    "time-grunt": "^0.3.1"
  }
}

Gruntfile.coffee

'use strict'

# # Globbing (元のコメントを翻訳)
# パフォーマンスを上げるため1階層下だけにマッチさせている:
# 'test/spec/{,*/}*.js'
# すべてのサブフォルダに再帰的にマッチさせたいならこちらを使う:
# 'test/spec/**/*.js'

module.exports = (grunt) ->

  # Show elapsed time after tasks run
  require('time-grunt') grunt

  # Load all Grunt tasks
  require('load-grunt-tasks') grunt

  grunt.initConfig

    # Project settings
    config:
      # Configurable paths
      app: '.'
      css: '<%= config.app %>/.'
      ts:  '<%= config.app %>/.'
      js:  '<%= config.app %>/.'

    # grunt-contrib-watch
    watch:
      typescript:
        files: ['<%= config.ts %>/{,*/}*.ts']
        tasks: ['typescript:server']
      livereload:
        options:
          livereload: '<%= connect.options.livereload %>'
        files: [
          '<%= config.app %>/{,*/}*.html'
          '<%= config.css %>/{,*/}*.css'
          '<%= config.js %>/{,*/}*.js'
        ]

    # grunt-typescript
    typescript:
      options: {}
      server:
        src: ['<%= config.ts %>/**/*.ts']
        dest: '<%= config.js %>/greeter.js'
        optionj:
          module: 'amd'
          target: 'es5'

    # grunt-contrib-connect
    connect:
      options:
        port: 9000
        livereload: 35729
        # change this to '0.0.0.0' to access the server from outside
        hostname: 'localhost'
      livereload:
        options:
          open: true
          base: ['<%= config.app %>']

    # grunt-concurrent
    concurrent:
      server: [
        'typescript:server'
      ]
  # end of grunt.initConfig

  # register tasks
  grunt.registerTask 'serve', ->
    grunt.task.run [
      'concurrent:server'
      'connect:livereload'
      'watch'
    ]

  grunt.registerTask 'default', ['serve']
8
6
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?