Skip to content

Commit

Permalink
feat: Add glob support in the root config (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moe Sattler authored and tleunen committed Aug 27, 2016
1 parent 922857d commit 1f6245b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"import"
],
"dependencies": {
"glob": "^7.0.6",
"resolve": "^1.1.7"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'path';
import resolve from 'resolve';
import glob from 'glob';
import mapToRelative from './mapToRelative';

function createAliasFileMap(pluginOpts) {
Expand Down Expand Up @@ -110,6 +111,17 @@ export default ({ types: t }) => {
}

return {
manipulateOptions(babelOptions) {
const findPluginOptions = babelOptions.plugins.find(plugin => plugin[0] === this)[1];
if (findPluginOptions.root) {
findPluginOptions.root = findPluginOptions.root.reduce((resolvedDirs, dirPath) => {
if (glob.hasMagic(dirPath)) {
return resolvedDirs.concat(glob.sync(dirPath));
}
return resolvedDirs.concat(dirPath);
}, []);
}
},
visitor: {
CallExpression: {
exit(nodePath, state) {
Expand Down
64 changes: 41 additions & 23 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
/* eslint-env mocha */
/* eslint-disable prefer-arrow-callback */
/* eslint-disable func-names */
import assert from 'assert';
import { transform } from 'babel-core'; // eslint-disable-line import/no-extraneous-dependencies
import plugin from '../src';

function testRequireImport(source, output, transformerOpts) {
it('with a require statement', () => {
it('with a require statement', function () {
const code = `var something = require("${source}");`;
const result = transform(code, transformerOpts);

assert.strictEqual(result.code, `var something = require("${output}");`);
});

it('with an import statement', () => {
it('with an import statement', function () {
const code = `import something from "${source}";`;
const result = transform(code, transformerOpts);

assert.strictEqual(result.code, `import something from "${output}";`);
});
}

describe('root', () => {
describe('root', function () {
const transformerOpts = {
plugins: [
[plugin, {
Expand All @@ -28,48 +30,64 @@ describe('root', () => {
]
};

describe('should rewrite the file path inside a root directory', () => {
const transformerOptsGlob = {
plugins: [
[plugin, {
root: ['./test/**/components']
}]
]
};

describe('should rewrite the file path inside a root directory', function () {
testRequireImport(
'c1',
'./test/examples/components/c1',
transformerOpts
);
});

describe('should rewrite the sub file path inside a root directory', () => {
describe('should rewrite the sub file path inside a root directory', function () {
testRequireImport(
'sub/sub1',
'./test/examples/components/sub/sub1',
transformerOpts
);
});

describe('should rewrite the file while keeping the extension', () => {
describe('should rewrite the file while keeping the extension', function () {
testRequireImport(
'sub/sub1.css',
'./test/examples/components/sub/sub1.css',
transformerOpts
);
});

describe('should rewrite the file with a filename containing a dot', () => {
describe('should rewrite the file with a filename containing a dot', function () {
testRequireImport(
'sub/custom.modernizr3',
'./test/examples/components/sub/custom.modernizr3',
transformerOpts
);
});

describe('should not rewrite a path outisde of the root directory', () => {
describe('should not rewrite a path outisde of the root directory', function () {
testRequireImport(
'example-file',
'example-file',
transformerOpts
);
});

describe('should rewrite the file path inside a root directory according to glob', function () {
testRequireImport(
'c1',
'./test/examples/components/c1',
transformerOptsGlob
);
});
});

describe('alias', () => {
describe('alias', function () {
const transformerOpts = {
plugins: [
[plugin, {
Expand All @@ -83,17 +101,17 @@ describe('alias', () => {
]
};

describe('should alias a known path', () => {
describe('using a simple exposed name', () => {
describe('when requiring the exact name', () => {
describe('should alias a known path', function () {
describe('using a simple exposed name', function () {
describe('when requiring the exact name', function () {
testRequireImport(
'utils',
'./src/mylib/subfolder/utils',
transformerOpts
);
});

describe('when requiring a sub file of the exposed name', () => {
describe('when requiring a sub file of the exposed name', function () {
testRequireImport(
'utils/my-util-file',
'./src/mylib/subfolder/utils/my-util-file',
Expand All @@ -102,16 +120,16 @@ describe('alias', () => {
});
});

describe('using a "complex" exposed name', () => {
describe('when requiring the exact name', () => {
describe('using a "complex" exposed name', function () {
describe('when requiring the exact name', function () {
testRequireImport(
'awesome/components',
'./src/components',
transformerOpts
);
});

describe('when requiring a sub file of the exposed name', () => {
describe('when requiring a sub file of the exposed name', function () {
testRequireImport(
'awesome/components/my-comp',
'./src/components/my-comp',
Expand All @@ -120,7 +138,7 @@ describe('alias', () => {
});
});

describe('with a dot in the filename', () => {
describe('with a dot in the filename', function () {
testRequireImport(
'utils/custom.modernizr3',
'./src/mylib/subfolder/utils/custom.modernizr3',
Expand All @@ -129,24 +147,24 @@ describe('alias', () => {
});
});

describe('should alias the path with its extension', () => {
describe('should alias the path with its extension', function () {
testRequireImport(
'awesome/components/my-comp.css',
'./src/components/my-comp.css',
transformerOpts
);
});

describe('should not alias a unknown path', () => {
describe('when requiring a node module', () => {
describe('should not alias a unknown path', function () {
describe('when requiring a node module', function () {
testRequireImport(
'other-lib',
'other-lib',
transformerOpts
);
});

describe('when requiring a specific un-mapped file', () => {
describe('when requiring a specific un-mapped file', function () {
testRequireImport(
'./l/otherLib',
'./l/otherLib',
Expand All @@ -155,15 +173,15 @@ describe('alias', () => {
});
});

describe('(legacy) should support aliasing a node module with "npm:"', () => {
describe('(legacy) should support aliasing a node module with "npm:"', function () {
testRequireImport(
'abstract/thing',
'concrete/thing',
transformerOpts
);
});

describe('should support aliasing a node modules', () => {
describe('should support aliasing a node modules', function () {
testRequireImport(
'underscore/map',
'lodash/map',
Expand Down

0 comments on commit 1f6245b

Please sign in to comment.