简体   繁体   English

如何在Browserify中使用多个包

[英]How to use multiple bundles in browserify

I'm just trying to work out a simple example of generating more than one bundle with browserify, but I cannot get it to work. 我只是想尝试一个简单的示例,用Browserify生成多个捆绑软件,但我无法使其正常工作。 I started with this simple example from the browserify docs ( https://github.com/substack/node-browserify#multiple-bundles ): 我从浏览器文档( https://github.com/substack/node-browserify#multiple-bundles )中的一个简单示例开始:

beep.js: beep.js:

var robot = require('./robot');
alert(robot('beep'));

robot.js: robot.js:

module.exports = function (s) { return s.toUpperCase() + '!' };

Then to build the bundles: 然后构建捆绑包:

browserify -r ./robot.js > common.js
browserify -x ./robot.js beep.js -d > beep_bundle.js

My page: 我的页面:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>beep</title>

    <script src="common.js"></script>
    <script src="beep_bundle.js"></script>
</head>
<body>

</body>
</html>

What I find is that when it runs, I get an error: 我发现它在运行时出现错误:

Uncaught Error: Cannot find module '/robot.js' 

This seems to be because there is a discrepancy between the two output files beep_bundle.js and common.js . 这似乎是因为两个输出文件beep_bundle.jscommon.js之间存在差异。

beep_bundle.js (note that the string "/robot.js" appears in the mapping): beep_bundle.js(请注意,字符串“ /robot.js”出现在映射中):

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var robot = require('./robot.js');
alert(robot('beep'));
},{"./robot.js":"/robot.js"}]},{},[1])
//# ...

common.js (note that the string "./robot.js" appears in the mapping): common.js(请注意,字符串“ ./robot.js”出现在映射中):

require=(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({"./robot.js":[function(require,module,exports){
module.exports = function (s) { return s.toUpperCase() + '!' };
},{}]},{},[])
//# ...

If I manually edit beep_bundle.js to have "./robot.js", it works correctly. 如果我手动编辑beep_bundle.js以使其具有“ ./robot.js”,则它可以正常工作。 What should I do to make this work correctly? 我应该怎么做才能使其正常工作?

甚至不要在Browserify 5+上尝试此操作,它已严重损坏... https://github.com/substack/node-browserify/issues/933

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

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