import { PreprocessorGroup } from "svelte/types/compiler/preprocess" ; import { EXT_SVELTE } from "../constants" ; import { parse } from "path" ; interface PreprocessIncludeOptions { default test ? : RegExp ; script : Array < { example content : string ; default test ? : RegExp ; default behavior ? : "prepend" | "append" ; } > ; markup : Array < { example content : string ; default test ? : RegExp ; default behavior ? : "prepend" | "append" ; } > ; } export function include ( options ? ) { const script = options ?. script ?? [ ] ; const markup = options ?. markup ?? [ ] ; const test = options ?. test ?? EXT_SVELTE ; return { script ( { filename , content } ) { if ( script . length > 0 && filename !== undefined && ! /node_modules/ . test ( filename ) && test . test ( filename ) ) { script . forEach ( ( entry ) => { const behavior = entry . behavior ?? "prepend" ; const testEntry = entry . test ?? test ; if ( testEntry . test ( filename ) ) { if ( behavior === "prepend" ) { content = entry . content + content ; } else if ( behavior === "append" ) { content += entry . content ; } } } ) ; return { code : content } ; } return { code : content } ; } , markup ( { filename , content } ) { const { name } = parse ( filename ) ; if ( markup . length > 0 && ! /node_modules/ . test ( filename ) && ! /.svelte-kit/ . test ( filename ) && ! /^(__layout)/ . test ( name ) && test . test ( filename ) ) { markup . forEach ( ( entry ) => { const behavior = entry . behavior ?? "prepend" ; const testEntry = entry . test ?? test ; if ( testEntry . test ( filename ) ) { if ( behavior === "prepend" ) { content = entry . content + content ; } else if ( behavior === "append" ) { content += entry . content ; } } } ) ; return { code : content } ; } return { code : content } ; } } ; }