問題描述
例如,您正在 Backbone 或其他任何東西上構建一個項目,并且您需要按特定順序加載腳本,例如underscore.js
需要在 backbone.js
之前加載.
如何讓它連接腳本以使它們井井有條?
//JS concat,剝離調試和縮小gulp.task('腳本', function() {gulp.src(['./source/js/*.js', './source/js/**/*.js']).pipe(concat('script.js')).pipe(stripDebug()).pipe(丑化()).pipe(gulp.dest('./build/js/'));});
我的 source/index.html
中的腳本順序正確,但由于文件是按字母順序組織的,gulp 將在 之后連接
,和我的underscore.js
>backbone.jssource/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. 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. The same technique will work with Gulp as well.
這篇關于使用 Gulp 按順序連接腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!