Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
antimatter15 committed Aug 19, 2016
1 parent 8c61641 commit e17c1c5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions demo/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function wumbo(){
return require('lodash')
}
35 changes: 35 additions & 0 deletions dist/uncommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (_ref) {
var t = _ref.types;

return {
visitor: {
CallExpression: function CallExpression(path) {
if (t.isIdentifier(path.node.callee, { name: "require" }) // detect require() expressions
&& !path.scope.getBinding('require') && // where "require" isn't bound to a variable
path.node.arguments.length == 1 && // with one argument
t.isStringLiteral(path.node.arguments[0])) {
// which is a string literal

var module = path.node.arguments[0].value;
// find the parent program
var program = path.findParent(function (node) {
return t.isProgram(node);
});
// generate an identifier which doesn't clash with anything
var id = program.scope.generateUidIdentifier(module);
path.replaceWith(id);

// create an import declaration
program.unshiftContainer('body', t.importDeclaration([t.importNamespaceSpecifier(id)], t.stringLiteral(module)));
}
}
}
};
};

26 changes: 26 additions & 0 deletions lib/uncommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function ({ types: t }) {
return {
visitor: {
CallExpression(path) {
if(t.isIdentifier(path.node.callee, { name: "require" }) // detect require() expressions
&& !path.scope.getBinding('require') && // where "require" isn't bound to a variable
path.node.arguments.length == 1 && // with one argument
t.isStringLiteral(path.node.arguments[0])){ // which is a string literal

var module = path.node.arguments[0].value;
// find the parent program
var program = path.findParent(node => t.isProgram(node))
// generate an identifier which doesn't clash with anything
var id = program.scope.generateUidIdentifier(module)
path.replaceWith(id)

// create an import declaration
program.unshiftContainer('body', t.importDeclaration([
t.importNamespaceSpecifier(id)
], t.stringLiteral(module)));
}
}
}
}
}

19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "babel-plugin-uncommon-transform",
"version": "0.5.0",
"description": "Transform CommonJS require() into ES2015 imports",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eponymous-labs/babel-plugin-uncommon-transform.git"
},
"author": "antimatter15 <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/eponymous-labs/babel-plugin-uncommon-transform/issues"
},
"homepage": "https://github.com/eponymous-labs/babel-plugin-uncommon-transform#readme"
}

0 comments on commit e17c1c5

Please sign in to comment.