@mnrendra/mixexports
Version:
A function to mix CommonJS exports.
7 lines (6 loc) • 5.05 kB
JavaScript
"use strict";var x=require("esbuild"),g=require("acorn"),b=require("acorn-walk"),d=require("escodegen"),h=require("require-from-string");const y=e=>{if(!e.startsWith("#!"))return{code:e};const t=e.indexOf(`
`),r=e.slice(0,t).trim(),o=e.slice(t+1);if(r.includes(`
`)||r.includes("\r")||r.includes(`\r
`))throw new Error('Your code contains "\\r\\n" or "\\r". Please change it to "\\n"!');return{shebang:r,code:o}},u=(e,t,r)=>{e[t]={...e[t],...r}},w=e=>({left:t})=>{if(t.type==="MemberExpression"&&t.object.type==="Identifier"&&t.property.type==="Identifier"){const{object:r,property:o}=t;if(r.name==="module"&&o.name==="exports")e.hasModuleExports=!0,r.name="exports",o.name="default",u(e.expor,"default",{value:"exports.default"});else if(r.name==="exports"){const{name:n}=o;e.hasExportsDefault=n==="default",u(e.expor,n,{value:`exports.${n}`})}}},c={format:{newline:""}},v=(e,t,r)=>{if(r.type!=="ObjectExpression")throw new Error(`Invalid named export property descriptor! Start: ${r.start}, End: ${r.end}`);const o={};r.properties.forEach(a=>{a.type==="Property"&&a.key.type==="Identifier"&&(o[a.key.name]=a.value)});const n=Object.keys(o),i=a=>new Error(`The \`${t}\` export has ${a} attributes, so it cannot be imported using destructuring as \`import { ${t} }\`!`);if(n.includes("get")&&n.length>2||n.includes("get")&&n.length>1&&!n.includes("enumerable"))throw i("a getter and additional");if(n.includes("value")&&n.includes("set"))throw i("a value and setter");if(n.includes("value")&&n.includes("configurable"))throw i("a value and configurable");if(!n.includes("value")&&!n.includes("get"))throw i("no value or getter");n.forEach(a=>{if(a==="get")u(e.expor,t,{get:d.generate(o.get,c)});else if(a==="value")u(e.expor,t,{value:`exports.${t}`});else if(a==="enumerable"){const{enumerable:s}=o;if(s.type==="UnaryExpression"&&s.operator==="!"&&s.argument.type==="Literal"&&typeof s.argument.value=="number"&&typeof s.argument.raw=="string"&&`${s.argument.value}`===s.argument.raw&&s.argument.value===0||s.type==="Literal"&&typeof s.value=="boolean"&&typeof s.raw=="string"&&`${s.value}`===s.raw&&s.raw==="true")r.properties=r.properties.filter(l=>!(l.type==="Property"&&l.key.type==="Identifier"&&l.key.name==="enumerable"));else throw i("a value or getter with `enumerable` set to `false` in its")}else a==="writable"?u(e.expor,t,{writable:d.generate(o.writable,c)}):u(e.expor,t,JSON.parse(`{ "${a}": ${d.generate(o[a],c)} }`))})},$=(e,t)=>{const[r,o,n]=t;if(r.type==="Identifier"&&r.name==="exports"){if(!(o.type==="Literal"&&typeof o.value=="string"&&typeof o.raw=="string"&&(`"${o.value}"`===o.raw||`'${o.value}'`===o.raw)))throw new Error(`Unable to extract the named export! Start: ${o.start}, End: ${o.end}`);v(e,o.value,n)}},E=(e,t)=>{const[r]=t;if(r.type==="Identifier"&&r.name==="exports")throw new Error("The `exports` object is defined using `defineProperties`, so it cannot be imported using destructuring as `import { ... }`!")},j=e=>({callee:t,arguments:r})=>{if(t.type==="MemberExpression"&&t.object.type==="Identifier"&&t.object.name==="Object"&&t.property.type==="Identifier")switch(t.property.name){case"defineProperty":$(e,r);break;case"defineProperties":E(e,r);break}},M=e=>{const t={hasModuleExports:!1,hasExportsDefault:!1,expor:{}},{shebang:r,code:o}=y(e),n=g.parse(o,{sourceType:"module",ecmaVersion:"latest"});if(b.simple(n,{AssignmentExpression:w(t),CallExpression:j(t)}),t.hasModuleExports&&t.hasExportsDefault)throw new Error("Your code contains both `module.exports` and `exports.default`, which is not recommended. Please use one!");return{shebang:r,code:d.generate(n),expor:t.expor}},p=e=>{const t="Failed to mix exports:",r=`"${e}" cannot be used as a default value.`,o='Only "object" and "function" are allowed.';return new Error(t+" "+r+" "+o)},k=e=>{if(e===null)throw p("null");switch(typeof e){case"bigint":throw p("bigint");case"boolean":throw p("boolean");case"number":throw p("number");case"string":throw p("string");case"symbol":throw p("symbol")}return e},O=(e,t)=>{const r=h(e);return k(r.default),{hasDefault:t.default!==void 0&&r.default!==void 0}},I=e=>e?"module.exports=exports.default;":"module.exports={};exports.default=module.exports;",P=e=>`__esModule:{value:${e!==void 0?`${e}`:"exports.__esModule"}},`,D=(e,t)=>Object.keys(e).filter(r=>r!=="__esModule"&&r!=="default").map(r=>`${r}:${JSON.stringify(t?e[r]:{...e[r],enumerable:!0}).replace(/"/g,"")},`).join(""),_=(e,{hasDefault:t,defineEsModule:r,expor:o})=>""+e+I(t)+"Object.defineProperties(module.exports,{"+P(r)+D(o,t)+"default:{value:exports.default}});",q=async(e,{defineEsModule:t,minify:r=!1}={})=>{const{shebang:o,code:n,expor:i}=M(e),a=typeof o=="string",s=Object.keys(i).length>0;let l="";if(a&&s)throw new Error(`Your code contains a shebang "${o}" and exports, so it cannot be processed!`);if(!a&&s){const{hasDefault:m}=O(n,i);l=_(n,{hasDefault:m,defineEsModule:t,expor:i})}else a&&!s&&(l=n);const{code:f}=await x.transform(l,{format:"cjs",minify:r});return a?`${o}
${f}`:f};module.exports=q;
//# sourceMappingURL=index.js.map