Skip to content

Commit

Permalink
replacing specifier
Browse files Browse the repository at this point in the history
  • Loading branch information
antimatter15 committed Aug 19, 2016
1 parent d1eb897 commit e57c2d6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# babel-plugin-uncommon-transform

Transform CommonJS require() into ES2015 imports

### Example

Input code:

require('lodash').range(10)

var express = require('express')

Result:

import _lodash from 'lodash';
_lodash.range(10)

import _express from 'express';
var express = _express;

6 changes: 6 additions & 0 deletions demo/result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import _lodash from 'lodash';
function wumbo() {
return _lodash;
}

var derp = wumbo();
4 changes: 3 additions & 1 deletion demo/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function wumbo(){
return require('lodash')
}
}

var derp = wumbo()
2 changes: 1 addition & 1 deletion dist/uncommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ exports.default = function (_ref) {
path.replaceWith(id);

// create an import declaration
program.unshiftContainer('body', t.importDeclaration([t.importNamespaceSpecifier(id)], t.stringLiteral(module)));
program.unshiftContainer('body', t.importDeclaration([t.importDefaultSpecifier(id)], t.stringLiteral(module)));
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion lib/uncommon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Copyright (c) 2016 antimatter15

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

export default function ({ types: t }) {
return {
visitor: {
Expand All @@ -16,7 +36,7 @@ export default function ({ types: t }) {

// create an import declaration
program.unshiftContainer('body', t.importDeclaration([
t.importNamespaceSpecifier(id)
t.importDefaultSpecifier(id)
], t.stringLiteral(module)));
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-uncommon-transform",
"version": "0.5.1",
"version": "0.6",
"description": "Transform CommonJS require() into ES2015 imports",
"main": "dist/uncommon.js",
"scripts": {
Expand Down

0 comments on commit e57c2d6

Please sign in to comment.