简体   繁体   中英

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. I started with this simple example from the browserify docs ( https://github.com/substack/node-browserify#multiple-bundles ):

beep.js:

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

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.js (note that the string "/robot.js" appears in the mapping):

(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):

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. What should I do to make this work correctly?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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