Alternate grunt.js tasks for development/production environments

爷,独闯天下 提交于 2019-12-23 09:49:29

问题


I would really love to be able to have a development grunt file and using the same file an production version of the script.

I have tried the suggestion on SO but my script will just fail when trying to call a dev/prod argument. I believe that the answer is for an older version of grunt, or maybe the plugins I am using.

module.exports = function (grunt) {

    // load all grunt tasks
    require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        compass: {
            dev: {
                options: {
                    config: 'config.rb',
                    force: true,
                    livereload: true
                }
            }
        },
        uglify: {
            build: {
                src: ['docroot/js/*.js', 'docroot/components/**/*.js'],
                dest: 'docroot/dis/main.min.js'
            }
        },
        watch: {
            options: {
                dateFormat: function(time) {
                    grunt.log.writeln('The watch finished in ' + time + 'ms at' + (new Date()).toString());
                    grunt.log.writeln('Waiting for more changes...');
                },
                livereload: true
            },
            sass: {
                files: ['docroot/sass/*.scss'],
                tasks: ['compass:dev']
            },
            /* watch and see if our javascript files change, or new packages are installed */
            js: {
                files: '<%= uglify.build.src %>',
                tasks: ['uglify']
            },
            /* watch our files for change, reload */
            livereload: {
                files: ['*.html', 'docroot/css/*.css', 'docroot/img/*', 'docroot/js/{main.min.js, plugins.min.js}'],
                options: {
                    livereload: true
                }
            }
        }
    });


    grunt.registerTask('default', 'watch');
};

Really, so long as I can get two version running by calling them with, for example:

 grunt //local
 grunt prod //live

then I can play around with the scripts and what to load.


回答1:


You can also just register a task that calls an array of tasks

grunt.registerTask('prod', ['tasks1','task2']); 

before your default task, that would be

$ grunt prod


来源:https://stackoverflow.com/questions/18476252/alternate-grunt-js-tasks-for-development-production-environments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!