简体   繁体   English

Grunt + Concat + Angularjs

[英]Grunt + Concat + Angularjs

Setup: 设定:

  • A Gruntfile with the following task: 具有以下任务的Gruntfile

    concat: { build: { files: { 'build/app.js': [ 'src/ .js', 'src/ / .js', '!src/vendors/ ' ], } } concat:{构建:{文件:{'build / app.js':['src / .js','src / / .js','!src / vendors / '],}}

  • A lot of angular modules, with its controllers, services, and so on, with a structure like this: 许多有角度的模块,及其控制器,服务等,其结构如下:

    a/ a.js // Module declaration like: angular.module('a',[]) a-controller.ks // Which sets a controller in its root module definition like: angular.module('a').controller()... a / a.js //模块声明,例如:angular.module('a',[])a-controller.ks //在其根模块定义中设置控制器,例如:angular.module('a')。controller ()...

Issue: 问题:

The task concatenates all the js files it finds in the build folder to a single app.js file, and it does this fine, but messes up with the order of files when concatenating . 该任务将在build文件夹中找到的所有js文件连接到一个app.js文件中,虽然可以很好地完成此工作, 但是在连接时会弄乱文件的顺序

For instance, it concatenates first the controller file instead of the main folder file containing the module declaration, triggering the following error: 例如,它首先串联控制器文件而不是包含模块声明的主文件夹文件,从而触发以下错误:

Module xxxx not available! 模块xxxx不可用!

I suppose the issue lies in the way concat builds up the files and that is done by the grunt core and specifically the minimatch library, and the possibility it treats dashes to be first than letters, but I don't know how configure to change that behavior, and even know if that is possible. 我想问题在于concat建立文件的方式,这是由grunt内核(特别是minimatch库)完成的,它可能将破折号比字母先处理,但是我不知道如何配置来更改行为,甚至知道是否有可能。

Question: 题:

So, the question is: How can I make Grunt/Grunt-concat to process dashed f first than the others in the same folder so the ordering is maintained? 因此,问题是:如何使Grunt / Grunt-concat能够比同一个文件夹中的其他文件先处理破折号f,从而保持顺序?

Thanks 谢谢

Update 1: 更新1:

After digging more, It seems that it has nothing to do with the ordering inside a folder, but Grunt/Core sending the root files to the end and putting them the leaf ones first. 在深入研究之后,似乎与文件夹内的排序无关,而是Grunt / Core将根文件发送到末尾,然后将它们放在第一个叶文件中。

Just specify the order you want to concat your files, placing them in order, what I mean is, first add your single files that should be concatenated at start, after your full folder that does not need to have an order, and finally your final files, something rougth like this: 只需指定要合并文件的顺序,然后按顺序排列它们,我的意思是,首先添加您应该在开始时串联的单个文件,在不需要顺序的完整文件夹之后,最后添加最后一个文件,类似这样的东西:

grunt.initConfig({
  concat: {
    js: {
      src: ['lib/before.js', 'lib/*', 'lib/after.js'],
      dest: 'bundle.js',
    }
  }
});

You will have to specify to the grunt-concat task the order you want your files built. 您将必须向grunt-concat任务指定要构建文件的顺序。 For my projects, I typically keep a folder structure where controllers go in a app/controllers folder, services in services , and etc, but names can vary. 对于我的项目,我通常会保留一个文件夹结构,其中控制器位于app/controllers文件夹中,服务位于services等中,但是名称可能会有所不同。 I also keep an app.js that declares my app module and specifies the config handler for it. 我还保留了一个app.js ,用于声明我的应用程序模块并为其指定配置处理程序。 I use a config like this for grunt-uglify but the same can be done for concat with little to no changes: 我对grunt-uglify使用了这样的配置,但是对concat可以做同样的事情,几乎没有变化:

uglify: {
    development: {
        files: {
            'public/scripts/app.js': [
                'public/app/app.js',
                'public/app/controllers/*.js',
                'public/app/directives/*.js',
                'public/app/services/*.js'
            ]
        }
    }
}

I just copy paste my answer, the detail you want on second picture, i hope help you. 我只复制粘贴我的答案,您想要的细节放在第二张照片上,希望对您有所帮助。

you may consider this solution 您可以考虑此解决方案

  1. Separate the module declaration to xxx.module.js 将模块声明分隔为xxx.module.js
  2. In grunt-contrib-concat modify the config like below : 在grunt-contrib-concat中修改如下配置:

    place this outside grunt.initConfig 将此放置在grunt.initConfig外部

var clientApp = './app/'; var clientApp ='./app/';


grunt-contrib-concat config grunt-contrib-concat配置

dist: {// grab module first, state the second
    src: [
        clientApp+'**/*-controller.js',
        clientApp+'**/*.module.js',
        clientApp+'**/*.state.js',
        clientApp+'**/*.js'
        ],
    dest: 'dist/<%= pkg.name %>.js'
}

i use state to so i have to define state too before trying to navigate to any state. 我使用状态来表示,所以在尝试导航到任何状态之前也必须定义状态。 This is preview my code, the module declaration is declared fist before anything, then my state. 这是预览我的代码,模块声明先声明为fist,然后是我的状态。 even minified doesnt create any problem. 即使缩小也不会造成任何问题。 我的代码

I hope this help you. 希望对您有所帮助。 i follow this johnpapa's style guide , your problem might solve there if my solution not work 我遵循了johnpapa的风格指南 ,如果我的解决方案不起作用,您的问题可能会在那里解决

订购

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM