【问题标题】:Grunt uglify in place recursivelyGrunt uglify 递归到位
【发布时间】:2017-07-31 14:30:53
【问题描述】:

我的应用中有几个 JS、CSS 和 HTML 文件。这是一个巨大的应用程序,我正在尝试递归地缩小和丑化这些文件就地。

我有一个构建服务器,我将原始集成获取到其中。在将文件扔到服务器上之前,我想在相同的目录结构下进行缩小。

即: /app/sales/checkout.js(普通文件) minifed 版本应该在同一个地方: /app/sales/checkout.js

我的 grunt 文件如下:

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

    // define source files and their destinations
    uglify: {
        files: {
            src: 'com.foo.web.online.portal/**/*.js',  // source files mask
            expand: true,    // allow dynamic building
            flatten: true,   // remove all unnecessary nesting
            dest: ''
        }
    },
    watch: {
        js:  { files: 'js/*.js', tasks: [ 'uglify' ] },
    }
});

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

// register at least this one task
grunt.registerTask('default', [ 'uglify' ]);


};

将目的地留空没有用。

我有很多文件夹和子文件夹: /checkout//几个js文件 /sales//几个js文件 /search/**/几个js文件

我怎样才能就地和递归地缩小?

谢谢。

【问题讨论】:

    标签: node.js gruntjs uglifyjs


    【解决方案1】:

    利用 grunt rename 函数动态构建文件对象。这可以使文件在相同的目录结构下被缩小(即用新缩小的数据覆盖原始文件数据)。

    Gruntfile.js

    uglify任务可以配置如下:

    // ...
    uglify: {
      dev: { // <-- include a target object
        files: [{
          expand: true,
          src: ['path/to/online/portal/**/*.js'],
    
          // The dest value should be whatever the src glob
          // pattern is, without the trailing /**/*.js part 
          dest: 'path/to/online/portal',
    
          cwd: '.',
          rename: function (dst, src) {
            return src;
          }
        }]
      }
    },
    //...
    

    有关详细信息,请参阅 grunt-contrib-uglify 文档的 this 部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2018-11-27
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      相关资源
      最近更新 更多