Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Support module directories with index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogasparin committed Jun 16, 2017
1 parent 0c87f2a commit b7387d0
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
"babel-preset-stage-0": "^6.24.1",
"babel-register": "^6.24.1",
"chai": "^4.0.1",
"date-fns": "^1.28.5",
"lodash-bound": "^1.1.2",
"lodash-compat": "^3.10.2",
"lodash-es": "^4.17.4",
"material-ui": "^0.18.3",
"mocha": "^3.4.2",
"ramda": "^0.24.1",
"react-bootstrap": "^0.31.0"
Expand Down
7 changes: 7 additions & 0 deletions src/ModuleCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ export default class ModuleCache extends MapCache {
_.each(dirPaths, dirPath => {
const base = path.relative(moduleRoot, dirPath);
const filePaths = glob.sync(path.join(dirPath, '*.js'));
if (base && _.includes(filePaths, path.join(dirPath, 'index.js'))) {
const indexEntry = path.parse(base);
const parentMap = this.get(indexEntry.dir);
if (parentMap && !parentMap.get(indexEntry.base.toLowerCase())) {
parentMap.set(indexEntry.base.toLowerCase(), indexEntry.base);
}
}
const pairs = _.map(filePaths, filePath => {
const name = path.basename(filePath, '.js');
return [name.toLowerCase(), name];
Expand Down
11 changes: 6 additions & 5 deletions src/importModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import mapping from './mapping';

function resolvePath(pkgStore, name, path) {
let { base, id } = pkgStore;
const lower = name.toLowerCase();
const module = mapping.modules.get(id);

if (!module.get(base).has(lower)) {
base = base ? '' : module.findKey(map => map.has(lower));
const nameCases = [name.toLowerCase(), _.kebabCase(name), _.snakeCase(name)];
let realName = _.find(nameCases, n => module.get(base).has(n));

if (!realName) {
base = base ? '' : module.findKey(map => realName = _.find(nameCases, n => map.has(n)));
if (!base) {
throw path.buildCodeFrameError([
`The '${ id }' method \`${ name }\` is not a known module.`,
'Please report bugs to https://github.com/lodash/babel-plugin-lodash/issues.'
].join('\n'));
}
}
return id + '/' + (base ? base + '/' : '') + module.get(base).get(lower);
return id + '/' + (base ? base + '/' : '') + module.get(base).get(realName);
}

function importModule(pkgStore, name, path) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function lodash({ types }) {
},

ImportDeclaration(path) {
if (store.get(path.node.source.value)) {
if (path.node.source.loc && store.get(path.node.source.value)) {
// Remove old import.
path.remove();
}
Expand Down
5 changes: 5 additions & 0 deletions test/mixed-fixtures/date-fns/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["env", { "targets": { "node": 4 } }]
]
}
5 changes: 5 additions & 0 deletions test/mixed-fixtures/date-fns/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dt from 'date-fns';
import { isToday } from 'date-fns';

dt.isTomorrow(Date.now());
isToday(Date.now());
14 changes: 14 additions & 0 deletions test/mixed-fixtures/date-fns/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var _is_today = require('date-fns/is_today');

var _is_today2 = _interopRequireDefault(_is_today);

var _is_tomorrow = require('date-fns/is_tomorrow');

var _is_tomorrow2 = _interopRequireDefault(_is_tomorrow);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _is_tomorrow2.default)(Date.now());
(0, _is_today2.default)(Date.now());
3 changes: 3 additions & 0 deletions test/mixed-fixtures/date-fns/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "date-fns"
}
6 changes: 6 additions & 0 deletions test/mixed-fixtures/material-ui/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
["env", { "targets": { "node": 4 } }],
"react"
]
}
8 changes: 8 additions & 0 deletions test/mixed-fixtures/material-ui/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import { AppBar } from 'material-ui';

<MuiThemeProvider>
<AppBar />
</MuiThemeProvider>

export { AutoComplete } from 'material-ui';
28 changes: 28 additions & 0 deletions test/mixed-fixtures/material-ui/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

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

var _AutoComplete2 = require('material-ui/AutoComplete');

var _AutoComplete3 = _interopRequireDefault(_AutoComplete2);

var _AppBar2 = require('material-ui/AppBar');

var _AppBar3 = _interopRequireDefault(_AppBar2);

var _MuiThemeProvider = require('material-ui/styles/MuiThemeProvider');

var _MuiThemeProvider2 = _interopRequireDefault(_MuiThemeProvider);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

React.createElement(
_MuiThemeProvider2.default,
null,
React.createElement(_AppBar3.default, null)
);

exports.AutoComplete = _AutoComplete3.default;
3 changes: 3 additions & 0 deletions test/mixed-fixtures/material-ui/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"id": "material-ui"
}

0 comments on commit b7387d0

Please sign in to comment.