简体   繁体   English

使用Browserify和Gulp使用共享公共库创建单独的JavaScript包

[英]Create separate JavaScript bundles with a shared common library using Browserify and Gulp

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. 对于我的团队在工作中,我试图建立一个半自动的JavaScript脚本和依赖管理系统,杯和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). 我甚至不确定使用当前可用的工具集(以及我有限的JavaScript知识)是否可以实现我想要实现的目标。 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. 对于共享模块,例如Module-vModule-y ,我不希望脚本通过包含在它们各自的捆绑包中而被复制。

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. 我知道使用Browserify我可以手动忽略或排除模块,这在项目年轻时很好 - 但随着项目的增长管理需要包含哪些依赖项将变得非常繁琐。

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 . 它还引用了gulp-browserify,后来被列入黑名单

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. 在我的图表中,我可以看到我有三个Browserify 入口点 ,但是我缺乏Gulp / Node / Browserify经验意味着我正在努力探索如何实现我想要的目标。

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 ; 它从似乎Browserify的插件文档 ,这可能是能够通过实现因素束亚组 向我指出的 ; however again due to my lack of Node/Browserify/Gulp experience I am struggling to pull all the pieces together. 然而,由于我缺乏Node / Browserify / Gulp经验,我正努力将所有部分组合在一起。

Related Questions 相关问题

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 . 此输出和图表之间的主要区别在于, common.js文件是根据blog.jspage.js之间的常见依赖关系自动生成的。 This is described in the factor-bundle documentation . 这在factor-bundle文档中有所描述。

Notes: 笔记:

  • I found that Node would throw an error if the output files didn't already exist. 我发现如果输出文件尚不存在,Node会抛出错误。 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插件使用时访问因子束流事件 (甚至可能不可能),因此对输出文件(例如uglifying等)的任何进一步工作都可能需要在管道中的其他地方完成,可能使用另一个browserify插件,甚至在事实之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM