Skip to content

Commit 3ec42bd

Browse files
committed
Add a warning for when double application is possible
1 parent ea9e457 commit 3ec42bd

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
@@ -490,6 +490,61 @@ describe('module-resolver', () => {
490490
});
491491
});
492492
});
493+
494+
describe('multiple alias application warning', () => {
495+
const mockWarn = jest.fn();
496+
jest.mock('../src/log', () => ({
497+
warn: mockWarn,
498+
}));
499+
jest.resetModules();
500+
const pluginWithMock = require.requireActual('../src').default;
501+
const fileName = path.resolve('test/testproject/src/app.js');
502+
503+
const cycleAliasTransformerOpts = {
504+
babelrc: false,
505+
plugins: [
506+
[pluginWithMock, {
507+
alias: {
508+
first: 'second',
509+
second: 'first',
510+
},
511+
}],
512+
],
513+
filename: fileName,
514+
};
515+
516+
beforeEach(() => {
517+
mockWarn.mockClear();
518+
process.env.NODE_ENV = 'development';
519+
});
520+
521+
it('should print a warning for a package that may be resolved multiple times', () => {
522+
testWithImport(
523+
'first',
524+
'second',
525+
cycleAliasTransformerOpts,
526+
);
527+
528+
expect(mockWarn.mock.calls.length).toBe(1);
529+
expect(mockWarn).toBeCalledWith('Resolving "first" may give different results if done multiple times. Remove cycles from the configuration or alias to absolute paths.');
530+
});
531+
532+
describe('production environment', () => {
533+
beforeEach(() => {
534+
process.env.NODE_ENV = 'production';
535+
});
536+
537+
it('should not print a warning for a package that may be resolved multiple times', () => {
538+
testWithImport(
539+
'first',
540+
'second',
541+
cycleAliasTransformerOpts,
542+
);
543+
544+
expect(mockWarn.mock.calls.length).toBe(0);
545+
});
546+
});
547+
});
493548
});
494549

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