Skip to content

Commit

Permalink
fix(alias): pass on isEntry flag and custom options (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 19, 2021
1 parent f1f91d5 commit 5d30e6f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/alias/package.json
Expand Up @@ -54,7 +54,7 @@
"@rollup/plugin-node-resolve": "^8.4.0",
"@rollup/plugin-typescript": "^5.0.2",
"del-cli": "^3.0.1",
"rollup": "^2.23.0",
"rollup": "^2.58.0",
"typescript": "^4.1.2"
},
"types": "types/index.d.ts",
Expand Down
10 changes: 7 additions & 3 deletions packages/alias/src/index.ts
Expand Up @@ -83,7 +83,7 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {
// enforce void return value
});
},
resolveId(importee, importer) {
resolveId(importee, importer, resolveOptions) {
const importeeId = normalizeId(importee);
const importerId = normalizeId(importer);

Expand All @@ -99,10 +99,14 @@ export default function alias(options: RollupAliasOptions = {}): Plugin {

const customResolver = getCustomResolver(matchedEntry, options);
if (customResolver) {
return customResolver.call(this, updatedId, importerId, {});
return customResolver.call(this, updatedId, importerId, resolveOptions);
}

return this.resolve(updatedId, importer, { skipSelf: true }).then((resolved) => {
return this.resolve(
updatedId,
importer,
Object.assign({ skipSelf: true }, resolveOptions)
).then((resolved) => {
let finalResult: PartialResolvedId | null = resolved;
if (!finalResult) {
finalResult = { id: updatedId };
Expand Down
98 changes: 96 additions & 2 deletions packages/alias/test/test.js
Expand Up @@ -32,8 +32,10 @@ function resolveWithRollup(plugins, tests) {
// The buildStart hook is the first to have access to this.resolve
// We map the tests to an array of resulting ids
Promise.all(
tests.map(({ source, importer }) =>
this.resolve(source, importer).then((result) => (result ? result.id : null))
tests.map(({ source, importer, options }) =>
this.resolve(source, importer, options).then((result) =>
result ? result.id : null
)
)
)
);
Expand Down Expand Up @@ -437,3 +439,95 @@ test('Alias + rollup-plugin-node-resolve', (t) =>
)
);
}));

test('Forwards isEntry and custom options to a custom resolver', (t) => {
const resolverCalls = [];
return resolveAliasWithRollup(
{
entries: {
entry: 'entry-point',
nonEntry: 'non-entry-point'
},
customResolver: (...args) => {
resolverCalls.push(args);
return args[0];
}
},
[
{ source: 'entry', importer: '/src/importer.js', options: { isEntry: true } },
{
source: 'nonEntry',
importer: '/src/importer.js',
options: { isEntry: false, custom: { test: 42 } }
}
]
).then((result) => {
t.deepEqual(resolverCalls, [
[
'entry-point',
'/src/importer.js',
{
custom: void 0,
isEntry: true
}
],
[
'non-entry-point',
'/src/importer.js',
{
custom: { test: 42 },
isEntry: false
}
]
]);
t.deepEqual(result, ['entry-point', 'non-entry-point']);
});
});

test('Forwards isEntry and custom options to other plugins', (t) => {
const resolverCalls = [];
return resolveWithRollup(
[
alias({
entries: {
entry: 'entry-point',
nonEntry: 'non-entry-point'
}
}),
{
name: 'test',
resolveId(...args) {
resolverCalls.push(args);
}
}
],
[
{ source: 'entry', importer: '/src/importer.js', options: { isEntry: true } },
{
source: 'nonEntry',
importer: '/src/importer.js',
options: { isEntry: false, custom: { test: 42 } }
}
]
).then((result) => {
t.deepEqual(resolverCalls, [
[
'entry-point',
'/src/importer.js',
{
custom: void 0,
isEntry: true
}
],
[
'non-entry-point',
'/src/importer.js',
{
custom: { test: 42 },
isEntry: false
}
]
]);
t.deepEqual(result, ['entry-point', 'non-entry-point']);
});
});
58 changes: 54 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d30e6f

Please sign in to comment.