简体   繁体   English

使用Grunt的初学者需要一些有关Gruntfile.js的建议

[英]Beginner at using Grunt need some advice for my Gruntfile.js

I'm new to setting up my own grunt, and this is what I have come up with. 我是刚建立自己的咕unt声的人,这就是我想出的。 I was just wondering if someone could look it over and give me some hints/advice. 我只是想知道是否有人可以看一下并给我一些提示/建议。

module.exports = function (grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        coffee: {
            compile: {
                expand: true,
                flatten: true,
                cwd: 'src/coffee',
                src: ['*.coffee'],
                dest: 'src/js/',
                ext: '.js'
            }
        },
        concat: {
            css: {
                src: [
                    'src/css/*'
                ],
                dest: 'css/.css'
            },
            js: {
                src: [
                    'src/js/*'
                ],
                dest: 'js/package.js'
            }
        },
        cssmin: {
            css: {
                src: 'css/package.css',
                dest: 'css/package.min.css'
            }
        },
        uglify: {
            js: {
                files: {
                    'js/package.min.js': ['js/package.js']
                }
            }
        },
        watch: {
            aspx: {
                files: ['*.aspx', '*.master']
            },
            css: {
                files: ['src/css/*'],
                tasks: ['concat:css', 'cssmin']
            },
            coffee: {
                files: ['src/coffee/*'],
                tasks: ['coffee:compile']
            },
            js: {
                files: ['src/js/*'],
                tasks: ['concat:js', 'uglify']
            },
            livereload: {
                files: ['*.aspx', '*.master', 'css/*.css', 'js/*.js'],
                options: { nospawn: true, livereload: true }
            }
       }
    });
    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-coffee');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.registerTask('default', ['coffee:compile','concat:css', 'cssmin:css', 'concat:js', 'uglify:js', 'watch']);
};

It does work, and reloads and compiles perfectly. 它确实可以工作,并且可以完美地重新加载和编译。 I was just wondering if there may be a more effiecent way to handle this. 我只是想知道是否可能有更有效的方法来处理此问题。 Being my first gruntfile I know it is very far from perfect. 作为我的第一个gruntfile,我知道这还远非完美。

I would recommend load-grunt-tasks in order to cut down on the complexity of the main Gruntfile.js. 我建议使用load-grunt-tasks ,以减少主要Gruntfile.js的复杂性。 It's incredibly simple to use. 使用起来非常简单。 It allows you to split up the Gruntfile.js into a number of smaller JS files stored in a separate Grunt folder, for example: 它允许您将Gruntfile.js拆分为多个较小的JS文件,这些文件存储在单独的Grunt文件夹中,例如:

/root
  /Grunt
    cssmin.js
    coffee.js
    watch.js
    ...

And then your main Gruntfile.js to load in your tasks is simply, again for example: 然后,再次简单说明您要加载任务的主要Gruntfile.js,例如:

module.exports = function (grunt) {
  require('load-grunt-tasks')(grunt);
}

It's all held together with YAML file called aliases.yaml that sits in the Grunt folder that details the Grunt commands and their associated processes. 所有这些都与名为aliases.yaml YAML文件一起保存,该文件位于Grunt文件夹中,该文件夹详细说明了Grunt命令及其相关进程。 So with this in the YAML file: 因此,在YAML文件中:

lint:
  - clear
  - jshint
  - jscs

You could run grunt lint and it would run those tasks. 您可以运行grunt lint ,它将运行这些任务。

I've found it a) a complete lifesaver, and b) helped me understand Grunt at a whole different level. 我发现它是:a)完整的救生员,b)帮助我从不同的角度理解了Grunt。

I've made an example repo for you to help explain what I'm talking about . 我为您提供了一个示例存储库,以帮助您解释我在说什么 I hope it's of some help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM