Skip to content

Commit 49474fe

Browse files
committed
pass default onwarn and onerror handlers to user's callbacks
1 parent 3cac20c commit 49474fe

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/index.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,34 @@ import Stylesheet from './css/Stylesheet';
88
import { Parsed, CompileOptions, Warning } from './interfaces';
99

1010
function normalizeOptions(options: CompileOptions): CompileOptions {
11-
return assign(
12-
{
13-
generate: 'dom',
11+
let normalizedOptions = Object.assign({ generate: 'dom' }, options);
12+
const { onwarn, onerror } = normalizedOptions;
13+
if (onwarn) {
14+
normalizedOptions.onwarn = (warning: Warning) =>
15+
onwarn(warning, defaultOnwarn);
16+
} else {
17+
normalizedOptions.onwarn = defaultOnwarn;
18+
}
19+
if (onerror) {
20+
normalizedOptions.onerror = (error: Error) => onwarn(error, defaultOnerror);
21+
} else {
22+
normalizedOptions.onerror = defaultOnerror;
23+
}
24+
return normalizedOptions;
25+
}
1426

15-
onwarn: (warning: Warning) => {
16-
if (warning.loc) {
17-
console.warn(
18-
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
19-
); // eslint-disable-line no-console
20-
} else {
21-
console.warn(warning.message); // eslint-disable-line no-console
22-
}
23-
},
27+
function defaultOnwarn(warning: Warning) {
28+
if (warning.loc) {
29+
console.warn(
30+
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
31+
); // eslint-disable-line no-console
32+
} else {
33+
console.warn(warning.message); // eslint-disable-line no-console
34+
}
35+
}
2436

25-
onerror: (error: Error) => {
26-
throw error;
27-
},
28-
},
29-
options
30-
);
37+
function defaultOnerror(error: Error) {
38+
throw error;
3139
}
3240

3341
export function compile(source: string, _options: CompileOptions) {

0 commit comments

Comments
 (0)