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

Commit

Permalink
fix: Fix support for custom extensions from babel (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tleunen committed Feb 5, 2017
1 parent 45a78be commit fa885e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ function getPlugins(file, target) {
}
}

function opts(file, config) {
return {
...config,
basedir: path.dirname(file),
};
}

exports.interfaceVersion = 2;

/**
Expand All @@ -60,15 +53,29 @@ exports.resolve = (source, file, options) => {
cwd: plugin[1] && plugin[1].cwd ? plugin[1].cwd : config.cwd,
root: config.root.concat(plugin[1] && plugin[1].root ? plugin[1].root : []),
alias: Object.assign(config.alias, plugin[1] ? plugin[1].alias : {}),
extensions: plugin[1] && plugin[1].extensions ? plugin[1].extensions : config.extensions,
}), { root: [], alias: {}, cwd: projectRootDir });

const manipulatedOpts = babelModuleResolver.manipulatePluginOptions(pluginOpts);

const src = babelModuleResolver.mapModule(
source, file, manipulatedOpts, path.resolve(manipulatedOpts.cwd),
);

const extensions = options.extensions ||
manipulatedOpts.extensions ||
babelModuleResolver.defaultExtensions;

return {
found: true,
path: resolve.sync(src || source, opts(file, options)),
path: resolve.sync(
src || source,
{
...options,
extensions,
basedir: path.dirname(file),
},
),
};
} catch (e) {
return { found: false };
Expand Down
3 changes: 2 additions & 1 deletion test/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"alias": {
"components": "./test/examples/components",
"underscore": "npm:lodash"
}
},
"extensions": [".js", ".customExt"]
}]
],
"env": {
Expand Down
Empty file.
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});

it('should return `true` when the file uses a custom extension from the babel plugin', () => {
expect(resolverPlugin.resolve('components/customFile', path.resolve('./test/examples/components/c1.js'), opts))
.toEqual({
found: true,
path: path.resolve(__dirname, './examples/components/customFile.customExt'),
});
});

it('should return `true` when no mapping is required', () => {
expect(resolverPlugin.resolve('./sub/sub/c2', path.resolve('./test/examples/components/c1'), opts))
.toEqual({
Expand Down

0 comments on commit fa885e8

Please sign in to comment.