import { Plugin , ResolvedConfig , normalizePath } from "vite" ; import path from "path" ; import debug from "debug" ; import * as kl from "kolorist" ; import type { RollupFilter } from "./utils.js" ; import { parseId } from "./utils.js" ; export interface PreactDevtoolsPluginOptions { injectInProd ? : boolean ; shouldTransform : RollupFilter ; } export function preactDevtoolsPlugin ( { injectInProd = false , shouldTransform , } ) { const log = debug ( "vite:preact-devtools" ) ; let entry = "" ; let config ; let found = false ; const plugin = { name : "preact:devtools" , enforce : "pre" , configResolved ( resolvedConfig ) { config = resolvedConfig ; } , resolveId ( url , importer = "" ) { const { id } = parseId ( url ) ; if ( ! found && /\.html$/ . test ( importer ) && shouldTransform ( id ) ) { found = true ; entry = normalizePath ( path . join ( config . root , id ) ) ; return undefined ; } } , transform ( code , url ) { const { id } = parseId ( url ) ; if ( entry === id && ( ! config . isProduction || injectInProd ) ) { const source = injectInProd ? "preact/devtools" : "preact/debug" ; code = `import "${ source }";\n${ code }` ; log ( `[inject] ${ kl . cyan ( source ) } -> ${ kl . dim ( id ) }` ) ; return code ; } } } ; return plugin ; }