-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathindex.js
71 lines (56 loc) · 1.53 KB
/
index.js
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
import parse from './parse/index.js';
import validate from './validate/index.js';
import generate from './generators/dom/index.js';
import generateSSR from './generators/server-side-rendering/index.js';
import { assign } from './shared/index.js';
import { version } from '../package.json';
function normalizeOptions ( options ) {
return assign( {
generate: 'dom',
// a filename is necessary for sourcemap generation
filename: 'SvelteComponent.html',
onwarn: 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 => {
throw error;
}
}, options );
}
export function compile ( source, _options ) {
const options = normalizeOptions( _options );
let 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, _options = {} ) {
_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 };