Skip to content

Commit feefe9e

Browse files
fatfisztleunen
authored andcommitted
fix: Prevent double application of the transform (#176)
1 parent e25fc96 commit feefe9e

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.DS_Store
2-
node_modules/
2+
/node_modules/
33
npm-debug.log
44
lib/
55
coverage/

src/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ export function mapPathString(nodePath, state) {
5050

5151
const modulePath = getRealPath(nodePath.node.value, state);
5252
if (modulePath) {
53+
if (nodePath.node.pathResolved) {
54+
return;
55+
}
56+
5357
nodePath.replaceWith(state.types.stringLiteral(modulePath));
58+
nodePath.node.pathResolved = true;
5459
}
5560
}
5661

test/index.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,62 @@ describe('module-resolver', () => {
509509
});
510510
});
511511
});
512+
513+
describe('multiple alias application', () => {
514+
it('should resolve the cyclic alias only once', () => {
515+
const fileName = path.resolve('test/testproject/src/app.js');
516+
const cycleAliasTransformerOpts = {
517+
babelrc: false,
518+
plugins: [
519+
[plugin, {
520+
alias: {
521+
first: 'second',
522+
second: 'first',
523+
},
524+
}],
525+
[plugin, {
526+
alias: {
527+
first: 'second',
528+
second: 'first',
529+
},
530+
}],
531+
],
532+
filename: fileName,
533+
};
534+
535+
testWithImport(
536+
'first',
537+
'second',
538+
cycleAliasTransformerOpts,
539+
);
540+
});
541+
542+
it('should resolve the prefix alias only once', () => {
543+
const fileName = path.resolve('test/testproject/src/app.js');
544+
const cycleAliasTransformerOpts = {
545+
babelrc: false,
546+
plugins: [
547+
[plugin, {
548+
alias: {
549+
prefix: 'prefix/lib',
550+
},
551+
}],
552+
[plugin, {
553+
alias: {
554+
prefix: 'prefix/lib',
555+
},
556+
}],
557+
],
558+
filename: fileName,
559+
};
560+
561+
testWithImport(
562+
'prefix/test',
563+
'prefix/lib/test',
564+
cycleAliasTransformerOpts,
565+
);
566+
});
567+
});
512568
});
513569

514570
describe('with custom cwd', () => {

test/testproject/node_modules/first/index.js

Whitespace-only changes.

test/testproject/node_modules/second/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)