将javascript文件连接在一个文件下

Concat javascript files under a single file

本文关键字:文件 一个 javascript 连接      更新时间:2023-09-26

文件结构。

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

Core.js

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

我需要的是通过复制该文件夹中的所有其他js文件来覆盖每个构建上的core.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 grunt任务,我相信男人不为这个目的,但没有找到一种方法来配置他们的需要。

1) grunt-concat有两个问题,第一个是它从文件的开始追加内容,而不是像我想要的那样在文件的末尾追加内容。而且它不会覆盖现有的内容。

2) grunt-copy不覆盖,但它覆盖整个文件。

我所尝试的是使用进程函数进行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'
        }]
      },
    },
    

应该给你你想要的