forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
75 lines (60 loc) · 1.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import parse from './parse/index';
import validate from './validate/index';
import generate from './generators/dom/index';
import generateSSR from './generators/server-side-rendering/index';
import { assign } from './shared/index.js';
import { version } from '../package.json';
import { Parsed, CompileOptions, Warning } from './interfaces';
function normalizeOptions(options: CompileOptions): CompileOptions {
return assign(
{
generate: 'dom',
// a filename is necessary for sourcemap generation
filename: 'SvelteComponent.html',
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
}
},
onerror: (error: Error) => {
throw error;
},
},
options
);
}
export function compile(source: string, _options: CompileOptions) {
const options = normalizeOptions(_options);
let parsed: Parsed;
try {
parsed = parse(source, options);
} catch (err) {
options.onerror(err);
return;
}
validate(parsed, source, options);
const compiler = options.generate === 'ssr' ? generateSSR : generate;
return compiler(parsed, source, options);
}
export function create(source: string, _options: CompileOptions = {}) {
_options.format = 'eval';
const compiled = compile(source, _options);
if (!compiled || !compiled.code) {
return;
}
try {
return new Function('return ' + compiled.code)();
} catch (err) {
if (_options.onerror) {
_options.onerror(err);
return;
} else {
throw err;
}
}
}
export { parse, validate, version as VERSION };