UNPKG

1.54 kBJavaScriptView Raw
1var browserify = require('../');
2var vm = require('vm');
3var fs = require('fs');
4var path = require('path');
5var test = require('tap').test;
6var prelude = fs.readFileSync(path.join(__dirname, 'multi_bundle', '_prelude.js'), 'utf8');
7
8test('unique require', function (t) {
9 t.plan(6);
10
11 var core = browserify({
12 externalRequireName: 'unique_require',
13 prelude: prelude
14 });
15 core.require(__dirname + '/multi_bundle/b.js', { expose: './b' });
16
17 var app = browserify(
18 [__dirname + '/multi_bundle/a.js'],
19 { prelude: prelude }
20 );
21 // inform this bundle that b exists in another bundle
22 app.external('./b');
23
24 core.bundle(function (err, src) {
25 var c = {
26 console: console,
27 t : t,
28 baton: {
29 times: 0
30 }
31 };
32
33 // loading core will cause no require to run
34 vm.runInNewContext(src, c);
35 t.equal(c.baton.times, 0);
36
37 // loading the app will require
38 app.bundle(function (err, src) {
39 vm.runInNewContext(src, c);
40
41 // b required for the first time
42 t.equal(c.baton.times, 1);
43
44 // running the file again
45 // because it is using the same b, no reloading
46 vm.runInNewContext(src, c);
47
48 // b should not have been required again
49 // because it was part of the core bundle
50 t.equal(c.baton.times, 1);
51
52 t.equal(c.unique_require('./b'), 'foo');
53 });
54 });
55});
56