Skip to content

Commit

Permalink
fix: Fix resolver with filename containing a dot (#75)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tleunen committed Aug 22, 2016
1 parent fc05c9f commit bb6c903
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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...
}
Expand Down
Empty file.
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit bb6c903

Please sign in to comment.