Skip to content

Commit

Permalink
[fix] Validate error path for addDependency() (#199)
Browse files Browse the repository at this point in the history
Fixes #197
  • Loading branch information
wabain committed Jun 2, 2022
1 parent 201272b commit 1c41c98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ module.exports = function(source, map) {

callback(null, js.code, js.map);
}, err => {
this.addDependency(err.file);
const file = err.file || err.filename;
if (typeof file === 'string' && file !== this.resourcePath) {
this.addDependency(file);
}
callback(err);
}).catch(err => {
// wrap error to provide correct
Expand Down
32 changes: 20 additions & 12 deletions test/loader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ function d([str]) {
describe('loader', () => {
function testLoader(fileName, callback, query, version = 2) {
return done => {
function cb() {
const addedDependencies = new Set();

function cb(...args) {
while (args.length < 4) {
args.push(undefined);
}
args.push(addedDependencies);
try {
callback(...[].slice.call(arguments));
callback(...args);
} catch (err) {
expect(callbackSpy).to.have.been.called;
return done(err);
Expand All @@ -32,10 +38,13 @@ describe('loader', () => {

const callbackSpy = spy(cb);

const dependencySpy = spy(function(p) { addedDependencies.add(p); });

loader.call(
{
cacheable: cacheableSpy,
async: () => callbackSpy,
addDependency: dependencySpy,
resourcePath: fileName,
version,
query
Expand All @@ -45,6 +54,10 @@ describe('loader', () => {
);

expect(cacheableSpy).to.have.been.called;

for (const call of dependencySpy.getCalls()) {
expect(call.firstArg).to.be.a('string');
}
};
}

Expand Down Expand Up @@ -246,23 +259,18 @@ describe('loader', () => {
});

it('should not preprocess successfully', done => {
const { warn } = console;
const warnings = [];

console.warn = msg => {
warnings.push(msg);
};

testLoader(
'test/fixtures/style-valid.html',
(err, code, map) => {
(err, code, map, context, addedDependencies) => {
expect(err).to.exist;
console.warn = warn;
expect(addedDependencies).to.include('/some/subresource.css');
},
{
preprocess: {
style: () => {
throw new Error('Error while preprocessing');
const e = new Error('Error while preprocessing');
e.filename = '/some/subresource.css';
throw e;
}
}
}
Expand Down

0 comments on commit 1c41c98

Please sign in to comment.