-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathshim-root-level-var.js
64 lines (53 loc) · 1.91 KB
/
shim-root-level-var.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
/*jshint asi: true */
var browserify_version = require('./utils/browserify-version');
var browserify = require('browserify')
, test = require('tap').test
, vm = require('vm')
, path = require('path')
, proxyquire = require('proxyquire')
var entry = require.resolve('./fixtures/entry-requires-root-level-var.js');
var file = require.resolve('./fixtures/shims/root-level-var');
function runBundle(fullPaths, cb) {
function resolveShims (file_, msgs, cb) {
var res = file_ === file
? { exports: 'nineties' }
: null;
setTimeout(cb.bind(null, null, { shim: res }), 0)
}
var shim = proxyquire('../../', {
'./lib/resolve-shims': resolveShims
})
browserify(entry, { fullPaths: fullPaths })
.transform(shim)
.bundle(function (err, src) {
if (err) return cb(err);
var ctx = { window: {}, console: console };
ctx.self = ctx.window;
var require_ = vm.runInNewContext(src, ctx);
cb(null, require_)
})
}
test('when I shim a module that declares its export as a var on the root level instead of attaching it to the window', function (t) {
runBundle(false, function (err, require_) {
if (err) { t.fail(err); return t.end() }
t.equal(
require_(1).message
, "I declare vars on the script level and expect them to get attached to the window because I'm doin' it 90s style baby!"
, 'shims that sucker'
);
t.end()
});
})
if (browserify_version >= 5)
test('when I shim a module that declares its export as a var on the root level instead of attaching it to the window, using fullPaths', function (t) {
runBundle(true, function (err, require_) {
if (err) { t.fail(err); return t.end() }
t.equal(
require_(entry).message
, "I declare vars on the script level and expect them to get attached to the window because I'm doin' it 90s style baby!"
, 'shims that sucker'
);
t.end()
});
})