【问题标题】:Turn Grunt Concat off while developing开发时关闭 Grunt Concat
【发布时间】:2017-01-27 10:39:19
【问题描述】:

我目前遇到一个问题,即 Grunt 将所有内容连接在一起,因此如果出现 .js 错误,那么它只会在连接文件中的一行显示它,而不是我真正应该查看的地方有没有办法关闭 concat ,uglify 或我需要阻止这种情况发生的任何事情......在“开发人员模式”下标记它然后进入生产它可以自动返回到 concat 和 uglify 的方式真的很酷,但我我很满意在我处理它时将其关闭,然后恢复更改。

Grunt 文件:

/*global module:false*/
module.exports = function (grunt) {

    require('jit-grunt')(grunt, {
        useminPrepare: 'grunt-usemin'
    });
    require('time-grunt')(grunt);

    // Project configuration.
    grunt.initConfig({
        // Metadata.
        pkg: grunt.file.readJSON('package.json'),
        banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %> */\n',
        // Task configuration.
        meta: {
            app_files: ['src/main/webapp/**/*.js', '!src/main/webapp/bower_components/**/*', '!src/main/webapp/assets/lib/**/*'],
            lib_files: ['src/main/webapp/assets/lib/**/*'],
            dist_dir:  'target'
        },
        jshint: {
            options: {
                jshintrc: true
            },
            gruntfile: {
                src: 'Gruntfile.js'
            },
            app_files: {
                src: '<%= meta.app_files %>'
            }
        },
        jscs: {
            options: {
                config: '.jscsrc',
                fix: true
            },
            src: '<%= meta.app_files %>'
        },
        clean: {
            dist: '<%= meta.dist_dir %>',
            temp: '.tmp',
            coverage: 'coverage'
        },
        connect: {
            server: {
                options: {
                    port: 9000,
                    base: 'src/main/webapp',
                    open: true,
                    livereload: true
                }
            },
            dist: {
                options: {
                    base: '<%= meta.dist_dir %>',
                    open: true
                }
            }
        },
        watch: {
            livereload: {
                files: ['<%= meta.app_files %>', 'src/main/webapp/**/*.html', 'src/main/webapp/**/*.css'],
                options: {
                    livereload: true
                }
            },
            gruntfile: {
                files: '<%= jshint.gruntfile.src %>',
                tasks: ['jshint:gruntfile']
            },
            jshint: {
                files: '<%= meta.app_files %>',
                tasks: ['newer:jshint:app_files', 'newer:jscs']
            }
        },
        useminPrepare: {
            html: 'src/main/webapp/index.html',
            options: {
                dest: '<%= meta.dist_dir %>/wiz'
            }
        },
        usemin: {
            html: '<%= meta.dist_dir %>/wiz/index.html'
        },
        copy: {
            dist: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: 'src/main/webapp',
                    dest: '<%= meta.dist_dir %>/wiz',
                    filter: 'isFile',
                    src: [
                        '**/*',
                        '!**/*.js',
                        '!**/*.css',
                        '!bower_components/**/*'
                    ]
                }]
            },
            assets: {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: 'src/main/webapp/assets/lib',
                    dest: 'target/assets/lib',
                    src: [
                        'chart-1.0.1.js',
                        'angular-chart.js',
                        'angular-google-chart-0.1.0-beta.1.js',
                        'Chart.HorizontalBar.js'
                    ]
                }]
            }
        },
        htmlmin: {
            dist: {
                options: {
                    collapseWhitespace: true,
                    collapseBooleanAttributes: true,
                    removeCommentsFromCDATA: true,
                    removeOptionalTags: true
                },
                files: [{
                    expand: true,
                    cwd: '<%= meta.dist_dir %>/wiz',
                    src: ['**/*.html'],
                    dest: '<%= meta.dist_dir %>/wiz'
                }]
            }
        },
        karma: {
            all: {
                configFile: 'karma.conf.js',
                singleRun: true
            },
            chrome: {
                configFile: 'karma.conf.js',
                singleRun: true,
                browsers: ['Chrome']
            },
            firefox: {
                configFile: 'karma.conf.js',
                singleRun: true,
                browsers: ['Firefox']
            },
            ie: {
                configFile: 'karma.conf.js',
                singleRun: true,
                browsers: ['IE']
            },
            phantomjs: {
                configFile: 'karma.conf.js',
                singleRun: true,
                browsers: ['PhantomJS']
            }
        },
        wiredep: {
            app: {
                src: [ 'src/main/webapp/index.html' ]
            },
            test: {
                src: ['karma.conf.js'],
                devDependencies: true
            }
        },
        uglify: {
            options: {
                mangle: false
            }
        },
        cssmin: {
            options: {
                beautify: true
            }
        }
    });

    grunt.registerTask('serve', function (target) {
        if (target === 'dist') {
            return grunt.task.run('connect:dist:keepalive');
        }

        grunt.task.run(['wiredep', 'connect:server', 'watch:livereload']);
    });

    grunt.registerTask('test', function (target) {
        grunt.task.run(['clean:coverage', 'wiredep:test', 'karma:' + (target ? target : 'all')]);
    });

    grunt.registerTask('check-code', ['newer:jshint', 'newer:jscs']);

    grunt.registerTask('build', [
        'clean:dist',
        'wiredep:app',
        'useminPrepare',
        'concat',
        'copy:dist',
        'copy:assets',
        'cssmin',
        'uglify',
        'usemin',
        'clean:temp'
    ]);

    grunt.registerTask('default', [
//        'newer:jshint',
//        'newer:jscs',
//        'test:phantomjs',
        'build'
    ]);
};

【问题讨论】:

  • 您需要创建两种不同的配置,一种用于开发,另一种用于生产。在 dev 中,您可以删除 concat 和 uglify 选项。

标签: javascript gruntjs bower grunt-contrib-uglify grunt-contrib-concat


【解决方案1】:

创建不同的配置如下:

grunt.registerTask('dev', [
// List all tasks for dev
]);

grunt.registerTask('prod', [
// List all tasks for production
]);

然后像这样执行:grunt dev

【讨论】:

  • 我有点困惑,所以我不会调用我传统的 grunt.registerTask('build',而是调用 grunt.registerTask('dev', [ // List all tasks for dev ]); 怎么做我叫 grunt dev
  • 简单地说,在你的命令提示符下写: grunt dev 。它将执行 dev 任务而不是 build。(grunt 后面的单词是您要执行的任务的名称。)
猜你喜欢
  • 1970-01-01
  • 2015-08-04
  • 1970-01-01
  • 2016-09-13
  • 2023-03-28
  • 2018-11-27
  • 1970-01-01
  • 2016-01-01
  • 1970-01-01
相关资源
最近更新 更多