Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting!
Is it executed once per execution?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know yes.

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 */
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you just name the anonym fn with "test" instead? You should be able to remove these 2 eslint rules after that

Copy link
Copy Markdown
Author

@ghost ghost Aug 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still would warn me prefer-arrow-callback.

Also I'd need to give the upper scope functions (root and alias) different names to not trigger the no-shadow rule.

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