Skip to content

Instantly share code, notes, and snippets.

@nsfmc
Created February 24, 2016 17:12
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 nsfmc/aebef4e7a4944819ff40 to your computer and use it in GitHub Desktop.
Save nsfmc/aebef4e7a4944819ff40 to your computer and use it in GitHub Desktop.
change .default-less require calls for certain modules to require().default
/**
* add .default to requires for 'stores/index' that don't already have them.
*
* this comes up because babel6 Does The Right Thing with es6 exports and
* commonjs requires now need to play by the rules of the es6 exported modules
* and specify the default argument. see more:
* https://medium.com/@kentcdodds/misunderstanding-es6-modules-upgrading-babel-tears-and-a-solution-ad2d5ab93ce0#.tjlvbfd4x
*
*/
export default function transformer(file, api) {
const j = api.jscodeshift;
const {expression, statement, statements} = j.template;
return j(file.source)
.find(j.VariableDeclarator, {init: { type: 'CallExpression', callee:
{name: 'require'},
arguments: [{value: 'stores/index'}]}})
.replaceWith(
p => j.variableDeclarator(
p.node.id,
j.memberExpression(
p.node.init,
j.identifier('default')
))
)
.toSource();
};
@nsfmc
Copy link
Author

nsfmc commented Feb 24, 2016

the effect is going from

var newStore = require('stores/index').default;
var oldStore = require('stores/index');
var somethingElse = require('storeso/index');

to

var newStore = require('stores/index').default;
var oldStore = require('stores/index').default;
var somethingElse = require('storeso/index');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment