Skip to content

Commit 43bb971

Browse files
committed
Log errors with correct context
Allow webpack to print verbose error logs, including the error context (in case of parse errors).
1 parent 7c54d92 commit 43bb971

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ module.exports = function(source, map) {
99

1010
try {
1111
let { code, map } = compile(source, {
12-
// name: CamelCase component name
1312
filename: filename,
1413
format: query.format || 'es',
1514
name: query.name,
1615
onerror: (err) => {
17-
console.log(err.message);
18-
this.emitError(err.message);
16+
this.emitError(err);
1917
},
2018
onwarn: (warn) => {
21-
console.log(warn.message);
22-
this.emitWarn(warn.message);
19+
this.emitWarn(warn);
2320
}
2421
});
2522

2623
this.callback(null, code, map);
2724
} catch (err) {
28-
this.callback(err);
25+
// wrap error to provide correct
26+
// context when logging to console
27+
this.callback(new Error(err.toString()));
2928
}
3029
};

test/fixtures/bad.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<p>Count: {{count}}</p>
2-
<button on::click='set({ count: count + 1 })'>+1</button>
1+
<p>Count: {{{count}}</p>
2+
<button on:click='set({ count: count + 1 })'>+1</button>

test/loader.spec.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ describe('loader', function() {
1919

2020
return function() {
2121

22+
const fileContents = readFile(fileName);
23+
2224
const cacheableSpy = spy(function() { });
2325

2426
const callbackSpy = spy(callback);
2527

26-
const fileContents = readFile(fileName);
27-
2828
loader.call({
2929
cacheable: cacheableSpy,
3030
callback: callbackSpy,
31+
emitWarn: function(warning) {},
32+
emitError: function(e) {},
3133
filename: fileName,
32-
query: query,
34+
query,
3335
}, fileContents, null);
3436

3537
expect(callbackSpy).to.have.been.called;
@@ -49,9 +51,17 @@ describe('loader', function() {
4951

5052

5153
it('should compile bad',
52-
testLoader('test/fixtures/bad.html', function(err, code, map) {
54+
testLoader('test/fixtures/bad.html', function(err, code, map, context) {
55+
5356
expect(err).to.exist;
5457

58+
expect(err.message).to.eql(
59+
'Expected }}} (1:18)\n' +
60+
'1: <p>Count: {{{count}}</p>\n' +
61+
' ^\n' +
62+
'2: <button on:click=\'set({ count: count + 1 })\'>+1</button>'
63+
);
64+
5565
expect(code).not.to.exist;
5666
expect(map).not.to.exist;
5767
})

0 commit comments

Comments
 (0)