Skip to content

Commit

Permalink
Merge pull request #883 from sveltejs/default-onwarn-onerror
Browse files Browse the repository at this point in the history
pass default onwarn and onerror handlers to user's callbacks
  • Loading branch information
Rich-Harris authored Oct 17, 2017
2 parents 3cac20c + 0567d08 commit ba3641f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ The Svelte compiler optionally takes a second argument, an object of configurati
| `css` | `true`, `false` | Whether to include code to inject your component's styles into the DOM. | `true` |
| `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. | `{}` |
| `legacy` | `boolean` | Ensures compatibility with very old browsers, at the cost of some extra code |
| `onerror` | `function` | Specify a callback for when Svelte encounters an error while compiling the component. | (exception is thrown) |
| `onwarn` | `function` | Specify a callback for when Svelte encounters a non-fatal warning while compiling the component. | (warning is logged to console) |
| `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) |
| `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) |

## Example/starter repos

Expand Down
12 changes: 2 additions & 10 deletions src/generators/shared/utils/wrapModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,13 @@ function getGlobals(dependencies: Dependency[], options: CompileOptions) {
const error = new Error(
`Could not determine name for imported module '${d.source}' – use options.globals`
);
if (onerror) {
onerror(error);
} else {
throw error;
}
onerror(error);
} else {
const warning = {
message: `No name was supplied for imported module '${d.source}'. Guessing '${d.name}', but you should use options.globals`,
};

if (onwarn) {
onwarn(warning);
} else {
console.warn(warning); // eslint-disable-line no-console
}
onwarn(warning);
}

name = d.name;
Expand Down
39 changes: 21 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ import Stylesheet from './css/Stylesheet';
import { Parsed, CompileOptions, Warning } from './interfaces';

function normalizeOptions(options: CompileOptions): CompileOptions {
return assign(
{
generate: 'dom',
let normalizedOptions = assign({ generate: 'dom' }, options);
const { onwarn, onerror } = normalizedOptions;
normalizedOptions.onwarn = onwarn
? (warning: Warning) => onwarn(warning, defaultOnwarn)
: defaultOnwarn;
normalizedOptions.onerror = onerror
? (error: Error) => onerror(error, defaultOnerror)
: defaultOnerror;
return normalizedOptions;
}

onwarn: (warning: Warning) => {
if (warning.loc) {
console.warn(
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
); // eslint-disable-line no-console
} else {
console.warn(warning.message); // eslint-disable-line no-console
}
},
function defaultOnwarn(warning: Warning) {
if (warning.loc) {
console.warn(
`(${warning.loc.line}:${warning.loc.column}) – ${warning.message}`
); // eslint-disable-line no-console
} else {
console.warn(warning.message); // eslint-disable-line no-console
}
}

onerror: (error: Error) => {
throw error;
},
},
options
);
function defaultOnerror(error: Error) {
throw error;
}

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

0 comments on commit ba3641f

Please sign in to comment.