Skip to content

Commit b8343b9

Browse files
committed
Merge pull request #91 from substack/fix-ignore-missing-cache
Fix ignoreMissing when using cache
2 parents 86d9791 + c2553d8 commit b8343b9

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Deps.prototype.resolve = function (id, parent, cb) {
136136
var opts = self.options;
137137

138138
if (xhas(self.cache, parent.id, 'deps', id)
139-
&& self.cache[parent.id].deps) {
139+
&& self.cache[parent.id].deps[id]) {
140140
var file = self.cache[parent.id].deps[id];
141141
var pkg = self.pkgCache[file];
142142
if (pkg) return cb(null, file, pkg);

test/ignore_missing.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
var parser = require('../');
2+
var test = require('tap').test;
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var files = {
7+
main: path.join(__dirname, '/ignore_missing/main.js'),
8+
other: path.join(__dirname, '/ignore_missing/other.js')
9+
};
10+
11+
var sources = Object.keys(files).reduce(function (acc, file) {
12+
acc[file] = fs.readFileSync(files[file], 'utf8');
13+
return acc;
14+
}, {});
15+
16+
test('ignoreMissing', function (t) {
17+
t.plan(1);
18+
var p = parser({ignoreMissing: true});
19+
p.end({file: files.main, entry: true});
20+
21+
var rows = [];
22+
p.on('data', function (row) { rows.push(row) });
23+
p.on('end', function () {
24+
t.same(rows.sort(cmp), [
25+
{
26+
id: files.main,
27+
file: files.main,
28+
source: sources.main,
29+
entry: true,
30+
deps: { './other': files.other }
31+
},
32+
{
33+
id: files.other,
34+
file: files.other,
35+
source: sources.other,
36+
deps: { 'missingModule': undefined }
37+
}
38+
].sort(cmp));
39+
});
40+
});
41+
42+
test('ignoreMissing off', function (t) {
43+
t.plan(1);
44+
var p = parser();
45+
p.end({file: files.main, entry: true});
46+
47+
var rows = [];
48+
p.on('data', function (row) { rows.push(row) });
49+
p.on('error', function (err) {
50+
t.match(
51+
String(err),
52+
/Cannot find module 'missingModule'/
53+
);
54+
});
55+
p.on('end', function () {
56+
t.fail('should have errored');
57+
});
58+
});
59+
60+
function cmp (a, b) { return a.id < b.id ? -1 : 1 }

test/ignore_missing/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./other');

test/ignore_missing/other.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('missingModule');

test/ignore_missing_cache.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var parser = require('../');
2+
var test = require('tap').test;
3+
var fs = require('fs');
4+
var path = require('path');
5+
6+
var files = {
7+
main: path.join(__dirname, '/ignore_missing/main.js'),
8+
other: path.join(__dirname, '/ignore_missing/other.js')
9+
};
10+
11+
var sources = Object.keys(files).reduce(function (acc, file) {
12+
acc[file] = fs.readFileSync(files[file], 'utf8');
13+
return acc;
14+
}, {});
15+
16+
var cache = {};
17+
cache[files.main] = {
18+
source: sources.main,
19+
deps: { './other': files.other }
20+
};
21+
cache[files.other] = {
22+
source: sources.other,
23+
deps: { 'missingModule': undefined }
24+
};
25+
26+
test('ignoreMissing with cache', function (t) {
27+
t.plan(1);
28+
var p = parser({ cache: cache, ignoreMissing: true });
29+
p.end({file: files.main, entry: true});
30+
31+
var rows = [];
32+
p.on('data', function (row) { rows.push(row) });
33+
p.on('end', function () {
34+
t.same(rows.sort(cmp), [
35+
{
36+
id: files.main,
37+
file: files.main,
38+
source: sources.main,
39+
entry: true,
40+
deps: { './other': files.other }
41+
},
42+
{
43+
id: files.other,
44+
file: files.other,
45+
source: sources.other,
46+
deps: { 'missingModule': undefined }
47+
}
48+
].sort(cmp));
49+
});
50+
});
51+
52+
function cmp (a, b) { return a.id < b.id ? -1 : 1 }

0 commit comments

Comments
 (0)