Skip to content

Commit bb6c903

Browse files
authored
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
1 parent fc05c9f commit bb6c903

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ export function mapModule(source, file, pluginOpts) {
3131
// check if the file exists (will throw if not)
3232
const extensions = pluginOpts.extensions || defaultBabelExtensions;
3333
const fileAbsPath = resolve.sync(`./${source}`, { basedir: path.resolve(rootDirs[i]), extensions });
34+
const realFileExt = path.extname(fileAbsPath);
35+
const sourceFileExt = path.extname(source);
3436
// map the source and keep its extension if the import/require had one
35-
return mapToRelative(file, replaceExt(fileAbsPath, path.extname(source)));
37+
const ext = realFileExt === sourceFileExt ? realFileExt : '';
38+
return mapToRelative(file, replaceExt(fileAbsPath, ext));
3639
} catch (e) {
3740
// empty...
3841
}

test/examples/components/sub/custom.modernizr3.js

Whitespace-only changes.

test/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ describe('root', () => {
5252
);
5353
});
5454

55+
describe('should rewrite the file with a filename containing a dot', () => {
56+
testRequireImport(
57+
'sub/custom.modernizr3',
58+
'./test/examples/components/sub/custom.modernizr3',
59+
transformerOpts
60+
);
61+
});
62+
5563
describe('should not rewrite a path outisde of the root directory', () => {
5664
testRequireImport(
5765
'example-file',
@@ -111,6 +119,14 @@ describe('alias', () => {
111119
);
112120
});
113121
});
122+
123+
describe('with a dot in the filename', () => {
124+
testRequireImport(
125+
'utils/custom.modernizr3',
126+
'./src/mylib/subfolder/utils/custom.modernizr3',
127+
transformerOpts
128+
);
129+
});
114130
});
115131

116132
describe('should alias the path with its extension', () => {

0 commit comments

Comments
 (0)