-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathshim-depends.js
143 lines (108 loc) · 4.24 KB
/
shim-depends.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'use strict';
/*jshint asi: true */
var browserify_version = require('./utils/browserify-version');
var browserify = require('browserify')
, test = require('tap').test
, vm = require('vm')
, proxyquire = require('proxyquire')
var jquery = { exports: '$' };
var jqueryPath = require.resolve('./fixtures/shims/crippled-jquery');
var underscore = { exports: null };
var underscorePath = require.resolve('./fixtures/shims/lib-exporting-_');
var dependent = {
exports: 'dep'
, depends: { }
};
dependent.depends[jqueryPath] = '$';
var dependentPath = require.resolve('./fixtures/shims/lib-depending-on-jquery');
var multidependentPath = require.resolve('./fixtures/shims/lib-depending-on-jquery-and-_');
var multidependent = {
exports: 'dep'
, depends: { }
};
multidependent.depends[jqueryPath] = '$';
multidependent.depends[underscorePath] = '_';
function inspect(obj, depth) {
console.error(require('util').inspect(obj, false, depth || 5, true));
}
function runFirstBundle(entry, fullPaths, cb) {
function resolveShims (file_, msgs, cb) {
var res;
if (file_ === jqueryPath) res = jquery;
if (file_ === dependentPath) res = dependent;
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('\nwhen I shim "jquery" and shim a lib that depends on it', function (t) {
var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery');
runFirstBundle(entry, false, function (err, require_) {
if (err) { t.fail(err.message); return t.end(); }
var dep = require_(1);
t.equal(dep.jqVersion, '1.8.3', 'when dependent gets required, $ is attached to the window');
t.end()
})
})
if (browserify_version >= 5)
test('\nwhen I shim "jquery" and shim a lib that depends on it, using fullPaths', function (t) {
var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery');
runFirstBundle(entry, true, function (err, require_) {
if (err) { t.fail(err.message); return t.end(); }
var dep = require_(entry);
t.equal(dep.jqVersion, '1.8.3', 'when dependent gets required, $ is attached to the window');
t.end()
})
})
function runSecondBundle(entry, fullPaths, cb) {
function resolveShims (file_, msgs, cb) {
var res;
if (file_ === jqueryPath) res = jquery;
if (file_ === underscorePath) res = underscore;
if (file_ === multidependentPath) res = multidependent;
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('\nwhen I shim "jquery" and _ lib in debug mode and shim a lib that depends on both', function (t) {
var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery-and-_');
runSecondBundle(entry, false, function (err, require_) {
if (err) { t.fail(err.message); return t.end(); }
var dep = require_(1);
t.equal(dep.jqVersion, '1.8.3', 'when multidependent gets required, $ is attached to the window');
t.equal(dep._(), 'super underscore', 'and _ is attached to the window');
t.end()
})
})
if (browserify_version >= 5)
test('\nwhen I shim "jquery" and _ lib in debug mode and shim a lib that depends on both, using fullPaths', function (t) {
var entry = require.resolve('./fixtures/entry-requires-depend-on-jquery-and-_');
runSecondBundle(entry, true, function (err, require_) {
if (err) { t.fail(err.message); return t.end(); }
var dep = require_(entry);
t.equal(dep.jqVersion, '1.8.3', 'when multidependent gets required, $ is attached to the window');
t.equal(dep._(), 'super underscore', 'and _ is attached to the window');
t.end()
})
})