Skip to content

Instantly share code, notes, and snippets.

@foo9
Last active December 18, 2015 10:48
Show Gist options
  • Select an option

  • Save foo9/5770749 to your computer and use it in GitHub Desktop.

Select an option

Save foo9/5770749 to your computer and use it in GitHub Desktop.
GruntでCoffeeScriptコンパイル&圧縮

GruntでCoffeeScriptコンパイル&圧縮

npm, nodeは事前に導入済み

$ npm install -g grunt-cli
$ mkdir hogehoge
$ cd hogehoge
$ npm init
$ npm install grunt --save-dev
$ npm install grunt-contrib-coffee --save-dev
$ npm install grunt-contrib-uglify --save-dev
$ npm install grunt-contrib-watch --save-dev
$ mkdir coffee
$ mkdir js
$ mkdir compress

Gruntfile.coffee

module.exports = (grunt) ->
    pkg = grunt.file.readJSON 'package.json'
    grunt.initConfig
        watch:
            files: ['coffee/**/*.coffee'],
            tasks: ['coffee', 'uglify']

        coffee:
            compile:
                options:
                    sourceMap: true
                files: [
                    expand: true,
                    cwd: 'coffee/',
                    src: ['**/*.coffee'],
                    dest: 'js/',
                    ext: '.js'
                ]
        uglify:
            compress_target:
                options:
                    sourceMap: (fileName) ->
                        fileName.replace /\.js$/, '.js.map'
                files: [
                    expand: true,
                    cwd: 'js/',
                    src: ['**/*.js'],
                    dest: 'compress/',
                    ext: '.js'
                ]

    grunt.loadNpmTasks 'grunt-contrib-coffee'
    grunt.loadNpmTasks 'grunt-contrib-watch'
    grunt.loadNpmTasks 'grunt-contrib-uglify'

$ grunt watch

/coffee配下に.coffeeファイルを作成、編集すると自動的にコンパイル&圧縮する


参考

http://dev.classmethod.jp/tool/grunt-coffee-uglify/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment