Last active
March 3, 2022 10:13
-
-
Save thomasboyt/6406507 to your computer and use it in GitHub Desktop.
grunt post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
options: { | |
separator: ';' | |
}, | |
dist: { | |
src: ['src/**/*.js'], | |
dest: 'dist/<%= pkg.name %>.js' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
grunt.registerTask('helloWorld', 'Say hello!', function() { | |
grunt.log.writeln("Hello world!"); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadConfig(path) { | |
var glob = require('glob'); | |
var object = {}; | |
var key; | |
glob.sync('*', {cwd: path}).forEach(function(option) { | |
key = option.replace(/\.js$/,''); | |
object[key] = require(path + option); | |
}); | |
return object; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var config = { | |
pkg: grunt.file.readJSON('package.json'), | |
env: process.env | |
}; | |
grunt.util._.extend(config, loadConfig('./tasks/options/')); | |
grunt.initConfig(config); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('load-grunt-tasks')(grunt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.loadNpmTasks('grunt-contrib-uglify'); | |
grunt.loadNpmTasks('grunt-contrib-jshint'); | |
grunt.loadNpmTasks('grunt-contrib-qunit'); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
grunt.loadNpmTasks('grunt-contrib-concat'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.loadTasks('tasks'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(grunt) { | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON('package.json'), | |
concat: { | |
options: { | |
separator: ';' | |
}, | |
dist: { | |
src: ['src/**/*.js'], | |
dest: 'dist/<%= pkg.name %>.js' | |
} | |
}, | |
uglify: { | |
options: { | |
banner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n' | |
}, | |
dist: { | |
files: { | |
'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>'] | |
} | |
} | |
}, | |
qunit: { | |
files: ['test/**/*.html'] | |
}, | |
jshint: { | |
files: ['gruntfile.js', 'src/**/*.js', 'test/**/*.js'], | |
options: { | |
// options here to override JSHint defaults | |
globals: { | |
jQuery: true, | |
console: true, | |
module: true, | |
document: true | |
} | |
} | |
}, | |
watch: { | |
files: ['<%= jshint.files %>'], | |
tasks: ['jshint', 'qunit'] | |
} | |
}); | |
grunt.loadNpmTasks('grunt-contrib-uglify'); | |
grunt.loadNpmTasks('grunt-contrib-jshint'); | |
grunt.loadNpmTasks('grunt-contrib-qunit'); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
grunt.loadNpmTasks('grunt-contrib-concat'); | |
grunt.registerTask('test', ['jshint', 'qunit']); | |
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']); | |
}; |
I think this tutorial could be improved by including a repository that shows the directory layout.
I'm familiar with Grunt, but following the step-by-step instructions, I somehow missed wrapping my Gruntfile.js in a module.exports
. Maybe complete files would help.
Other than that, this was very helpful! Thank you for putting this together.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have one other question:
Some of my grunts are fairly detailed to set up. So I plan to have them on Github so I can pull them down without having to remember how to set them up.
Is it possible to adapt the script to have a separate options folder for each task, or to append the grunt name to the task filename?
Then I could just merge the existing
tasks
andoptions
folder when importing my preconfigured ones.Note that I might want to have several grunts using the same tasks (
copy
being used one way for one grunt and another way for a different grunt).Thanks.