My Gruntfile is becoming pretty big right now and I want to split it up into multiple files. I've Googled and experimented a lot but I can't get it to work.
I want something like this:
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
concat: getConcatConfiguration()
});
}
functions.js
function getConcatConfiguration() {
// Do some stuff to generate and return configuration
}
How can I load functions.js into my Gruntfile.js?
Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node.
grunt. util. _ is deprecated and we highly encourage you to npm install lodash and var _ = require('lodash') to use lodash .
json file, which is then parsed to a JavaScript object. Grunt has a simple template engine to output the values of properties in the configuration object. Here we tell the concat task to concatenate all files that exist within src/ and end in .
The Gruntfile. js or Gruntfile. coffee file is a valid JavaScript or CoffeeScript file that belongs in the root directory of your project, next to the package.
How you can do it:
you need to export your concat configuration, and require it in your Gruntfile (basic node.js stuff)!
i would recommend putting all every taskspecific configuration into one file named after the configuration (in this case i named it concat.js
).
Moreover i moved concat.js
into a folder named grunt
Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
concat: require('grunt/concat')(grunt);
});
};
grunt/concat.js
module.exports = function getConcatConfiguration(grunt) {
// Do some stuff to generate and return configuration
};
How you SHOULD do it:
there was already someone there who created a module named load-grunt-config. this does exactly what you want.
go ahead and put everything (as mentioned above) into separate files into a location of your choice (default folder ist grunt
).
then your standard gruntfile should probably look like this:
module.exports = function(grunt) {
require('load-grunt-config')(grunt);
// define some alias tasks here
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With