問(wèn)題描述
對(duì)于我的工作團(tuán)隊(duì),我正在嘗試在 Gulp 和 Browserify 的幫助下建立一個(gè)半自動(dòng)化 JavaScript 腳本和依賴管理系統(tǒng).
我什至不確定使用當(dāng)前可用的工具集(以及我有限的 JavaScript 知識(shí))是否可以實(shí)現(xiàn)我想要實(shí)現(xiàn)的目標(biāo).我相信我想要實(shí)現(xiàn)的是一個(gè)非常常見(jiàn)的場(chǎng)景,但我無(wú)法找到我一直在尋找的信息.
考慮下圖:
線條表示依賴關(guān)系.對(duì)于共享模塊,例如 Module-v 和 Module-y,我不希望通過(guò)包含在它們各自的包中來(lái)復(fù)制腳本.p>
我知道使用 Browserify 我可以手動(dòng)忽略或排除模塊,這在項(xiàng)目還很年輕時(shí)很好 - 但隨著項(xiàng)目的增長(zhǎng),管理需要包含哪些依賴項(xiàng)將變得非常麻煩.
我認(rèn)為這里有一個(gè) 類似的問(wèn)答我要問(wèn)的本質(zhì)相同,但對(duì)我來(lái)說(shuō),還不是很清楚.它還引用了 gulp-browserify,它已被列入黑名單.
在我的圖表中,我可以看到我有三個(gè) Browserify 入口點(diǎn),但我缺乏 Gulp/Node/Browserify 經(jīng)驗(yàn)意味著我正在努力思考如何嘗試實(shí)現(xiàn)我想要的.
我很高興能夠嘗試將其拼湊起來(lái),正如我已經(jīng)嘗試過(guò)的那樣 - 但是項(xiàng)目經(jīng)理正在喘不過(guò)氣來(lái),所以我不得不拼湊一個(gè)臨時(shí)的解決方案",直到我可以實(shí)施一些更自動(dòng)化和更強(qiáng)大的東西.
提前致謝.
編輯
從 Browserify 的插件文檔看來(lái),這可能可以通過(guò)使用 factor-bundle 其中 子棧 指出給我;然而,由于我缺乏 Node/Browserify/Gulp 經(jīng)驗(yàn),我再次努力將所有部分整合在一起.
相關(guān)問(wèn)題
- 如何以編程方式將 factor-bundle 與 browserify 一起使用?
想通了,分享經(jīng)驗(yàn):
代碼示例:
var gulp = require('gulp'),source = require('vinyl-source-stream'),browserify = require('browserify'),因子 = 要求(因子捆綁");gulp.task('瀏覽器', function(){返回瀏覽器化({條目:['blog.js', 'page.js']}).plugin(因素,{//文件輸出順序必須與入口順序一致o: ['bundle/blog.js', 'bundle/page.js']}).捆({調(diào)試:真}).pipe(source('common.js')).pipe(gulp.dest('bundle/'));});
此輸出與圖表的主要區(qū)別在于,common.js
文件是根據(jù) blog.js
和 page 之間的共同依賴關(guān)系自動(dòng)生成的.js代碼>.這在 factor-bundle 文檔中有描述.
注意事項(xiàng):
我發(fā)現(xiàn)如果輸出文件不存在,Node 會(huì)拋出錯(cuò)誤.我不確定為什么我會(huì)假設(shè) factor-bundle 只是將流寫(xiě)入輸出文件,而不管它是否存在.作為一種解決方法,在清理"輸出目錄之后,我只是創(chuàng)建了占位符"文件以保持其正常運(yùn)行.
我還沒(méi)弄清楚如何訪問(wèn) factor-bundle 流事件 當(dāng)將其用作 browserify 插件時(shí)(甚至可能無(wú)法實(shí)現(xiàn)),因此對(duì)輸出文件的任何進(jìn)一步工作(例如 uglifying 等)都可能需要在管道中的其他地方完成,可能需要使用另一個(gè) browserify 插件,甚至事后.
For my team at work, I'm trying to set up a semi-automated JavaScript script and dependency management system with the help of Gulp and Browserify.
I'm not even sure if what I'm trying to achieve is possible with the currently available set of tools (and my limited JavaScript knowledge). I believe what I'm trying to achieve is a pretty common scenario, but I haven't been able to find the information I've been looking for.
Consider the following diagram:
The lines indicate depedencies. For shared modules, such as Module-v and Module-y, I don't want the scripts to be duplicated by being included in each of their respective bundles.
I know that using Browserify I can manually ignore or exclude modules, which is fine when the project is young - but as the project grows managing which dependencies need to be included where is going to become very cumbersome.
A similar Q&A here I think has the same essence of what I'm trying to ask, but to me, it isn't quite clear. It also references gulp-browserify which has since been blacklisted.
In my diagram, I can see that I have three Browserify entry points, but my lack of Gulp/Node/Browserify experience means I'm struggling to wrap my head around how I can try to achieve what I want to.
I'm happy to do the work to try and piece it together, as I already have been trying - however project managers are breathing down my neck so I'm having to hack together a temporary "solution" until I can implement something a little more automated and robust.
Thanks in advance.
Edit
It seems from Browserify's plugin documentation that this might be able to be achieved by using factor-bundle which substack pointed out to me; however again due to my lack of Node/Browserify/Gulp experience I am struggling to pull all the pieces together.
Related Questions
- How can I use factor-bundle with browserify programmatically?
Figured it out, sharing the learns:
Code example:
var gulp = require('gulp'),
source = require('vinyl-source-stream'),
browserify = require('browserify'),
factor = require('factor-bundle');
gulp.task('browserify', function(){
return browserify({
entries: ['blog.js', 'page.js']
})
.plugin(factor, {
// File output order must match entry order
o: ['bundle/blog.js', 'bundle/page.js']
})
.bundle({
debug: true
})
.pipe(source('common.js'))
.pipe(gulp.dest('bundle/'));
});
The key difference between this output and the diagram, is that the common.js
file is automatically generated based on common depenedencies between blog.js
and page.js
. This is described in the factor-bundle documentation.
Notes:
I found that Node would throw an error if the output files didn't already exist. I'm unsure why as I would have assumed that factor-bundle would simply write a stream to the outputting file regardless of whether it was there or not. As a workaround, after 'cleaning' the output directory, I simply created 'placeholder' files to keep it happy.
I haven't figured out how to access the factor-bundle stream event when using it as a browserify plugin (it may not even be possible), so any further work on the output files (such as uglifying etc) would likely need to be done somewhere else in the pipeline, possibly with another browserify plugin, or even after the fact.
這篇關(guān)于使用 Browserify 和 Gulp 使用共享的公共庫(kù)創(chuàng)建單獨(dú)的 JavaScript 包的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!