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

Commit

Permalink
Fix env support and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy Leunen committed Jul 24, 2016
1 parent a271efa commit 0abf436
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 26 deletions.
11 changes: 10 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,14 @@
{ "src": "./test/examples/components", "expose": "components" },
{ "src": "npm:lodash", "expose": "underscore" }
]]
]
],
"env": {
"test": {
"plugins": [
["module-alias", [
{ "src": "./test/examples/components/sub/sub", "expose": "subsub" }
]]
]
}
}
}
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ const mapModule = require('babel-plugin-module-alias').mapModule;
const assign = require('object-assign');
const findBabelConfig = require('find-babel-config'); // eslint-disable-line

function getMappingFromBabel(start) {
// eslint-disable-next-line strict
'use strict';
function findModuleAliasConfig(conf) {
return conf.plugins.find(p => p[0] === 'module-alias');
}

function getMappingFromBabel(start) {
const c = findBabelConfig(start);
const env = process.env.BABEL_ENV || process.env.NODE_ENV || 'development';

if (c && c.config) {
let pluginConfig;

if (Array.isArray(c.config.plugins)) {
pluginConfig = c.config.plugins.find(p => p[0] === 'module-alias');
if (c.config.plugins) {
pluginConfig = findModuleAliasConfig(c.config);
}

if (c.config.env && c.config.env[env] && Array.isArray(c.config.env[env].plugins)) {
pluginConfig = c.config.env[env].plugins.find(p => p[0] === 'module-alias');
const envPluginConfig = findModuleAliasConfig(c.config.env[env]);
if (pluginConfig) {
pluginConfig[1] = pluginConfig[1].concat(envPluginConfig[1]);
} else {
pluginConfig = envPluginConfig;
}
}

if (pluginConfig) {
Expand Down
11 changes: 11 additions & 0 deletions test/examples/components/sub/envonly/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"env": {
"test": {
"plugins": [
["module-alias", [
{ "src": "../sub", "expose": "subsub" }
]]
]
}
}
}
Empty file.
90 changes: 72 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,83 @@ describe('eslint-import-resolver-module-alias', () => {
.to.eql({ found: true, path: null });
});

it('should return `false` with a file with an unknown extension', () => {
expect(resolverPlugin.resolve('./c3', path.resolve('./test/examples/components/c1'), opts))
.to.eql({ found: false });
describe('with specific file extensions', () => {
it('should return `false` with a file with an unknown extension', () => {
expect(resolverPlugin.resolve('./c3', path.resolve('./test/examples/components/c1'), opts))
.to.eql({ found: false });
});

it('should return `true` with a file with an expected extension', () => {
expect(resolverPlugin.resolve('./c3', path.resolve('./test/examples/components/c1'), extensionOpts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/c3.jsx')
});
});

it('should return `false` when mapped to a file with an unknown extension', () => {
expect(resolverPlugin.resolve('components/c3', path.resolve('./test/examples/components/subcomponent/sub/c2'), opts))
.to.eql({ found: false });
});

it('should return `true` when mapped to a file with an expected extension', () => {
expect(resolverPlugin.resolve('components/c3', path.resolve('./test/examples/components/subcomponent/sub/c2'), extensionOpts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/c3.jsx')
});
});
});

it('should return `true` with a file with an expected extension', () => {
expect(resolverPlugin.resolve('./c3', path.resolve('./test/examples/components/c1'), extensionOpts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/c3.jsx')
describe('mapping only in specific environment', () => {
describe('when both the default and "env" are available', () => {
it('should return `false` when the mapping doesn\'t exist in "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';

expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/c1'), opts))
.to.eql({ found: false });

process.env.NODE_ENV = oldEnv;
});
});

it('should return `false` when mapped to a file with an unknown extension', () => {
expect(resolverPlugin.resolve('components/c3', path.resolve('./test/examples/components/subcomponent/sub/c2'), opts))
.to.eql({ found: false });
});
it('should return `true` when the mapping exists in the "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';

it('should return `true` when mapped to a file with an expected extension', () => {
expect(resolverPlugin.resolve('components/c3', path.resolve('./test/examples/components/subcomponent/sub/c2'), extensionOpts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/c3.jsx')
expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/c1'), opts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/sub/sub/c2.js')
});

process.env.NODE_ENV = oldEnv;
});
});

describe('when ony specific env is available', () => {
it('should return `false` when the mapping doesn\'t exist in "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';

expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/sub/envonly/yo'), opts))
.to.eql({ found: false });

process.env.NODE_ENV = oldEnv;
});

it('should return `true` when the mapping exists in the "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';

expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/sub/envonly/yo'), opts))
.to.eql({
found: true,
path: path.resolve(__dirname, './examples/components/sub/sub/c2.js')
});

process.env.NODE_ENV = oldEnv;
});
});
});
});

0 comments on commit 0abf436

Please sign in to comment.