{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { join } from \"node:path\"\nimport type { Options as PugOptions, LocalsObject } from \"pug\"\nimport type { Logger, Plugin } from \"vite\"\nimport { compileFile } from \"pug\"\nimport pc from \"picocolors\"\n\ninterface PluginOptions extends PugOptions {\n  /**\n   * Look for pug files in the directory\n   * of currently compiled index.html\n   * (locally)\n   * instead of project root.\n   *\n   * Can accept a function to determine the option per-html-file.\n   */\n  localImports?: boolean | ((htmlfile: string) => boolean)\n}\n\nexport function pugs(html: string, pugger: (filename: string) => string, logger?: Pick<Logger, \"warn\">) {\n  return html.replace(/<pug.+?(file|src)=\"(.+?)\".*?\\/.*?>/gi, (_tag: string, attr: string, filename: string) => {\n    if (attr === \"file\" && logger) {\n      logger.warn(\n        `${pc.red(`the ${pc.bold(`file`)} attribute is deprecated,`)} ${pc.cyan(\n          `please include ${pc.italic(filename)} with ${pc.bold(`src`)} instead`\n        )}`\n      )\n    }\n    return pugger(filename)\n  })\n}\n\nexport default function pugPlugin(options?: PluginOptions, locals?: LocalsObject): Plugin {\n  return {\n    name: \"vite-plugin-pug\",\n\n    handleHotUpdate({ file, server }) {\n      if (file.endsWith(\".pug\")) {\n        server.config.logger.info(`${pc.red(`pug’s not hot`)} 🌭 ${pc.cyan(file)}`)\n        server.ws.send({\n          type: \"full-reload\"\n        })\n      }\n    },\n\n    transformIndexHtml(html, { server, filename: htmlfile }) {\n      return pugs(\n        html,\n        filename => {\n          const compile = (filepath: string) => compileFile(filepath, options)(locals)\n          if (\n            (typeof options?.localImports === \"function\" && options.localImports(htmlfile)) ||\n            options?.localImports\n          ) {\n            // extract current directory from the html file path\n            const filedir = htmlfile.replace(/(.*)[\\\\\\/].*\\.html$/, \"$1\")\n\n            // apply current directory to the pug file imported from html\n            const filepath = join(filedir, filename)\n\n            return compile(filepath)\n          }\n\n          return compile(filename)\n        },\n        server?.config.logger\n      )\n    }\n  }\n}\n"],"mappings":"0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,E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ger","_tag","attr","filename","pc","options","locals","file","server","htmlfile","compile","filepath","filedir"]}