Why is it recommended to use concat then uglify when the latter can do both?

前端 未结 2 1018
长发绾君心
长发绾君心 2020-12-12 21:12

I keep seeing the recommendation for making JS files ready for production to be concat then uglify.

For example here, in on of Yeoman\'s grunt tasks.

2条回答
  •  無奈伤痛
    2020-12-12 21:29

    In the example you mention, which I'm quoting below, the files are first concatenated with concat and then uglified/minified by uglify:

    {
      concat: {
        '.tmp/concat/js/app.js': [
          'app/js/app.js',
          'app/js/controllers/thing-controller.js',
          'app/js/models/thing-model.js',
          'app/js/views/thing-view.js'
        ]
      },
      uglifyjs: {
        'dist/js/app.js': ['.tmp/concat/js/app.js']
      }
    }
    

    The same could be achieved with:

    {
      uglifyjs: {
        'dist/js/app.js': [
          'app/js/app.js',
          'app/js/controllers/thing-controller.js',
          'app/js/models/thing-model.js',
          'app/js/views/thing-view.js'
        ]
      }
    }
    

    Typically, the task clean would then run after tasks that write to a temporary folder (in this example concat) and delete whatever content is in that folder. Some people also like to run clean before tasks like compass, to delete things like randomly named image sprites (which are newly generated every time the task runs). This would keep wheels turning even for the most paranoid.

    This is all a matter of preference and workflow, as is with when to run jshint. Some people like to run it before the compilation, others prefer to run it on compiled files.

    Complex projects with an incredible amount of JavaScript files - or with a increasingly broad number of peers & contributors, might choose to concatenate files outside uglify just to keep things more readable and maintainable. I think this was the reasoning behind Yeoman's choice of transformation flow.

    uglify can be notoriously slow depending of the project's configuration, so there might be some small gain in concatenating it with concat first - but that would have to be confirmed.

    concat also supports separators, which uglify doesn't as far as README.md files are concerned.

    concat: {
      options: {
        separator: ';',
      }
    }
    

提交回复
热议问题