UNPKG

vite-plugin-debugger

Version:

A vite plugin provide the debugger tools for mobile devices.

61 lines (60 loc) 2.1 kB
import { n as transformCDN, t as readFileContent } from "./helpers-CkWXZWVT.mjs"; import { isPackageExists } from "local-pkg"; import { normalizePath } from "vite"; //#region src/plugins/vconsole.ts const transformVConsoleOptions = async (html, opts) => { const { debug, active } = opts; const { options, cdn = "jsdelivr", src } = opts.config || {}; const tags = []; let injectCodes = ``; injectCodes += "\n(function(debug,active,options,cdn){\n"; injectCodes += readFileContent("./precompiled/show-or-not.js"); injectCodes += readFileContent("./precompiled/vconsole/prepend-script.js"); injectCodes += `\n})(${debug},${JSON.stringify(active)},${JSON.stringify(options)},${JSON.stringify(src || await transformCDN("vConsole", cdn))});\n`; tags.push({ tag: "script", injectTo: "head", children: injectCodes }); if (debug !== void 0) return { html, tags }; }; const transformVConsoleImport = (code, opts) => { const { debug, active } = opts; const { options = {} } = opts.config || {}; let injectCodes = ``; injectCodes += "\n/* eslint-disable */\n"; injectCodes += "\n(function(debug,active,options){\n"; injectCodes += readFileContent("./precompiled/show-or-not.js"); injectCodes += readFileContent("./precompiled/vconsole/dynamic-import.js"); injectCodes += `\n})(${debug},${JSON.stringify(active)},${JSON.stringify(options)});\n`; injectCodes += "\n/* eslint-enable */\n"; return { code: `${injectCodes}${code}`, map: null }; }; function vDebugger(options) { const { local = isPackageExists("vconsole"), entry } = options; const entryPath = entry ? (Array.isArray(entry) ? entry : [entry]).map((path) => normalizePath(path)) : []; if (local) return { name: "vite:mobile-devtools:vconsole", transform(code, id) { if (entryPath.includes(id)) return transformVConsoleImport(code, options); return { code, map: null }; } }; return { name: "vite:mobile-devtools:vconsole", transformIndexHtml: async (html) => { return await transformVConsoleOptions(html, options); } }; } //#endregion export { vDebugger as default, vDebugger };