UNPKG

vite-plugin-bundle-obfuscator

Version:
3 lines (2 loc) 6.32 kB
"use strict";var J=Object.create;var b=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var te=(t,e)=>{for(var o in e)b(t,o,{get:e[o],enumerable:!0})},E=(t,e,o,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of Y(e))!ee.call(t,n)&&n!==o&&b(t,n,{get:()=>e[n],enumerable:!(r=Q(e,n))||r.enumerable});return t};var h=(t,e,o)=>(o=t!=null?J(Z(t)):{},E(e||!t||!t.__esModule?b(o,"default",{value:t,enumerable:!0}):o,t)),oe=t=>E(b({},"__esModule",{value:!0}),t);var se={};te(se,{default:()=>X});module.exports=oe(se);var O=h(require("vite")),F=require("worker_threads"),N=h(require("path")),_=h(require("os")),A=require("zlib"),P=h(require("javascript-obfuscator"));function ne(t){return Object.prototype.toString.call(t)==="[object RegExp]"}function re(t){return Object.prototype.toString.call(t)==="[object String]"}function p(t){return Object.prototype.toString.call(t)==="[object Object]"}function R(t){return Array.isArray(t)}function k(t){let e=Object.prototype.toString.call(t);return e==="[object Function]"||e==="[object AsyncFunction]"}function x(t){return Object.prototype.toString.call(t)==="[object Boolean]"}function M(t,e){for(let o of e)if(ne(o)){if(o.test(t))return!0}else if(re(o)&&t.includes(o))return!0;return!1}var L={excludes:[],enable:!0,log:!0,apply:"build",autoExcludeNodeModules:!1,threadPool:!1,options:{compact:!0,controlFlowFlattening:!0,controlFlowFlatteningThreshold:1,deadCodeInjection:!1,debugProtection:!1,debugProtectionInterval:0,disableConsoleOutput:!1,identifierNamesGenerator:"hexadecimal",log:!1,numbersToExpressions:!1,renameGlobals:!1,selfDefending:!0,simplify:!0,splitStrings:!1,ignoreImports:!0,stringArray:!0,stringArrayCallsTransform:!0,stringArrayCallsTransformThreshold:.5,stringArrayEncoding:[],stringArrayIndexShift:!0,stringArrayRotate:!0,stringArrayShuffle:!0,stringArrayWrappersCount:1,stringArrayWrappersChainedCalls:!0,stringArrayWrappersParametersMaxCount:2,stringArrayWrappersType:"variable",stringArrayThreshold:.75,unicodeEscapeSequence:!1}},B="node_modules",z="vendor-modules",j="vendor-",S=(r=>(r.B="B",r.KB="KB",r.MB="MB",r))(S||{}),g=Object.freeze({info:"\x1B[36m",warn:"\x1B[33m",success:"\x1B[32m"});var m=class{constructor(e){this._log=e?console.log.bind(console):this.noop}noop(){}forceLog(...e){console.log(...e)}info(e){this._log(e)}};function w(){return O?.version?Number(O.version.split(".")[0]):2}function ie(t){let e=Math.floor(t/36e5),o=Math.floor(t%36e5/6e4),r=Math.floor(t%6e4/1e3);return[e?`${e}h `:"",o?`${o}m `:"",r||!e&&!o?`${r}s`:""].filter(Boolean).join("")}function $(t){return x(t)?t:p(t)&&"enable"in t?t.enable:!1}function D(t){return $(t.threadPool)}function H(t){return $(t.autoExcludeNodeModules)}function V(t){let{autoExcludeNodeModules:e}=t;return x(e)?[]:p(e)&&e.enable?e.manualChunks||[]:[]}function v(t){return j+t}function I(t){let{threadPool:e}=t,o=_.default.cpus().length;return x(e)?o:p(e)&&e.enable&&e.size?e.size:o}function W(t,e){let o=[];return Object.entries(e).forEach(([r,n])=>{"code"in n&&n.code&&!M(r,t.excludes)&&o.push([r,n])}),o}function G(t,e){for(let o of e)if(t.includes(o))return v(o);return z}function K(t,e,o){let r=new m(t.log);r.info(`obfuscating ${e}...`);let n=P.default.obfuscate(o.code,t.options).getObfuscatedCode();return r.info(`obfuscation complete for ${e}.`),n}function U(t,e){return new Promise((o,r)=>{let n=new F.Worker(N.default.join(__dirname,"./worker/index.js"));n.postMessage({config:t,chunk:e}),n.on("message",i=>{e.forEach(([s,c])=>{let u=i.find(a=>a.fileName===s);u&&u.obfuscatedCode&&(c.code=u.obfuscatedCode)}),o(i),n.unref()}),n.on("error",i=>{r(i),n.unref()})})}function T(t){let e=Object.values(S),o=0;for(;t>=1024&&o<e.length-1;)t/=1024,o++;return{value:Number(t.toFixed(2)),unit:e[o]}}var y=class{constructor(e){this._log=e,this.originalSize=this.createEmptySizeResult(),this.obfuscatedSize=this.createEmptySizeResult(),this.startTime=0,this.endTime=0}createEmptySizeResult(){return{original:{value:0,unit:"B"},gzip:{value:0,unit:"B"}}}start(e){this.startTime=performance.now(),this.originalSize=this.calculateBundleSize(e)}end(e){this.obfuscatedSize=this.calculateBundleSize(e),this.endTime=performance.now(),this.logResult()}calculateBundleSize(e){let{totalSize:o,gzipSize:r}=e.reduce((n,[,i])=>{if(i.code){let{code:s}=i;n.totalSize+=Buffer.byteLength(s,"utf-8"),n.gzipSize+=(0,A.gzipSync)(s,{level:9}).byteLength}return n},{totalSize:0,gzipSize:0});return{original:T(o),gzip:T(r)}}analyze(){let{originalSize:e,obfuscatedSize:o}=this,r=ie(this.endTime-this.startTime),n=((o.original.value-e.original.value)/e.original.value*100).toFixed(2),i=((o.gzip.value-e.gzip.value)/e.gzip.value*100).toFixed(2);return`\u2713 obfuscated in ${r} | \u{1F4E6} ${e.original.value}${e.original.unit} (gzip: ${e.gzip.value}${e.gzip.unit}) \u2192 \u{1F512} ${o.original.value}${o.original.unit} (gzip: ${o.gzip.value}${o.gzip.unit}) | \u{1F4C8} ${n}% (gzip: ${i}%)`}logResult(){this._log.forceLog(g.success+"%s\x1B[0m",this.analyze())}};function X(t){let e={...L,...t},o=new m(e.log),r=i=>{if(!e.enable||!H(e))return;i.build=i.build||{},i.build.rollupOptions=i.build.rollupOptions||{};let{output:s}=i.build.rollupOptions,c=[...V(e)],u=()=>{e.excludes.push(z,...c.map(v))},a=l=>{if(l.includes(B))return G(l,c)};if(!s){u(),i.build.rollupOptions.output={manualChunks:a};return}if(R(s)){o.forceLog(g.warn,"rollupOptions.output is an array, ignoring autoExcludeNodeModules configuration.");return}if(p(s)){if(!s.manualChunks)u(),s.manualChunks=a;else if(p(s.manualChunks))o.forceLog(g.warn,"rollupOptions.output.manualChunks is an object, ignoring autoExcludeNodeModules configuration.");else if(k(s.manualChunks)){let l=s.manualChunks;u(),s.manualChunks=(d,f)=>a(d)||l(d,f)}}},n=async(i,{bundle:s})=>{if(!e.enable||!s)return i;o.forceLog(g.info+` starting obfuscation process...`);let c=new y(o),u=W(e,s);if(c.start(u),D(e)){let a=Math.min(I(e),u.length),l=Math.ceil(u.length/a),d=[];for(let f=0;f<a;f++){let q=u.slice(f*l,(f+1)*l);d.push(U(e,q))}await Promise.all(d)}else u.forEach(([a,l])=>{l.code=K(e,a,l)});return c.end(u),i};return{name:"vite-plugin-bundle-obfuscator",apply:e.apply,config:r,transformIndexHtml:w()>=5?{order:"post",handler:n}:{enforce:"post",transform:n}}}