Skip to content

Validate error path for addDependency() (#197) #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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