动态更改任务uglify的配置

时间:2013-08-08 14:08:58

标签: gruntjs

我需要根据需要更改我的uglify任务的配置以仅缩小文件(如jshint任务所述:https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed

修改适用于jshint任务但不适用于uglify,我认为问题是属性路径......

任何帮助将不胜感激;)

这是我的Gruntfile.js:

module.exports = function (grunt) {
    grunt.initConfig({

        // define source files and their destinations
        jshint: {
            all: ['dev/**/*.js'],
        },
        uglify: {
            dynamic_mappings: {
              // Grunt will search for "**/*.js" under "dev/" when the "minify" task
              // runs and build the appropriate src-dest file mappings then, so you
              // don't need to update the Gruntfile when files are added or removed.
            files: [{
                  expand: true,     // Enable dynamic expansion.
                  cwd: 'dev/',      // Src matches are relative to this path.
                  src: ['**/*.js'], // Actual pattern(s) to match.
                  dest: 'build',   // Destination path prefix.
                  ext: '.min.js',   // Dest filepaths will have this extension.
                },
              ],
            }
        }
        watch: {
        options: { spawn: false },
            js:  { files: 'dev/**/*.js', tasks: [ 'uglify' ] },
        }
    });

    // load plugins
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    // default task
    grunt.registerTask('default', [ 'watch' ]);

    grunt.event.on('watch', function(action, filepath) {
        grunt.config(['jshint', 'all'], filepath);
        grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);
    });

};

2 个答案:

答案 0 :(得分:8)

该行

grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);

完全替换uglify.dynamic_mappings.files的原始配置

而是尝试包含其他原始参数以及新的src:filepath

答案 1 :(得分:0)

如何不缩小已经缩小的每个" grunt build"在自耕农

   uglify:        {
      onlyScripts: {
        files:   [{
          dest: '<%= yeoman.dist %>/scripts/scripts.js',
          src:  ['.tmp/concat/scripts/scripts.js']
        }]
      }
    }

此外,现在uglify不会将您的vendor.js从临时文件夹中复制出来,因此请添加&#34; vendorJS&#34;部分到&#34;复制&#34;任务:

copy:
      //...
      vendorJS: {
        expand: true,
        cwd:    '.tmp/concat/scripts/',
        dest:   '<%= yeoman.dist %>/scripts/',
        src:    'vendor.js'
      }

然后,在&#34; build&#34;任务,将uglify的目标设置为“只有脚本”。并复制vendor.js:

  grunt.registerTask('build', [
    'jshint',
    'clean:dist',
    //'wiredep',
    // ...
    'useminPrepare',
    //'concurrent:dist',
    'autoprefixer',
    'concat',
    'ngAnnotate',
    'copy:dist',
    'cssmin',
    'uglify:onlyScripts',
    'copy:vendorJS',
   // ...
  ]);

http://eugenioz.blogspot.in/2014/08/how-to-not-minify-already-minified-on.html