Skip to content

Instantly share code, notes, and snippets.

@krissrex
Last active July 13, 2020 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krissrex/97dbbfdf1f906d797ab5c8cc7ae67384 to your computer and use it in GitHub Desktop.
Save krissrex/97dbbfdf1f906d797ab5c8cc7ae67384 to your computer and use it in GitHub Desktop.
jscodeshift transforms from import/export to require/module.exports for javascript
#!/bin/bash
jscodeshift -t transform-import-to-cjs.js --no-babel --parser=flow src/
// Press ctrl+space for code completion
module.exports = function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ImportDeclaration)
.forEach((path) => {
const node = path.value;
const specifiers = node.specifiers;
const comments = node.comments;
const destructuredImports = [];
specifiers
.filter((specifier) => specifier.type === "ImportSpecifier")
.forEach((specifier) => {
const importName = specifier.local.name;
destructuredImports.push(importName);
specifiers.splice(specifiers.indexOf(specifier), 1);
});
if (destructuredImports.length) {
const requireDeclaration = j.variableDeclaration("const", [
j.variableDeclarator(
j.objectPattern(
destructuredImports.map((name) => {
const prop = j.objectProperty(
j.identifier(name),
j.identifier(name)
);
prop.shorthand = true;
return prop;
})
),
j.callExpression(j.identifier("require"), [node.source])
),
]);
// Comments are lost on the path.replace(), including file level comments for the first import.
requireDeclaration.comments = comments;
path.insertBefore(requireDeclaration);
}
if (specifiers.length == 0) {
path.replace();
}
})
.toSource();
};
#!/bin/bash
jscodeshift -t transform-destructured-import-to-cjs.js --no-babel --parser=flow src/
#!/bin/bash
find src -type f -name '*.js' -exec sed -i 's/^export default {/module.exports = {/g' {} +
find src -type f -name '*.js' -exec sed -i 's/^export default /module.exports = /g' {} +
find src -type f -name '*.js' -exec sed -i 's/^export {/module.exports = {/g' {} +
grep 'module.exports' -Ro src/ | sort | uniq -c | sort
echo "Fix any duplicate module.exports"
/*
Converts
```
import myThing from "myLib";
```
to
```
const myThing = require("myLib");
```
*/
// TODO: avoid replacing if the import is both default and destructured:
// import myThing, { thingX, thingY } from "myLib";
// TODO: convert namespaced imports:
// import * as myThing from "myLib";
module.exports = function transformer(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ImportDeclaration)
.forEach((path) => {
const node = path.value;
const specifiers = node.specifiers;
const comments = node.comments;
specifiers
.filter((specifier) => specifier.type === "ImportDefaultSpecifier")
.forEach((specifier) => {
const importName = specifier.local.name;
const requireDeclaration = j.variableDeclaration("const", [
j.variableDeclarator(
j.identifier(importName),
j.callExpression(j.identifier("require"), [node.source])
),
]);
requireDeclaration.comments = comments;
path.replace(requireDeclaration);
});
})
.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment