問題描述
對于我的工作團隊,我正在嘗試在 Gulp 和 Browserify 的幫助下建立一個半自動化 JavaScript 腳本和依賴管理系統.
我什至不確定使用當前可用的工具集(以及我有限的 JavaScript 知識)是否可以實現我想要實現的目標.我相信我想要實現的是一個非常常見的場景,但我無法找到我一直在尋找的信息.
考慮下圖:
線條表示依賴關系.對于共享模塊,例如 Module-v 和 Module-y,我不希望通過包含在它們各自的包中來復制腳本.p>
我知道使用 Browserify 我可以手動忽略或排除模塊,這在項目還很年輕時很好 - 但隨著項目的增長,管理需要包含哪些依賴項將變得非常麻煩.
我認為這里有一個 類似的問答我要問的本質相同,但對我來說,還不是很清楚.它還引用了 gulp-browserify,它已被列入黑名單.
在我的圖表中,我可以看到我有三個 Browserify 入口點,但我缺乏 Gulp/Node/Browserify 經驗意味著我正在努力思考如何嘗試實現我想要的.
我很高興能夠嘗試將其拼湊起來,正如我已經嘗試過的那樣 - 但是項目經理正在喘不過氣來,所以我不得不拼湊一個臨時的解決方案",直到我可以實施一些更自動化和更強大的東西.
提前致謝.
編輯
從 Browserify 的插件文檔看來,這可能可以通過使用 factor-bundle 其中 子棧 指出給我;然而,由于我缺乏 Node/Browserify/Gulp 經驗,我再次努力將所有部分整合在一起.
相關問題
- 如何以編程方式將 factor-bundle 與 browserify 一起使用?
想通了,分享經驗:
代碼示例:
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']}).捆({調試:真}).pipe(source('common.js')).pipe(gulp.dest('bundle/'));});
此輸出與圖表的主要區別在于,common.js
文件是根據 blog.js
和 page 之間的共同依賴關系自動生成的.js代碼>.這在 factor-bundle 文檔中有描述.
注意事項:
我發現如果輸出文件不存在,Node 會拋出錯誤.我不確定為什么我會假設 factor-bundle 只是將流寫入輸出文件,而不管它是否存在.作為一種解決方法,在清理"輸出目錄之后,我只是創建了占位符"文件以保持其正常運行.
我還沒弄清楚如何訪問 factor-bundle 流事件 當將其用作 browserify 插件時(甚至可能無法實現),因此對輸出文件的任何進一步工作(例如 uglifying 等)都可能需要在管道中的其他地方完成,可能需要使用另一個 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.
這篇關于使用 Browserify 和 Gulp 使用共享的公共庫創建單獨的 JavaScript 包的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!