javascript - 如何使用最新的 Browserify (6.x.x) 创建 vendor bundle ?

标签 javascript browserify

嗯,我们使用 Browserify 2.x 一段时间了。我们正在进行一些重构,因此我们希望尝试更新到最新的 Browserify,以减少 future 版本的飞跃。

不幸的是,外部包的处理方式发生了变化。在旧版本中,我们可以简单地告诉 Browserify 将哪些模块排除在 bundle 之外,并从另一个模块中获取它们 - 基本上是什么 described here .

从版本 5.0.0 开始,Browserify 内部发生了一些重大变化。我们以这个命令为例。 debug 模块是 NPM 模块。

browserify -r debug -o vendor.js

在 Browserify@4 中运行此命令,输出文件将如下所示:

require=(function... {
    "debug":[function(require,module,exports){
        module.exports=require('2Bvkwp');
    },{}],
    "2Bvkwp":[function(require,module,exports){
        // actual code from debug module
    },{}]
});

现在有了 Browserify@5,它看起来像这样:

require=(function... {
    1:[function(require,module,exports){
        // actual code from debug module
    },{}]
});

为了完成方程式,我有一个包含 require('debug') 的简单文件,它与命令 browserify -x debug -e index.js -o main.js 捆绑在一起debug 模块的内部依赖设置为 undefined,这是可以的。

如果您查看 prelude.js文件中,有一个逻辑简单地使用先前定义的全局 require (存储在 previousRequire 变量中)来查找当前包中未定义的模块。但由于 vendor.js 没有公开任何类似 debug 模块的内容,因此它无法成功。

我在变更日志中找到的只是这一行:

hashing is gone so expose: true or explicit expose id is required for doing multi-export bundles

我无法找到这实际上意味着什么:(

最佳答案

您应该能够像这样创建 vendor 包:

browserify -r debug > vendor.js

然后像这样创建应用程序包:

browserify index.js -x debug > main.js

这工作得很好(我正在使用 <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3052425f4743554259564970061e011e00" rel="noreferrer noopener nofollow">[email protected]</a> )。

基本上,即使require('debug');在浏览器控制台中不起作用,browserify可以找到 debug vendor bundle 中的模块,只要 bundle 按正确的顺序加载即可,即:

<script src="vendor.js"></script>
<script src="main.js"></script>

它不必公开对外部代码的依赖关系,只需公开对其他 browserify 包的依赖关系。

关于javascript - 如何使用最新的 Browserify (6.x.x) 创建 vendor bundle ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26258423/

相关文章:

javascript - NodeJS 中的同步 less 编译

node.js - 让 browserify-shim 按预期工作

javascript - casperjs exit()/die() 不返回当前目录

javascript - 按对象键对对象数组进行排序

javascript - 元素后的伪 css 属性在 jquery 中不起作用

javascript - 通过 Browserify 在浏览器中使用 NPM 包

javascript - browserify 抛出错误 : Unexpected character '�' (1:0) while parsing file: image. png

javascript - 操作后关闭 FancyBox 吗?

javascript - 使用 jquery-slim 包找不到 jQuery 模块?

node.js - 浏览器化和 bower 。规范方法