From bb6c90352e765d69e813bef77f22e26c29902a8d Mon Sep 17 00:00:00 2001 From: Tommy Date: Mon, 22 Aug 2016 18:19:12 -0400 Subject: [PATCH] fix: Fix resolver with filename containing a dot (#75) Checking the extension of the source file and the real file allows us to verify it's a real extension of something from the filename. Fixes #74 --- src/index.js | 5 ++++- .../examples/components/sub/custom.modernizr3.js | 0 test/index.js | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/examples/components/sub/custom.modernizr3.js diff --git a/src/index.js b/src/index.js index c41bb35..6511b16 100644 --- a/src/index.js +++ b/src/index.js @@ -31,8 +31,11 @@ export function mapModule(source, file, pluginOpts) { // check if the file exists (will throw if not) const extensions = pluginOpts.extensions || defaultBabelExtensions; const fileAbsPath = resolve.sync(`./${source}`, { basedir: path.resolve(rootDirs[i]), extensions }); + const realFileExt = path.extname(fileAbsPath); + const sourceFileExt = path.extname(source); // map the source and keep its extension if the import/require had one - return mapToRelative(file, replaceExt(fileAbsPath, path.extname(source))); + const ext = realFileExt === sourceFileExt ? realFileExt : ''; + return mapToRelative(file, replaceExt(fileAbsPath, ext)); } catch (e) { // empty... } diff --git a/test/examples/components/sub/custom.modernizr3.js b/test/examples/components/sub/custom.modernizr3.js new file mode 100644 index 0000000..e69de29 diff --git a/test/index.js b/test/index.js index 5933a50..8fb0211 100644 --- a/test/index.js +++ b/test/index.js @@ -52,6 +52,14 @@ describe('root', () => { ); }); + describe('should rewrite the file with a filename containing a dot', () => { + testRequireImport( + 'sub/custom.modernizr3', + './test/examples/components/sub/custom.modernizr3', + transformerOpts + ); + }); + describe('should not rewrite a path outisde of the root directory', () => { testRequireImport( 'example-file', @@ -111,6 +119,14 @@ describe('alias', () => { ); }); }); + + describe('with a dot in the filename', () => { + testRequireImport( + 'utils/custom.modernizr3', + './src/mylib/subfolder/utils/custom.modernizr3', + transformerOpts + ); + }); }); describe('should alias the path with its extension', () => {