Skip to content

Commit e9a567c

Browse files
committed
Add a warning for when double application is possible
1 parent 0d991e3 commit e9a567c

5 files changed

Lines changed: 80 additions & 6 deletions

File tree

.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/getRealPath.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ const resolvers = [
7878
getRealPathFromAliasConfig,
7979
];
8080

81+
function getResolvedPath(sourcePath, currentFile, opts) {
82+
let resolvedPath = null;
83+
84+
resolvers.some((resolver) => {
85+
resolvedPath = resolver(sourcePath, currentFile, opts);
86+
return resolvedPath !== null;
87+
});
88+
89+
return resolvedPath;
90+
}
91+
92+
function checkIfDoubleApplicationPossible(sourcePath, resolvedPath, currentFile, opts) {
93+
if (resolvedPath !== null) {
94+
const resolvedAgainPath = getResolvedPath(resolvedPath, currentFile, opts);
95+
if (resolvedAgainPath !== null && resolvedAgainPath !== resolvedPath) {
96+
warn(`Resolving "${sourcePath}" may give different results if done multiple times. Remove cycles from the configuration or alias to absolute paths.`);
97+
}
98+
}
99+
}
100+
81101
export default function getRealPath(sourcePath, { file, opts }) {
82102
if (sourcePath[0] === '.') {
83103
return sourcePath;
@@ -86,12 +106,11 @@ export default function getRealPath(sourcePath, { file, opts }) {
86106
// File param is a relative path from the environment current working directory
87107
// (not from cwd param)
88108
const currentFile = path.resolve(file.opts.filename);
89-
let resolvedPath = null;
109+
const resolvedPath = getResolvedPath(sourcePath, currentFile, opts);
90110

91-
resolvers.some((resolver) => {
92-
resolvedPath = resolver(sourcePath, currentFile, opts);
93-
return resolvedPath !== null;
94-
});
111+
if (process.env.NODE_ENV !== 'production') {
112+
checkIfDoubleApplicationPossible(sourcePath, resolvedPath, currentFile, opts);
113+
}
95114

96115
return resolvedPath;
97116
}

test/index.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,61 @@ describe('module-resolver', () => {
509509
});
510510
});
511511
});
512+
513+
describe('multiple alias application warning', () => {
514+
const mockWarn = jest.fn();
515+
jest.mock('../src/log', () => ({
516+
warn: mockWarn,
517+
}));
518+
jest.resetModules();
519+
const pluginWithMock = require.requireActual('../src').default;
520+
const fileName = path.resolve('test/testproject/src/app.js');
521+
522+
const cycleAliasTransformerOpts = {
523+
babelrc: false,
524+
plugins: [
525+
[pluginWithMock, {
526+
alias: {
527+
first: 'second',
528+
second: 'first',
529+
},
530+
}],
531+
],
532+
filename: fileName,
533+
};
534+
535+
beforeEach(() => {
536+
mockWarn.mockClear();
537+
process.env.NODE_ENV = 'development';
538+
});
539+
540+
it('should print a warning for a package that may be resolved multiple times', () => {
541+
testWithImport(
542+
'first',
543+
'second',
544+
cycleAliasTransformerOpts,
545+
);
546+
547+
expect(mockWarn.mock.calls.length).toBe(1);
548+
expect(mockWarn).toBeCalledWith('Resolving "first" may give different results if done multiple times. Remove cycles from the configuration or alias to absolute paths.');
549+
});
550+
551+
describe('production environment', () => {
552+
beforeEach(() => {
553+
process.env.NODE_ENV = 'production';
554+
});
555+
556+
it('should not print a warning for a package that may be resolved multiple times', () => {
557+
testWithImport(
558+
'first',
559+
'second',
560+
cycleAliasTransformerOpts,
561+
);
562+
563+
expect(mockWarn.mock.calls.length).toBe(0);
564+
});
565+
});
566+
});
512567
});
513568

514569
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)