Is it possible to put Grunt config files in a sub directory of the project? I want to do this to keep things more organised.
E.g.
myproject/grunt/Gruntfile.js
myproject/grunt/package.json
myproject/grunt/node_modules/
I'm having problems running commands from Gruntfile.js under this configuration. Can Grunt handle looking to the parent directory? Or maybe I'm doing it wrong
sass: {
dev: {
files: {
"../style.css": "../scss/style.scss"
}
}
}
When I run this Grunt just seems to shrug, not understanding that I want it to look in the parent directory…
Source file "scss/style.scss" not found.
Yes, this should be possible, but you will probably want to use the grunt.file.setBase method or the --base
command-line option to make the tasks work just like you had put the Gruntfile in the root of the project. Otherwise, you will run into various issues with tasks that, by default, will not write to paths outside the working directory. For example, the force option on the grunt-contrib-clean plugin.
Here is an example which modifies the sample Gruntfile from the Getting Started page to use this method:
module.exports = function(grunt) {
// if you put the call to setBase here, then the package.json and
// loadNpmTasks paths will be wrong!!!
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
},
build: {
src: 'src/<%= pkg.name %>.js',
dest: 'build/<%= pkg.name %>.min.js'
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify');
// now that we've loaded the package.json and the node_modules we set the base path
// for the actual execution of the tasks
grunt.file.setBase('../')
// Default task(s).
grunt.registerTask('default', ['uglify']);
};
I don't use SASS, so can't comment on whether anything might be wrong with your task config, but the above works as a modification of the Getting Started example.
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