Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass default onwarn and onerror handlers to user's callbacks #883

Merged
merged 1 commit into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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