【问题标题】:Change the configuration of the task uglify dynamically动态更改 uglify 任务的配置
【发布时间】:2013-08-10 07:05:45
【问题描述】:

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

};

【问题讨论】:

  • 我没试过这个,但是文件配置语句可能完全替换了你的文件配置。您是否尝试在修改 uglify 配置时提供所有属性(expand、cwd 等...)?
  • 这就是解决方案!非常感谢。

标签: gruntjs


【解决方案1】:

线

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

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

尝试在新的 src: 文件路径中包含其他原始参数

【讨论】:

    【解决方案2】:

    如何在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多