将Grunt放在子目录中

Spa*_*awk 20 javascript gruntjs

是否可以将Grunt配置文件放在项目的子目录中?我想这样做是为了让事情更有条理.

例如

  • 的myproject /咕噜/ Gruntfile.js

  • 的myproject /咕噜/的package.json

  • 的myproject /咕噜/ node_modules /

我在使用此配置下从Gruntfile.js运行命令时遇到问题.Grunt可以处理查找父目录吗?或许我做错了

sass: {
    dev: {
       files: {
           "../style.css": "../scss/style.scss"
       }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我跑这个Grunt似乎只是耸耸肩,不明白我希望它在父目录中查看...

Source file "scss/style.scss" not found.
Run Code Online (Sandbox Code Playgroud)

exp*_*nit 28

是的,这应该是可能的,但您可能希望使用grunt.file.setBase方法--base命令行选项使任务正常工作,就像您将Gruntfile放在项目的根目录中一样.否则,您将遇到各种问题,默认情况下,这些任务不会写入工作目录之外的路径.例如,grunt-contrib-clean插件上的force选项.

下面是一个示例,它从"入门"页面修改示例Gruntfile以使用此方法:

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']);

};
Run Code Online (Sandbox Code Playgroud)

我不使用SASS,因此无法评论您的任务配置是否有任何问题,但上述内容可作为"入门"示例的修改.