mirror of https://github.com/sveltejs/svelte
40 lines
770 B
40 lines
770 B
import { parse, tokenizer } from 'acorn';
|
|
import spaces from '../utils/spaces.js';
|
|
|
|
export default function readScript ( parser, start, attributes ) {
|
|
const scriptStart = parser.index;
|
|
let scriptEnd = null;
|
|
|
|
for ( const token of tokenizer( parser.remaining() ) ) {
|
|
parser.index = scriptStart + token.end;
|
|
parser.allowWhitespace();
|
|
|
|
if ( parser.eat( '</script>' ) ) {
|
|
scriptEnd = scriptStart + token.end;
|
|
break;
|
|
}
|
|
}
|
|
|
|
const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd );
|
|
|
|
let ast;
|
|
|
|
try {
|
|
ast = parse( source, {
|
|
ecmaVersion: 8,
|
|
sourceType: 'module'
|
|
});
|
|
} catch ( err ) {
|
|
parser.acornError( err );
|
|
}
|
|
|
|
ast.start = scriptStart;
|
|
|
|
return {
|
|
start,
|
|
end: parser.index,
|
|
attributes,
|
|
content: ast
|
|
};
|
|
}
|