|
| 1 | +/** |
| 2 | + * Simple cjs -> esm transform for common patterns: |
| 3 | + * - const X = require('...') -> import X from '...' |
| 4 | + * - const {a, b} = require('...') -> import {a, b} from '...' |
| 5 | + * - exports.foo = expr -> export const foo = expr |
| 6 | + * - module.exports = expr -> export default expr |
| 7 | + * |
| 8 | + * Note: this is a best-effort codemod and will need manual review. |
| 9 | + */ |
| 10 | + |
| 11 | +module.exports = function (fileInfo, api) { |
| 12 | + const j = api.jscodeshift |
| 13 | + const root = j(fileInfo.source) |
| 14 | + |
| 15 | + // transform `const x = require('mod')` and destructuring |
| 16 | + root.find(j.VariableDeclarator, { |
| 17 | + init: { |
| 18 | + type: 'CallExpression', |
| 19 | + callee: { name: 'require' }, |
| 20 | + arguments: args => args && args.length === 1 && args[0].type === 'Literal', |
| 21 | + }, |
| 22 | + }).forEach((path) => { |
| 23 | + const init = path.node.init |
| 24 | + const source = init.arguments[0].value |
| 25 | + const id = path.node.id |
| 26 | + let importDecl |
| 27 | + |
| 28 | + if (id.type === 'Identifier') { |
| 29 | + importDecl = j.importDeclaration( |
| 30 | + [j.importDefaultSpecifier(j.identifier(id.name))], |
| 31 | + j.literal(source), |
| 32 | + ) |
| 33 | + j(path.parent).replaceWith(importDecl) |
| 34 | + } else if (id.type === 'ObjectPattern') { |
| 35 | + const specifiers = id.properties.map((prop) => { |
| 36 | + const imported = j.identifier(prop.key.name) |
| 37 | + const local = prop.value ? j.identifier(prop.value.name) : j.identifier(prop.key.name) |
| 38 | + return j.importSpecifier(imported, local) |
| 39 | + }) |
| 40 | + importDecl = j.importDeclaration(specifiers, j.literal(source)) |
| 41 | + j(path.parent).replaceWith(importDecl) |
| 42 | + } |
| 43 | + }) |
| 44 | + |
| 45 | + // transform `exports.foo = expr` -> `export const foo = expr` |
| 46 | + root.find(j.AssignmentExpression, { |
| 47 | + left: { |
| 48 | + type: 'MemberExpression', |
| 49 | + object: { name: 'exports' }, |
| 50 | + }, |
| 51 | + }).forEach((p) => { |
| 52 | + const left = p.node.left |
| 53 | + const name = left.property.name || (left.property.value && String(left.property.value)) |
| 54 | + const right = p.node.right |
| 55 | + const exportDecl = j.exportNamedDeclaration( |
| 56 | + j.variableDeclaration('const', [j.variableDeclarator(j.identifier(name), right)]), |
| 57 | + [], |
| 58 | + ) |
| 59 | + j(p.parent).replaceWith(exportDecl) |
| 60 | + }) |
| 61 | + |
| 62 | + // transform `module.exports = X` -> `export default X` |
| 63 | + root.find(j.AssignmentExpression, { |
| 64 | + left: { |
| 65 | + type: 'MemberExpression', |
| 66 | + object: { name: 'module' }, |
| 67 | + property: { name: 'exports' }, |
| 68 | + }, |
| 69 | + }).forEach((p) => { |
| 70 | + const right = p.node.right |
| 71 | + const exportDecl = j.exportDefaultDeclaration(right) |
| 72 | + j(p.parent).replaceWith(exportDecl) |
| 73 | + }) |
| 74 | + |
| 75 | + return root.toSource({ quote: 'single' }) |
| 76 | +} |
0 commit comments