Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
var assert = require('assert');
2
var resolve = require('../');
3
4
var fixtures_dir = __dirname + '/fixtures/node_modules';
5
6
// no package.json, load index.js
7
test('index.js of module dir', function() {
8
var path = resolve.sync('module-a', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
9
assert.equal(path, require.resolve('./fixtures/node_modules/module-a/index'));
10
});
11
12
// package.json main field specifies other location
13
test('alternate main', function() {
14
var path = resolve.sync('module-b', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
15
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
16
});
17
18
// package.json has 'browser' field which is a string
19
test('string browser field as main', function() {
20
var path = resolve.sync('module-c', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
21
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser'));
22
});
23
24
// package.json has 'browser' field which is a string
25
test('string browser field as main - require subfile', function() {
26
var parent = {
27
filename: fixtures_dir + '/module-c/browser.js',
28
paths: [ fixtures_dir + '/module-c/node_modules' ],
29
package: { main: './browser.js' }
30
};
31
32
var path = resolve.sync('./bar', parent);
33
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/bar'));
34
});
35
36
// package.json has browser field as object
37
// one of the keys replaces the main file
38
// this would be done if the user needed to replace main and some other module
39
test('object browser field as main', function() {
40
var path = resolve.sync('module-d', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
41
assert.equal(path, require.resolve('./fixtures/node_modules/module-d/browser'));
42
});
43
44
// package.json has browser field as object
45
// one of the keys replaces the main file
46
// however the main has no prefix and browser uses ./ prefix for the same file
47
test('object browser field as main', function() {
48
var path = resolve.sync('module-k', { paths: [ fixtures_dir ], package: { main: 'fixtures' } });
49
assert.equal(path, require.resolve('./fixtures/node_modules/module-k/browser'));
50
});
51
52
// browser field in package.json maps ./foo.js -> ./browser.js
53
// when we resolve ./foo while in module-e, this mapping should take effect
54
// the result is that ./foo resolves to ./browser
55
test('object browser field replace file', function() {
56
var parent = {
57
filename: fixtures_dir + '/module-e/main.js',
58
package: { main: './main.js' }
59
};
60
61
var path = resolve.sync('./foo', parent);
62
assert.equal(path, require.resolve('./fixtures/node_modules/module-e/browser'));
63
});
64
65
// browser field in package.json maps "module" -> "alternate module"
66
test('test foobar -> module-b replacement', function() {
67
var parent = {
68
filename: fixtures_dir + '/module-h/index.js',
69
package: { main: './index.js' }
70
};
71
72
var path = resolve.sync('foobar', parent);
73
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
74
});
75
76
// same as above but replacing core
77
test('test core -> module-c replacement', function() {
78
var parent = {
79
filename: fixtures_dir + '/module-h/index.js',
80
package: { main: './index.js' }
81
};
82
83
var path = resolve.sync('querystring', parent);
84
assert.equal(path, require.resolve('./fixtures/node_modules/module-c/browser'));
85
});
86
87
// browser field in package.json maps "module" -> "alternate module"
88
test('test foobar -> module-b replacement with transform', function() {
89
var parent = {
90
filename: fixtures_dir + '/module-i/index.js',
91
package: { main: './index.js' }
92
};
93
94
var path = resolve.sync('foobar', parent);
95
assert.equal(path, require.resolve('./fixtures/node_modules/module-b/main'));
96
});
97
98
test('test foobar -> module-i replacement with transform in replacement', function() {
99
var parent = {
100
filename: fixtures_dir + '/module-j/index.js',
101
package: { main: './index.js' }
102
};
103
104
var path = resolve.sync('foobar', parent);
105
assert.equal(path, require.resolve('./fixtures/node_modules/module-i/index'));
106
});
107
108
// same as above, but without a paths field in parent
109
// should still checks paths on the filename of parent
110
test('object browser field replace file - no paths', function() {
111
var parent = {
112
filename: fixtures_dir + '/module-f/lib/main.js',
113
package: { main: './lib/main.js' }
114
};
115
116
var path = resolve.sync('./foo', parent);
117
assert.equal(path, require.resolve('./fixtures/node_modules/module-f/lib/browser'));
118
});
119
120
test('replace module in browser field object', function() {
121
var parent = {
122
filename: fixtures_dir + '/module-g/index.js',
123
package: { main: './index.js' }
124
};
125
126
var path = resolve.sync('foobar', parent);
127
assert.equal(path, require.resolve('./fixtures/node_modules/module-g/foobar-browser'));
128
});
129
130
test('override engine shim', function() {
131
var parent = {
132
filename: fixtures_dir + '/override-engine-shim/index.js',
133
package: { main: './index.js' },
134
modules: { url: "wonderland" }
135
};
136
var path = resolve.sync('url', parent);
137
assert.equal(path, require.resolve('./fixtures/node_modules/override-engine-shim/url-browser'));
138
});
139
140
test('alt-browser field', function() {
141
var parent = {
142
filename: fixtures_dir + '/alt-browser-field/index.js',
143
package: { main: './index.js' },
144
browser: 'chromeapp'
145
};
146
147
var path = resolve.sync('url', parent);
148
assert.equal(path, require.resolve('./fixtures/node_modules/alt-browser-field/url-chromeapp'));
149
});
150
151