Concat脚本按照Gulp的顺序排列 [英] Concat scripts in order with Gulp

查看:60
本文介绍了Concat脚本按照Gulp的顺序排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举例来说,你正在Backbone上建立一个项目,或者你需要按照一定的顺序加载脚本,例如, underscore.js 需要在 backbone.js 之前加载。



我如何获得它以连接脚本以便它们按顺序排列?

  // JS concat,strip debugging and minify 
gulp.task('scripts',function(){
gulp.src(['./ source / js / *。js','./source/js/**/*.js'])
.pipe(concat('script.js' ))
.pipe(stripDebug())
.pipe(uglify())
.pipe(gulp.dest('./ build / js /'));
});

我在 source / index.html ,但由于文件按字母顺序排列,gulp会在 backbone.js underscore.js c>,并且我的 source / index.html 中的脚本顺序无关紧要,它会查看目录中的文件。



那么有没有人有这个想法?



我的最佳想法是用 1 2 3 给他们正确的顺序,但我不确定我是否



当我学到更多时,我发现Browserify是一个很好的解决方案,它起初可能很痛苦,但它很棒。

解决方案

在构建我的AngularJS应用程序时,最近与Grunt有类似的问题。这是我发布的一个问题。 p>

我最终做的是在grunt配置中按顺序显式列出文件。所以它看起来像:

  [
'/path/to/app.js',
' /path/to/mymodule/mymodule.js',
'/path/to/mymodule/mymodule/*.js'
]

事情奏效了。 Grunt显然非常聪明,不会将同一个文件包含两次。



这与Gulp一样。


Say, for example, you are building a project on Backbone or whatever and you need to load scripts in a certain order, e.g. underscore.js needs to be loaded before backbone.js.

How do I get it to concat the scripts so that they’re in order?

// JS concat, strip debugging and minify
gulp.task('scripts', function() {
    gulp.src(['./source/js/*.js', './source/js/**/*.js'])
    .pipe(concat('script.js'))
    .pipe(stripDebug())
    .pipe(uglify())
    .pipe(gulp.dest('./build/js/'));
});

I have the right order of scripts in my source/index.html, but since files are organized by alphabetic order, gulp will concat underscore.js after backbone.js, and the order of the scripts in my source/index.html does not matter, it looks at the files in the directory.

So does anyone have an idea on this?

Best idea I have is to rename the vendor scripts with 1, 2, 3 to give them the proper order, but I am not sure if I like this.

As I learned more I found Browserify is a great solution, it can be a pain at first but it’s great.

解决方案

I had a similar problem recently with Grunt when building my AngularJS app. Here's a question I posted.

What I ended up doing is to explicitly list the files in order in the grunt config. So it looked like:

[
  '/path/to/app.js',
  '/path/to/mymodule/mymodule.js',
  '/path/to/mymodule/mymodule/*.js'
]

And things worked. And Grunt is apparently smart enough to not include the same file twice.

This works the same with Gulp.

这篇关于Concat脚本按照Gulp的顺序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆