Problem
If you ever needed to transform a JavaScript file from:
require('foo');
To:
require('foo').default;
This codemod does not transform existing
require().default.
Solution
Then you can use require-default-codemod:
npx require-default-codemod <path>
The codemod appends the default property to CommonJS require.
To run the codemod in your current working directory:
npx require-default-codemod .
Methodology
This codemod was created using jscodeshift.
The implementation was pretty straightforward using the AST explorer:
- Find the
requireidentifier - Filter out those with
defaultproperty - Replace with an expression that has the original value and the
defaultproperty
Code
Check out the GitHub repository for more details.