Skip to content

Commit bf50b47

Browse files
committed
deprecate onerror - fixes sveltejs#1745
1 parent b5140cd commit bf50b47

File tree

6 files changed

+44
-27
lines changed

6 files changed

+44
-27
lines changed

src/compile/index.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ import renderDOM from './render-dom/index';
55
import renderSSR from './render-ssr/index';
66
import { CompileOptions, Warning, Ast } from '../interfaces';
77
import Component from './Component';
8+
import deprecate from '../utils/deprecate';
89

910
function normalize_options(options: CompileOptions): CompileOptions {
1011
let normalized = assign({ generate: 'dom', dev: false }, options);
11-
const { onwarn, onerror } = normalized;
12+
const { onwarn } = normalized;
1213

1314
normalized.onwarn = onwarn
1415
? (warning: Warning) => onwarn(warning, default_onwarn)
1516
: default_onwarn;
1617

17-
normalized.onerror = onerror
18-
? (error: Error) => onerror(error, default_onerror)
19-
: default_onerror;
20-
2118
return normalized;
2219
}
2320

@@ -29,10 +26,6 @@ function default_onwarn({ start, message }: Warning) {
2926
}
3027
}
3128

32-
function default_onerror(error: Error) {
33-
throw error;
34-
}
35-
3629
function validate_options(options: CompileOptions, stats: Stats) {
3730
const { name, filename } = options;
3831

@@ -52,7 +45,17 @@ function validate_options(options: CompileOptions, stats: Stats) {
5245
}
5346
}
5447

55-
export default function compile(source: string, options: CompileOptions) {
48+
export default function compile(source: string, options: CompileOptions = {}) {
49+
const onerror = options.onerror || (err => {
50+
throw err;
51+
});
52+
53+
if (options.onerror) {
54+
// TODO remove in v3
55+
deprecate(`Instead of using options.onerror, wrap svelte.compile in a try-catch block`);
56+
delete options.onerror;
57+
}
58+
5659
options = normalize_options(options);
5760

5861
const stats = new Stats({
@@ -88,7 +91,6 @@ export default function compile(source: string, options: CompileOptions) {
8891

8992
return renderDOM(component, options);
9093
} catch (err) {
91-
options.onerror(err);
92-
return;
94+
onerror(err);
9395
}
9496
}

src/compile/wrapModule.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,17 @@ function getCompatibilityStatements(dependencies: Dependency[]) {
276276
}
277277

278278
function getGlobals(dependencies: Dependency[], options: CompileOptions) {
279-
const { globals, onerror, onwarn } = options;
279+
const { globals, onwarn } = options;
280280
const globalFn = getGlobalFn(globals);
281281

282282
return dependencies.map(d => {
283283
let name = globalFn(d.source);
284284

285285
if (!name) {
286286
if (d.name.startsWith('__import')) {
287-
const error = new Error(
287+
throw new Error(
288288
`Could not determine name for imported module '${d.source}' – use options.globals`
289289
);
290-
onerror(error);
291290
} else {
292291
const warning = {
293292
code: `options-missing-globals`,

src/index.ts

+17-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import compile from './compile/index';
22
import { CompileOptions } from './interfaces';
3+
import deprecate from './utils/deprecate';
34

45
export function create(source: string, options: CompileOptions = {}) {
5-
options.format = 'eval';
6-
7-
const compiled = compile(source, options);
6+
const onerror = options.onerror || (err => {
7+
throw err;
8+
});
89

9-
if (!compiled || !compiled.js.code) {
10-
return;
10+
if (options.onerror) {
11+
// TODO remove in v3
12+
deprecate(`Instead of using options.onerror, wrap svelte.create in a try-catch block`);
13+
delete options.onerror;
1114
}
1215

16+
options.format = 'eval';
17+
1318
try {
14-
return (new Function(`return ${compiled.js.code}`))();
15-
} catch (err) {
16-
if (options.onerror) {
17-
options.onerror(err);
19+
const compiled = compile(source, options);
20+
21+
if (!compiled || !compiled.js.code) {
1822
return;
19-
} else {
20-
throw err;
2123
}
24+
25+
return (new Function(`return ${compiled.js.code}`))();
26+
} catch (err) {
27+
onerror(err);
2228
}
2329
}
2430

src/interfaces.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export interface CompileOptions {
6060

6161
preserveComments?: boolean | false;
6262

63-
onerror?: (error: Error) => void;
6463
onwarn?: (warning: Warning) => void;
6564

6665
// to remove in v3
66+
onerror?: (error: Error) => void;
6767
skipIntroByDefault?: boolean;
6868
nestedTransitions?: boolean;
6969
}

src/utils/deprecate.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const seen = new Set();
2+
3+
export default function deprecate(message: string, code = message) {
4+
if (seen.has(code)) return;
5+
seen.add(code);
6+
7+
console.warn(`[svelte] DEPRECATION: ${message}`);
8+
}

test/parser/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('parse', () => {
4848
});
4949
});
5050

51+
// TODO remove in v3
5152
it('handles errors with options.onerror', () => {
5253
let errored = false;
5354

@@ -61,6 +62,7 @@ describe('parse', () => {
6162
assert.ok(errored);
6263
});
6364

65+
// TODO remove in v3
6466
it('throws without options.onerror', () => {
6567
assert.throws(() => {
6668
svelte.compile(`<h1>unclosed`);

0 commit comments

Comments
 (0)