单个文件下concat javascript个文件

Concat javascript files under a single file

文件结构。

/app 
  /components 
     /core 
        /extensions
           - array.js 
           - string.js
        /services
           - logger.js 
        /lib 
           - core.js 

Core.js

(function() {
     'use strict';
     angular.module('app.core',[]);  
}());

我需要的是在每个构建上覆盖 core.js,方法是将该文件夹中的所有其他 js 文件复制到它上面,同时保留模块减速的第一个原始内容。

这是 Core.js 想要的结果:

(function() {
     'use strict';
     angular.module('app.core',[]);  
}());
// content of array.js
(function() {
   'use strict';

    var core = angular.module('app.core');
    core.config( .....) 
 }());     

 // content of string.js
 ...
 // content of logger.js

我尝试了 2 个繁重的任务,我相信这些任务不是为了这个目的,但没有找到一种方法来配置它们以满足我的需要。

1) grunt-concat 有 2 个问题,第一个是它从文件开头附加内容,而不是像我想要的那样从文件结尾附加内容。并且它不会覆盖现有内容。

2) grunt-copy 确实覆盖了但它覆盖了整个文件。

我尝试的是使用 process 函数进行 grunt 复制。

  copy: {
      src: ['app/components/core/{,*/}*.js'],
      dest: 'app/components/core/lib/core.js',
      options:{
          process : function(content, srcpath){
              return content; // do some manipulation on content here. 
          }
      }         
  }

这也是有问题的,因为我必须在我的 Gruntfile 中保留包含 angular 模块定义的文本,并将其附加到进入我的过程函数的第一个内容,像这样看起来真的凌乱。

 process : function(content, srcpath){
              if(isFirst) {
                  isFirst = false;
                  // here i will append the angular module deceleration. 
              }
              return content;
          }

有没有一种优雅的方法可以实现我所描述的?

grunt-contrib-concat正是您所需要的,只需执行以下操作:

  1. 将您当前的 core.js 重命名为 _banner.js(或您喜欢的任何名称)- 最好不要覆盖您的横幅文件
  2. 设置 concat 聚合 _banner.js 然后你的其他文件,并将其保存为 core.js:

    concat: {
      core: {
        files: [{
          src: [
            'app/components/core/_banner.js',
            'app/components/core/extensions/*.js',
            'app/components/core/services/*.js'
          ],
          dest: 'app/components/core/lib/core.js'
        }]
      },
    },
    

应该给你想要的