admin管理员组

文章数量:1192157

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.

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.
Share Improve this question edited Aug 12, 2013 at 20:50 Steve P 19.4k7 gold badges72 silver badges96 bronze badges asked Aug 12, 2013 at 19:25 SparrwHawkSparrwHawk 14.2k22 gold badges62 silver badges92 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 28

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.

本文标签: javascriptPut Grunt in a subdirectoryStack Overflow