Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

javascript - 与Gulp一起使用Concat脚本(Concat scripts in order with Gulp)

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

(说,例如,你正在构建骨干或任何一个项目,你需要加载脚本按照一定的顺序,如underscore.js需要之前加载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.

(我在我的脚本的正确顺序source/index.html ,但由于文件是由字母顺序排列,一饮而尽将Concat的underscore.jsbackbone.js ,而在我的脚本的命令source/index.html不问题,它会查看目录中的文件。)

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.

(我有最好的办法是重新命名的供应商脚本123 ,给他们以正确的顺序,但我不知道如果我喜欢这个。)

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

(当我了解到更多信息后,我发现Browserify是一个很好的解决方案,乍一看可能很麻烦,但是却很棒。)

  ask by Michael Joseph Aubry translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a similar problem recently with Grunt when building my AngularJS app.

(最近,在构建AngularJS应用时,Grunt也遇到了类似的问题。)

Here's a question I posted.

(这是我发布的问题 。)

What I ended up doing is to explicitly list the files in order in the grunt config.

(我最终要做的是在grunt配置中按顺序显式列出文件。)

The config file will then look like this:

(配置文件将如下所示:)

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

Grunt is able to figure out which files are duplicates and not include them.

(Grunt能够找出哪些文件是重复的,而不包括它们。)

The same technique will work with Gulp as well.

(同样的技术也适用于Gulp。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...