From e57c2d6ca96c05bbd4bf79a82c02d7b3d3797b05 Mon Sep 17 00:00:00 2001 From: Kevin Kwok Date: Thu, 18 Aug 2016 20:59:07 -0700 Subject: [PATCH] replacing specifier --- README.md | 18 ++++++++++++++++++ demo/result.js | 6 ++++++ demo/test.js | 4 +++- dist/uncommon.js | 2 +- lib/uncommon.js | 22 +++++++++++++++++++++- package.json | 2 +- 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 demo/result.js diff --git a/README.md b/README.md index f373b6a..961cd3e 100644 --- a/README.md +++ b/README.md @@ -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; + diff --git a/demo/result.js b/demo/result.js new file mode 100644 index 0000000..c662382 --- /dev/null +++ b/demo/result.js @@ -0,0 +1,6 @@ +import _lodash from 'lodash'; +function wumbo() { + return _lodash; +} + +var derp = wumbo(); \ No newline at end of file diff --git a/demo/test.js b/demo/test.js index b2ad1b5..0b3d53b 100644 --- a/demo/test.js +++ b/demo/test.js @@ -1,3 +1,5 @@ function wumbo(){ return require('lodash') -} \ No newline at end of file +} + +var derp = wumbo() diff --git a/dist/uncommon.js b/dist/uncommon.js index 644b712..51120de 100644 --- a/dist/uncommon.js +++ b/dist/uncommon.js @@ -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))); } } } diff --git a/lib/uncommon.js b/lib/uncommon.js index b6c5900..ee87764 100644 --- a/lib/uncommon.js +++ b/lib/uncommon.js @@ -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: { @@ -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))); } } diff --git a/package.json b/package.json index 944ed73..fe10ff4 100644 --- a/package.json +++ b/package.json @@ -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": {