-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c61641
commit e17c1c5
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function wumbo(){ | ||
return require('lodash') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |