8

我需要更改我的 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 }]);
    });

};
4

2 回答 2

8

线

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

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

而是尝试将其他原始参数与新的 src 一起包含:文件路径

于 2013-08-09T07:12:36.687 回答
0

如何在 yeoman 中的每个“grunt build”上不缩小已经缩小的

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

此外,现在 uglify 不会从临时文件夹中复制您的 vendor.js,因此将“vendorJS”部分添加到“复制”任务中:

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

然后,在“构建”任务中,将 uglify 的目标设置为 'onlyScripts' 并复制 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

于 2015-07-22T12:43:55.710 回答