resolver.js
19.5 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
var path = require('path');
var test = require('tape');
var resolve = require('../');
var async = require('../async');
test('`./async` entry point', function (t) {
t.equal(resolve, async, '`./async` entry point is the same as `main`');
t.end();
});
test('async foo', function (t) {
t.plan(12);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
});
resolve('./foo.js', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.name, 'resolve');
});
resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg && pkg.main, 'resolver');
});
resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
t.equal(pkg.main, 'resolver');
});
resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo.js'));
});
resolve('foo', { basedir: dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
// Test that filename is reported as the "from" value when passed.
resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
});
});
test('bar', function (t) {
t.plan(6);
var dir = path.join(__dirname, 'resolver');
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg, undefined);
});
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg, undefined);
});
resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
t.equal(pkg.main, 'bar');
});
});
test('baz', function (t) {
t.plan(4);
var dir = path.join(__dirname, 'resolver');
resolve('./baz', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/quux.js'));
t.equal(pkg.main, 'quux.js');
});
resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/quux.js'));
t.equal(pkg.main, 'quux.js');
});
});
test('biz', function (t) {
t.plan(24);
var dir = path.join(__dirname, 'resolver/biz/node_modules');
resolve('./grux', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg, undefined);
});
resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg.main, 'biz');
});
resolve('./garply', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg, undefined);
});
resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg.main, 'grux');
});
resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg, undefined);
});
resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'tiv/index.js'));
t.equal(pkg.main, './lib');
});
resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg, undefined);
});
resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'grux/index.js'));
t.equal(pkg.main, 'tiv');
});
resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'garply/lib/index.js'));
t.equal(pkg.main, './lib');
});
});
test('quux', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver/quux');
resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'foo/index.js'));
t.equal(pkg.main, 'quux');
});
});
test('normalize', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
resolve('../grux', { basedir: dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg, undefined);
});
});
test('cup', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'cup.coffee'));
});
resolve('./cup.coffee', { basedir: dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'cup.coffee'));
});
resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
// Test that filename is reported as the "from" value when passed.
resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
});
});
test('mug', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'resolver');
resolve('./mug', { basedir: dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'mug.js'));
});
resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(dir, '/mug.coffee'));
});
resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
t.equal(res, path.join(dir, '/mug.js'));
});
});
test('other path', function (t) {
t.plan(6);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'bar');
var otherDir = path.join(resolverDir, 'other_path');
resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'other_path/root.js'));
});
resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js'));
});
resolve('root', { basedir: dir }, function (err, res) {
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) {
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
});
});
test('path iterator', function (t) {
t.plan(2);
var resolverDir = path.join(__dirname, 'resolver');
var exactIterator = function (x, start, getPackageCandidates, opts) {
return [path.join(resolverDir, x)];
};
resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(resolverDir, 'baz/quux.js'));
t.equal(pkg && pkg.name, 'baz');
});
});
test('incorrect main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
var dir = path.join(resolverDir, 'incorrect_main');
resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'index.js'));
});
});
test('missing index', function (t) {
t.plan(2);
var resolverDir = path.join(__dirname, 'resolver');
resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) {
t.ok(err instanceof Error);
t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
});
});
test('missing main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) {
t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
});
});
test('null main', function (t) {
t.plan(1);
var resolverDir = path.join(__dirname, 'resolver');
resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) {
t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error has correct error code');
});
});
test('main: false', function (t) {
t.plan(2);
var basedir = path.join(__dirname, 'resolver');
var dir = path.join(basedir, 'false_main');
resolve('./false_main', { basedir: basedir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(
res,
path.join(dir, 'index.js'),
'`"main": false`: resolves to `index.js`'
);
t.deepEqual(pkg, {
name: 'false_main',
main: false
});
});
});
test('without basedir', function (t) {
t.plan(1);
var dir = path.join(__dirname, 'resolver/without_basedir');
var tester = require(path.join(dir, 'main.js')); // eslint-disable-line global-require
tester(t, function (err, res, pkg) {
if (err) {
t.fail(err);
} else {
t.equal(res, path.join(dir, 'node_modules/mymodule.js'));
}
});
});
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver');
resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo.js'));
});
resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
});
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
t.plan(2);
var dir = path.join(__dirname, 'resolver');
resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
});
});
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
var testFile = path.basename(__filename);
t.test('sanity check', function (st) {
st.plan(1);
resolve('./' + testFile, function (err, res, pkg) {
if (err) t.fail(err);
st.equal(res, __filename, 'sanity check');
});
});
t.test('with a fake directory', function (st) {
st.plan(4);
resolve('./' + testFile + '/blah', function (err, res, pkg) {
st.ok(err, 'there is an error');
st.notOk(res, 'no result');
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
err && err.message,
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);
st.end();
});
});
t.end();
});
test('async dot main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});
test('async dot slash main', function (t) {
var start = new Date();
t.plan(3);
resolve('./resolver/dot_slash_main', function (err, ret) {
t.notOk(err);
t.equal(ret, path.join(__dirname, 'resolver/dot_slash_main/index.js'));
t.ok(new Date() - start < 50, 'resolve.sync timedout');
t.end();
});
});
test('not a directory', function (t) {
t.plan(6);
var path = './foo';
resolve(path, { basedir: __filename }, function (err, res, pkg) {
t.ok(err, 'a non-directory errors');
t.equal(arguments.length, 1);
t.equal(res, undefined);
t.equal(pkg, undefined);
t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
t.equal(err && err.code, 'MODULE_NOT_FOUND');
});
});
test('non-string "main" field in package.json', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
t.ok(err, 'errors on non-string main');
t.equal(err.message, 'package “invalid_main” `main` must be a string');
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
t.equal(res, undefined, 'res is undefined');
t.equal(pkg, undefined, 'pkg is undefined');
});
});
test('non-string "main" field in package.json', function (t) {
t.plan(5);
var dir = path.join(__dirname, 'resolver');
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
t.ok(err, 'errors on non-string main');
t.equal(err.message, 'package “invalid_main” `main` must be a string');
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
t.equal(res, undefined, 'res is undefined');
t.equal(pkg, undefined, 'pkg is undefined');
});
});
test('browser field in package.json', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'resolver');
resolve(
'./browser_field',
{
basedir: dir,
packageFilter: function packageFilter(pkg) {
if (pkg.browser) {
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
delete pkg.browser; // eslint-disable-line no-param-reassign
}
return pkg;
}
},
function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'browser_field', 'b.js'));
t.equal(pkg && pkg.main, 'b');
t.equal(pkg && pkg.browser, undefined);
}
);
});
test('absolute paths', function (t) {
t.plan(4);
var extensionless = __filename.slice(0, -path.extname(__filename).length);
resolve(__filename, function (err, res) {
t.equal(
res,
__filename,
'absolute path to this file resolves'
);
});
resolve(extensionless, function (err, res) {
t.equal(
res,
__filename,
'extensionless absolute path to this file resolves'
);
});
resolve(__filename, { basedir: process.cwd() }, function (err, res) {
t.equal(
res,
__filename,
'absolute path to this file with a basedir resolves'
);
});
resolve(extensionless, { basedir: process.cwd() }, function (err, res) {
t.equal(
res,
__filename,
'extensionless absolute path to this file with a basedir resolves'
);
});
});
test('malformed package.json', function (t) {
/* eslint operator-linebreak: ["error", "before"], function-paren-newline: "off" */
t.plan(
(3 * 3) // 3 sets of 3 assertions in the final callback
+ 2 // 1 readPackage call with malformed package.json
);
var basedir = path.join(__dirname, 'resolver/malformed_package_json');
var expected = path.join(basedir, 'index.js');
resolve('./index.js', { basedir: basedir }, function (err, res, pkg) {
t.error(err, 'no error');
t.equal(res, expected, 'malformed package.json is silently ignored');
t.equal(pkg, undefined, 'malformed package.json gives an undefined `pkg` argument');
});
resolve(
'./index.js',
{
basedir: basedir,
packageFilter: function (pkg, pkgfile, dir) {
t.fail('should not reach here');
}
},
function (err, res, pkg) {
t.error(err, 'with packageFilter: no error');
t.equal(res, expected, 'with packageFilter: malformed package.json is silently ignored');
t.equal(pkg, undefined, 'with packageFilter: malformed package.json gives an undefined `pkg` argument');
}
);
resolve(
'./index.js',
{
basedir: basedir,
readPackage: function (readFile, pkgfile, cb) {
t.equal(pkgfile, path.join(basedir, 'package.json'), 'readPackageSync: `pkgfile` is package.json path');
readFile(pkgfile, function (err, result) {
try {
cb(null, JSON.parse(result));
} catch (e) {
t.ok(e instanceof SyntaxError, 'readPackage: malformed package.json parses as a syntax error');
cb(null);
}
});
}
},
function (err, res, pkg) {
t.error(err, 'with readPackage: no error');
t.equal(res, expected, 'with readPackage: malformed package.json is silently ignored');
t.equal(pkg, undefined, 'with readPackage: malformed package.json gives an undefined `pkg` argument');
}
);
});