import { Node } from 'estree-walker' ; import MagicString from 'magic-string' ; export class Scripts { private scriptTags = ( this . htmlxAst . children as Node [ ] ) . filter ( ( child ) => child . type === 'Script' ) ; private topLevelScripts = this . scriptTags ; constructor ( private htmlxAst ) { } handleScriptTag = ( node , parent ) => { if ( parent !== this . htmlxAst && node . name === 'script' ) { this . topLevelScripts = this . topLevelScripts . filter ( ( tag ) => tag . start !== node . start || tag . end !== node . end ) ; } } ; getTopLevelScriptTags ( ) { let scriptTag = null ; let moduleScriptTag = null ; this . topLevelScripts . forEach ( ( tag ) => { if ( tag . attributes && tag . attributes . find ( ( a ) => a . name == 'context' && a . value . length == 1 && a . value [ 0 ] . raw == 'module' ) ) { moduleScriptTag = tag ; } else { scriptTag = tag ; } } ) ; return { scriptTag , moduleScriptTag } ; } blankOtherScriptTags ( str ) { this . scriptTags . filter ( ( tag ) => ! this . topLevelScripts . includes ( tag ) ) . forEach ( ( tag ) => { str . remove ( tag . start , tag . end ) ; } ) ; } }