2 lines
2.8 KiB
JavaScript

"use strict";var S=Object.defineProperty,_=Object.defineProperties;var q=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var C=(e,t,n)=>t in e?S(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,g=(e,t)=>{for(var n in t||(t={}))w.call(t,n)&&C(e,n,t[n]);if(y)for(var n of y(t))F.call(t,n)&&C(e,n,t[n]);return e},v=(e,t)=>_(e,q(t));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const j=require("vite-code-inspector-plugin"),R=require("webpack-code-inspector-plugin"),i=require("code-inspector-core"),m=require("fs"),f=require("path"),D=require("chalk"),E=require("dotenv");var O=(e,t,n)=>new Promise((l,u)=>{var a=o=>{try{c(n.next(o))}catch(s){u(s)}},r=o=>{try{c(n.throw(o))}catch(s){u(s)}},c=o=>o.done?l(o.value):Promise.resolve(o.value).then(a,r);c((n=n.apply(e,t)).next())});const W="esbuild-code-inspector-plugin";function k(e){return{name:W,setup(t){if(e.close||!i.isDev(e.dev,!1))return;const n={port:0,entry:"",output:e.output},{escapeTags:l=[]}=e,u=new Map;t.onLoad({filter:e.match||/\.(jsx|tsx|js|ts|mjs|mts)?$/},a=>O(this,null,function*(){let r=a.path;r=i.getMappingFilePath(r,e.mappings);let c=yield m.promises.readFile(r,"utf8"),o=u.get(r);if(!o||o.originCode!==c){let s=c,p=e.exclude||[];Array.isArray(p)||(p=[p]);const I=i.matchCondition([...p,/\/node_modules\//],r),T=i.matchCondition(e.include||[],r);if(I&&!T)return s;s=yield i.getCodeWithWebComponent({options:e,file:r,code:s,record:n});let d="";if(i.isJsTypeFile(r)?d="jsx":r.endsWith(".svelte")&&(d="svelte"),d)s=i.transformCode({content:s,filePath:r,fileType:d,escapeTags:l,pathType:e.pathType});else if(r.endsWith(".vue")){d="vue";const{descriptor:h}=i.parseSFC(s,{sourceMap:!1}),x=i.transformCode({content:h.template.content,filePath:r,fileType:d,escapeTags:l,pathType:e.pathType});s=s.replace(h.template.content,x)}const b=f.extname(r).replace(".","");o={originCode:c,output:{contents:s,loader:b}},u.set(r,o)}return o.output}))}}}function P(e){if(!(e!=null&&e.bundler)){console.log(D.red("Please specify the bundler in the options of code-inspector-plugin."));return}let t=!1;if(e.needEnvInspector)if(t=!0,process.env.CODE_INSPECTOR==="true")t=!1;else{const a=f.resolve(process.cwd(),".env.local");if(m.existsSync(a)){const r=m.readFileSync(a,"utf-8"),c=E.parse(r||"");(c==null?void 0:c.CODE_INSPECTOR)==="true"&&(t=!1)}}let n="";typeof __dirname!="undefined"?n=__dirname:n=f.dirname(i.fileURLToPath(typeof document=="undefined"?require("url").pathToFileURL(__filename).href:document.currentScript&&document.currentScript.src||new URL("index.js",document.baseURI).href));const l=v(g({},e),{close:t,output:f.resolve(n,"./")});return e.bundler==="webpack"||e.bundler==="rspack"?new R(l):e.bundler==="esbuild"?k(l):j.ViteCodeInspectorPlugin(l)}const L=P;exports.CodeInspectorPlugin=P;exports.codeInspectorPlugin=L;