Skip to content

Instantly share code, notes, and snippets.

@jstejada
Created October 16, 2014 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstejada/5aa6de6afd837d1c5f89 to your computer and use it in GitHub Desktop.
Save jstejada/5aa6de6afd837d1c5f89 to your computer and use it in GitHub Desktop.
Example Gruntfile with coffee, mocha, browserify
serverRootUri = "http://127.0.0.1:8000"
mochaPhantomJsTestRunner = serverRootUri + "/browser/test/index.html"
module.exports = (grunt) ->
grunt.initConfig
pkg: grunt.file.readJSON("package.json")
coffeelint:
gruntfile:
src: '<%= watch.gruntfile.files %>'
src:
src: '<%= watch.src.files %>'
test:
src: '<%= watch.test.files %>'
options:
no_trailing_whitespace:
level: 'error'
max_line_length:
level: 'warn'
coffee:
options:
bare: true
src:
expand: true
cwd: 'src/'
src: ['**/*.coffee']
dest: 'out/src/'
ext: '.js'
test:
expand: true
cwd: 'test/'
src: ['**/*.coffee']
dest: 'out/test/'
ext: '.js'
# run the mocha tests via Node.js
mochaTest:
test:
options:
reporter: "spec"
src: ["test/**/*.coffee"]
# remove all previous browserified builds
clean:
out: ["./out/**/*"]
dist: ["./browser/dist/**/*"]
tests: ["./browser/test/browserified_tests.js"]
# browserify everything
browserify:
# This browserify build be used by users of the module. It contains a
# UMD (universal module definition) and can be used via an AMD module
# loader like RequireJS or by simply placing a script tag in the page,
# which registers mymodule as a global var. You can see examples for both
# usages in browser/example/index.html (script tag) and
# browser/example/index-require.html (RequireJS).
standalone:
src: ["./out/src/<%= pkg.name %>.js"]
dest: "./browser/dist/<%= pkg.name %>.standalone.js"
options:
standalone: "<%= pkg.name %>"
# This browserify build can be required by other browserify modules that
# have been created with an --external parameter. See
# browser/test/index.html for an example.
require:
src: ["./out/src/<%= pkg.name %>.js"]
dest: "./browser/dist/<%= pkg.name %>.require.js"
options:
alias: ["./out/src/<%= pkg.name %>.js:"]
# These are the browserified tests. We need to browserify the tests to be
# able to run the mocha tests while writing the tests as clean, simple
# CommonJS mocha tests (that is, without cross-platform boilerplate
# code). This build will also include the testing libs chai, sinon and
# sinon-chai but must not include the module under test.
tests:
src: ["./out/test/**/*.js"]
dest: "./browser/test/browserified_tests.js"
options:
external: ["./out/src/<%= pkg.name %>.js"]
# Embed source map for tests
debug: true
# Uglify browser libs
uglify:
dist:
files:
"browser/dist/<%= pkg.name %>.standalone.min.js": ["<%= browserify.standalone.dest %>"]
"browser/dist/<%= pkg.name %>.require.min.js": ["<%= browserify.require.dest %>"]
connect:
# Used for mocha-phantomjs tests
server: {}
# you can use this manually by doing
# grunt connect:keepalive
# to start a server for the example pages (browser/example/*.html) or to
# run the tests manually in a browser
keepalive:
options:
keepalive: true
# run the mocha tests in the browser via PhantomJS
mocha_phantomjs:
all:
options:
urls: [mochaPhantomJsTestRunner]
watch:
options:
spawn: false
gruntfile:
files: 'Gruntfile.coffee'
tasks: ['coffeelint:gruntfile']
src:
files: ['src/**/*.coffee']
tasks: [
'newer:coffeelint:src'
'newer:coffee:src'
'mochaTest'
'browserify:standalone'
]
test:
files: ['test/**/*.coffee']
tasks: [
'newer:coffeelint:test'
'newer:coffee:test'
'mochaTest'
]
grunt.loadNpmTasks "grunt-contrib-clean"
grunt.loadNpmTasks "grunt-contrib-coffee"
grunt.loadNpmTasks "grunt-coffeelint"
grunt.loadNpmTasks "grunt-mocha-test"
grunt.loadNpmTasks "grunt-browserify"
grunt.loadNpmTasks "grunt-contrib-uglify"
grunt.loadNpmTasks "grunt-contrib-connect"
grunt.loadNpmTasks "grunt-mocha-phantomjs"
grunt.loadNpmTasks "grunt-contrib-watch"
grunt.loadNpmTasks "grunt-newer"
grunt.registerTask "compile", [
"coffeelint"
"coffee"
]
grunt.registerTask 'test', [
'mochaTest'
]
grunt.registerTask 'cleanBrowser', [
"clean:dist"
"clean:tests"
]
grunt.registerTask 'testBrowser', [
"connect:server"
"mocha_phantomjs"
]
grunt.registerTask "default", [
"compile"
"test"
"cleanBrowser"
"browserify"
"uglify"
"testBrowser"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment