Skip to content

Commit 0567d08

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

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ The Svelte compiler optionally takes a second argument, an object of configurati
8383
| `css` | `true`, `false` | Whether to include code to inject your component's styles into the DOM. | `true` |
8484
| `globals` | `object`, `function` | When outputting to the `'umd'`, `'iife'` or `'eval'` formats, an object or function mapping the names of imported dependencies to the names of global variables. | `{}` |
8585
| `legacy` | `boolean` | Ensures compatibility with very old browsers, at the cost of some extra code |
86-
| `onerror` | `function` | Specify a callback for when Svelte encounters an error while compiling the component. | (exception is thrown) |
87-
| `onwarn` | `function` | Specify a callback for when Svelte encounters a non-fatal warning while compiling the component. | (warning is logged to console) |
86+
| `onerror` | `function` | Specify a callback for when Svelte encounters an error while compiling the component. Passed two arguments: the error object, and another function that is Svelte's default onerror handling. | (exception is thrown) |
87+
| `onwarn` | `function` | Specify a callback for when Svelte encounters a non-fatal warning while compiling the component. Passed two arguments: the warning object, and another function that is Svelte's default onwarn handling. | (warning is logged to console) |
8888

8989
## Example/starter repos
9090

src/generators/shared/utils/wrapModule.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,13 @@ function getGlobals(dependencies: Dependency[], options: CompileOptions) {
272272
const error = new Error(
273273
`Could not determine name for imported module '${d.source}' – use options.globals`
274274
);
275-
if (onerror) {
276-
onerror(error);
277-
} else {
278-
throw error;
279-
}
275+
onerror(error);
280276
} else {
281277
const warning = {
282278
message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`,
283279
};
284280

285-
if (onwarn) {
286-
onwarn(warning);
287-
} else {
288-
console.warn(warning); // eslint-disable-line no-console
289-
}
281+
onwarn(warning);
290282
}
291283

292284
name = d.name;

src/index.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,29 @@ 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 = assign({ generate: 'dom' }, options);
12+
const { onwarn, onerror } = normalizedOptions;
13+
normalizedOptions.onwarn = onwarn
14+
? (warning: Warning) => onwarn(warning, defaultOnwarn)
15+
: defaultOnwarn;
16+
normalizedOptions.onerror = onerror
17+
? (error: Error) => onerror(error, defaultOnerror)
18+
: defaultOnerror;
19+
return normalizedOptions;
20+
}
1421

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-
},
22+
function defaultOnwarn(warning: Warning) {
23+
if (warning.loc) {
24+
console.warn(
25+
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
26+
); // eslint-disable-line no-console
27+
} else {
28+
console.warn(warning.message); // eslint-disable-line no-console
29+
}
30+
}
2431

25-
onerror: (error: Error) => {
26-
throw error;
27-
},
28-
},
29-
options
30-
);
32+
function defaultOnerror(error: Error) {
33+
throw error;
3134
}
3235

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

0 commit comments

Comments
 (0)