Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle module aliases that start with "." correctly #1196

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"browser": {
"./index": "index-shim.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "index",
"browser": "index-shim.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,21 @@ describe('loadAliases', () => {

expect(aliases['toString']).toBeUndefined();
});

it('works when the main field does not start with .', () => {
const aliases = loadAliases(fixturesDir.join('main-without-dot.json'), [
'browser',
]);

expect(aliases['./index']).toEqual('./index-shim.js');
});

it('works when value does not start with .', () => {
const aliases = loadAliases(
fixturesDir.join('alias-without-dot.json'),
['browser']
);

expect(aliases['./index']).toEqual('./index-shim.js');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ export function loadAliases(
}

if (aliasConfig === false || typeof aliasConfig === 'string') {
const main = pkgJson['main'] || './index.js';
let main = pkgJson['main'] || './index.js';

if (!main.startsWith('.')) {
main = `./${main}`;
}

aliases[main] = aliasConfig;
}
Expand All @@ -140,6 +144,18 @@ export function loadAliases(
delete aliases[key];
}
});

// Normalize local aliases to make them start with `./`

Object.keys(aliases).forEach((key) => {
if (key.startsWith('.')) {
const value = aliases[key];

if (typeof value === 'string' && !value.startsWith('.')) {
aliases[key] = `./${value}`;
}
}
});
}

// Store in cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,26 @@ function processAliases(
const absToFile = alias.absDir.join(
new FilePath(alias.to as string, {posix: true})
);
const fromRelToFile = absFromFile
.dirname()
.relative(absToFile);

rewriteFile(
absFromFile,
`/* redirected by alias field(s) in ${rootRelAbsDirPosixPath} */`,
`module.exports = require('./${fromRelToFile.asPosix}');`
);
if (!absToFile.is(absFromFile)) {
const fromRelToFile = absFromFile
.dirname()
.relative(absToFile);

rewriteFile(
absFromFile,
`/* redirected by alias field(s) in ${rootRelAbsDirPosixPath} */`,
`module.exports = require('./${fromRelToFile.asPosix}');`
);

log.info(
'replace-browser-modules',
`Redirected file '${rootRelFilePosixPath}' to ` +
`'./${fromRelToFile.asPosix}' as configured in ` +
`'${rootRelAbsDirPosixPath}'`
).linkToCode(2);
}

log.info(
'replace-browser-modules',
`Redirected file '${rootRelFilePosixPath}' to ` +
`'./${fromRelToFile.asPosix}' as configured in ` +
`'${rootRelAbsDirPosixPath}'`
).linkToCode(2);
break;
}

Expand Down
Loading