UNPKG

commonjs-to-es-module-codemod

Version:

jscodeshift codemod that convert CommonJS(require/exports) to ES Modules(import/export) for JavaScript/TypeScript

3 lines (2 loc) 5.7 kB
class e{constructor(e,t){this.prefix=`${e.path}:`,this.silent=t.silent,this.verbose=t.verbose}log(...e){!this.silent&&this.verbose>0&&console.log("[LOG]",this.prefix,...e)}warn(...e){this.silent||console.warn("[WARNING]",this.prefix,...e)}error(...e){this.silent||console.error("[ERROR]",this.prefix,...e)}lines(e){return e.loc?e.loc.start.line===e.loc.end.line?`(line ${e.loc.start.line})`:`(lines ${e.loc.start.line} to ${e.loc.end.line})`:""}}const t=(e,t)=>e.Program.check(t.parent.value);function r(r,n,o){const i=n.jscodeshift,s=new e(r,o),l=i(r.source).find(i.VariableDeclaration,{declarations:[{init:{type:"CallExpression",callee:{name:"require"}}}]}).filter(e=>t(i,e));return s.log(`${l.length} nodes will be transformed`),l.replaceWith(e=>{const t=[],n=[];for(const a of e.node.declarations){const e=null!==a.init&&"CallExpression"===a.init.type&&"require"===a.init.callee.name;if(e&&void 0!==a.init.property)if("Identifier"===a.id.type){var o,l;const e=a.init.arguments.shift();if(a.init.arguments.length)return s.error(`${s.lines(a)} too many arguments.Aborting transformation`),r.source;if(!i.Literal.check(e))return s.error(`${s.lines(a)} bad argument.Expecting a string literal, got `+i(e).toSource()+"`. Aborting transformation"),r.source;"Identifier"===(null==a||null==(o=a.init)?void 0:o.property.type)&&s.log("Unknown declaration",a);const t=i.importSpecifier(a.init.property,null==a||null==(l=a.init)?void 0:l.property);n.push(i.importDeclaration([t],e))}else"ObjectPattern"===a.id.type&&s.log("Does not support pattern",a);else if(e){if("Identifier"===a.id.type){const e=i.importDefaultSpecifier(a.id),t=a.init.arguments.shift();if(a.init.arguments.length)return s.error(`${s.lines(a)} too many arguments.Aborting transformation`),r.source;if(!i.Literal.check(t))return s.error(`${s.lines(a)} bad argument.Expecting a string literal, got `+i(t).toSource()+"`. Aborting transformation"),r.source;n.push(i.importDeclaration([e],t))}else if("ObjectPattern"===a.id.type){const e=a.id.properties.map(e=>{const t=i.identifier(e.key.name),r=i.identifier(e.value.name);return i.importSpecifier(t,r)}),t=a.init.arguments.shift();if(a.init.arguments.length)return s.error(`${s.lines(a)} too many arguments.Aborting transformation`),r.source;if(!i.Literal.check(t))return s.error(`${s.lines(a)} bad argument.Expecting a string literal, got `+i(t).toSource()+"`. Aborting transformation"),r.source;n.push(i.importDeclaration(e,t))}}else t.push(a)}return n.length>0&&(n[0].comments=e.node.comments),t.length>0?(s.warn(`${s.lines(e.node)} introduced leftover`),[...n,i.variableDeclaration(e.node.kind,t)]):n}).toSource()}function n(r,n,o){const i=n.jscodeshift,s=new e(r,o),l=i(r.source).find(i.VariableDeclaration,{declarations:[{init:{type:"MemberExpression",object:{type:"CallExpression",callee:{name:"require"}}}}]}).filter(e=>t(i,e));return s.log(`${l.length} nodes will be transformed`),l.replaceWith(e=>{const t=[],n=[];for(const l of e.node.declarations)if(null!==l.init&&"MemberExpression"===l.init.type&&"CallExpression"===l.init.object.type&&"require"===l.init.object.callee.name&&void 0!==l.init.property)if("Identifier"===l.id.type){var o;const e=l.init.object.arguments.shift();if(l.init.object.arguments.length)return s.error(`${s.lines(l)} too many arguments.Aborting transformation`),r.source;if(!i.Literal.check(e))return s.error(`${s.lines(l)} bad argument.Expecting a string literal, got `+i(e).toSource()+"`. Aborting transformation"),r.source;if("Identifier"!==(null==l||null==(o=l.init)?void 0:o.property.type))return s.log("Unknown declaration",l),r.source;const t=i.importSpecifier(l.init.property,l.id);n.push(i.importDeclaration([t],e))}else"ObjectPattern"===l.id.type&&s.log("Does not support pattern",l);else t.push(l);return n.length>0&&(n[0].comments=e.node.comments),t.length>0?(s.warn(`${s.lines(e.node)} introduced leftover`),[...n,i.variableDeclaration(e.node.kind,t)]):n}).toSource()}function o(r,n,o){const i=n.jscodeshift,s=new e(r,o),l=i(r.source).find(i.ExpressionStatement,{expression:{left:{object:{name:"module"},property:{name:"exports"}},operator:"="}}).filter(e=>t(i,e));return l.length>1?(s.error("There should not be more than one `module.exports` declaration in a file. Aborting transformation"),r.source):(s.log(`${l.length} nodes will be transformed`),l.replaceWith(e=>{const t=i.exportDefaultDeclaration(e.node.expression.right);return t.comments=e.node.comments,t}).toSource())}function i(r,n,o){const i=n.jscodeshift,s=e=>t(i,e),l=new e(r,o),a=i(r.source),c=a.find(i.ExpressionStatement,{expression:{left:{object:{object:{name:"module"},property:{name:"exports"}}},operator:"="}}).filter(s),p=a.find(i.ExpressionStatement,{expression:{left:{object:{name:"exports"}},operator:"="}}).filter(s);l.log(`${c.length+p.length} nodes will be transformed`);const u=e=>{const t=e.node,r=t.expression.left.property,n=t.expression.right;if("Identifier"===r.type&&"Identifier"===n.type){const e=i.exportNamedDeclaration(null,[i.exportSpecifier.from({exported:r,local:n})]);return e.comments=t.comments,e}const o=i.variableDeclaration("const",[i.variableDeclarator(r,n)]),s=i.exportNamedDeclaration(o);return s.comments=t.comments,s};return p.replaceWith(u),c.replaceWith(u),a.toSource()}function s(r,n,o){const i=n.jscodeshift,s=new e(r,o),l=i(r.source).find(i.ExpressionStatement,{expression:{callee:{name:"require"}}}).filter(e=>t(i,e));return s.log(`${l.length} nodes will be transformed`),l.replaceWith(e=>{const t=e.node.expression.arguments.pop(),r=i.importDeclaration([],t);return r.comments=e.node.comments,r}).toSource()}module.exports=(e,t,l)=>[o,n,s,r,i].reduce((e,r)=>r({source:e},t,l),e.source); //# sourceMappingURL=index.modern.js.map