forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
47 lines (38 loc) · 1.05 KB
/
script.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
import * as acorn from 'acorn';
import injectDynamicImport from 'acorn-dynamic-import/src/inject';
import repeat from '../../utils/repeat';
import { Parser } from '../index';
import { Node } from '../../interfaces';
const scriptClosingTag = '</script>';
injectDynamicImport(acorn);
export default function readScript(parser: Parser, start: number, attributes: Node[]) {
const scriptStart = parser.index;
const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart);
if (scriptEnd === -1) parser.error({
code: `unclosed-script`,
message: `<script> must have a closing tag`
});
const source =
repeat(' ', scriptStart) + parser.template.slice(scriptStart, scriptEnd);
parser.index = scriptEnd + scriptClosingTag.length;
let ast;
try {
ast = acorn.parse(source, {
ecmaVersion: 9,
sourceType: 'module',
plugins: {
dynamicImport: true
}
});
} catch (err) {
parser.acornError(err);
}
if (!ast.body.length) return null;
ast.start = scriptStart;
return {
start,
end: parser.index,
attributes,
content: ast,
};
}