grunt usemin处理位于js文件中的路径?

时间:2014-07-19 07:42:03

标签: angularjs gruntjs grunt-usemin

目前我有一些看起来像这样的html文件:

 <!-- build:js({.tmp,app}) scripts/moduleX-scripts.js -->
  <script src="scripts/moduleX/moduleXConfig.js"></script>
  <script src="scripts/moduleX/featureX/controller.js"></script>
  <script src="scripts/moduleX/featureX/service.js"></script>
  <script src="scripts/moduleX/featureY/controller.js"></script>
  <script src="scripts/moduleX/featureY/service.js"></script>
  <!-- endbuild -->

 <!-- build:js({.tmp,app}) scripts/moduleY-scripts.js -->
  <script src="scripts/moduleY/moduleYConfig.js"></script>
  <script src="scripts/moduleY/featureW/controller.js"></script>
  <script src="scripts/moduleY/featureW/service.js"></script>
  <script src="scripts/moduleY/featureZ/controller.js"></script>
  <script src="scripts/moduleY/featureZ/service.js"></script>
  <!-- endbuild -->

由于这是一个角度应用程序,我想使用ocLazyLoad来管理我的模块,所以我的配置看起来像这样:

angular.module('MyMainModule', [
  {
    name: 'moduleX',
    files: [
      'scripts/moduleX/moduleXConfig.js',
      'scripts/moduleX/featureX/controller.js',
      'scripts/moduleX/featureX/service.js',
      'scripts/moduleX/featureY/controller.js',
      'scripts/moduleX/featureY/service.js',
    ]
  },
  {
    name: 'moduleY',
    files: [
      'scripts/moduleY/moduleYConfig.js',
      'scripts/moduleY/featureW/controller.js',
      'scripts/moduleY/featureW/service.js',
      'scripts/moduleY/featureZ/controller.js',
      'scripts/moduleY/featureZ/service.js',
    ]
  }
  ])

是否有任何grunt插件可以处理这个.js文件并用缩小和修订的版本替换files数组?

1 个答案:

答案 0 :(得分:0)

您可以使用grunt-contrib-uglifyjs来缩小文件。

grunt.initConfig({
  uglify: {
    moduleX: {
      files: {
        'scripts/moduleX.min.js': ['scripts/moduleX/*.js']
      }
    },
    moduleY: {
      files: {
        'scripts/moduleY.min.js': ['scripts/moduleY/*.js']
      }
    }
  }
});

您需要做的唯一更改是包含一个在模块目录中声明模块的文件。例如,在scripts/moduleX目录中,您将拥有以下文件:

// scripts/modeulX.js
angular.module('moduleX', []);