Skip to content

Commit

Permalink
fix: Allow plugins to initalize themselves on unused hooks (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni authored Jun 21, 2024
1 parent 25c72f6 commit 676438b
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cool-islands-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-integration-kit": patch
---

Fixes initialization of plugins when necessary hooks are not used by consumer integrations
7 changes: 6 additions & 1 deletion package/src/core/with-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export const withPlugins = <
> => plugin.setup({ name }),
);

const definedHooks = Object.keys(providedHooks) as Array<keyof Hooks>;
const definedHooks = ([
...Object.keys(providedHooks),
...resolvedPlugins.flatMap(Object.keys),
] as Array<keyof Hooks>)
// Deduplicate the hook names
.filter((hookName, index, list) => list.indexOf(hookName) === index);

const hooks: AstroIntegration["hooks"] = Object.fromEntries(
definedHooks.map((hookName) => [
Expand Down
185 changes: 185 additions & 0 deletions package/tests/unit/with-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { describe, expect, it } from "vitest";
import { definePlugin } from "../../src/core/define-plugin.js";
import { withPlugins } from "../../src/core/with-plugins.js";
import type { AstroIntegrationLogger } from "astro";

describe('withPlugins', () => {
const fooPlugin = definePlugin({
name: 'foo',
setup({ name }) {
let innerState: string = 'initial state';

return {
'astro:build:start': ({ logger }) => {
logger.info(`Called from plugin "foo" on integration "${name}".`);

return {
foo: (msg: string) => {
logger.info(`Calling "foo" with msg: ${msg}`);
},
setState: (state: string) => {
innerState = state;
},
};
},
'astro:server:done': ({ logger }) => ({
getState: () => {
logger.info('Reading state');
return innerState;
},
}),
};
},
});

const otherFooPlugin = definePlugin({
name: 'foo',
setup({ name }) {
return {
'astro:build:start': ({ logger }) => {
logger.info(`Called from plugin "otherFoo" on integration "${name}".`);

return {
foo: (msg: string) => {
logger.info(`Calling "foo" (from otherFoo) with msg: ${msg}`);
}
};
},
};
},
});

const barPlugin = definePlugin({
name: 'bar',
setup({ name }) {
return {
'astro:build:start': ({ logger }) => {
logger.info(`Called from plugin "bar" on integration "${name}".`);

return {
foo: (msg: string) => {
logger.info(`Calling "foo" (from bar) with msg: ${msg}`);
}
};
},
};
},
});

it('should provide the plugins API to the hooks', () => {
const integration = withPlugins({
name: 'my-integration',
plugins: [fooPlugin],
hooks: {
'astro:build:start': ({ foo, setState }) => {
foo('from integration');
setState('integrationState')
},
'astro:server:done': ({ getState }) => {
expect(getState()).toEqual('integrationState');
},
},
});

const logger = new MemoryLogger();

integration.hooks['astro:build:start']?.({ logger });
integration.hooks['astro:server:done']?.({ logger });

expect(logger.log).toStrictEqual([
'Called from plugin "foo" on integration "my-integration".',
'Calling "foo" with msg: from integration',
'Reading state',
]);
});

it('should override plugins with the same name', () => {
const integration = withPlugins({
name: 'my-integration',
plugins: [fooPlugin, otherFooPlugin],
hooks: {
'astro:build:start': ({ foo }) => {
foo('from integration');
}
},
});

const logger = new MemoryLogger();

integration.hooks['astro:build:start']?.({ logger });

expect(logger.log).toStrictEqual([
'Called from plugin "otherFoo" on integration "my-integration".',
'Calling "foo" (from otherFoo) with msg: from integration',
]);
});

it('should override plugin APIs with the same name', () => {
const integration = withPlugins({
name: 'my-integration',
plugins: [fooPlugin, barPlugin],
hooks: {
'astro:build:start': ({ foo }) => {
foo('from integration');
}
},
});

const logger = new MemoryLogger();

integration.hooks['astro:build:start']?.({ logger });

expect(logger.log).toStrictEqual([
'Called from plugin "foo" on integration "my-integration".',
'Called from plugin "bar" on integration "my-integration".',
'Calling "foo" (from bar) with msg: from integration',
]);
});

it('should run plugin hooks that are not part of the integration', () => {
const integration = withPlugins({
name: 'my-integration',
plugins: [fooPlugin],
hooks: {
'astro:server:done': ({ getState }) => {
expect(getState()).toEqual('initial state');
},
},
});

const logger = new MemoryLogger();

integration.hooks['astro:build:start']?.({ logger });
integration.hooks['astro:server:done']?.({ logger });

expect(logger.log).toStrictEqual([
'Called from plugin "foo" on integration "my-integration".',
'Reading state'
]);
});
});

class MemoryLogger implements AstroIntegrationLogger {
log: string[] = [];

options = {} as any;
label: string = '';

fork(): AstroIntegrationLogger {
return this;
}

info(message: string): void {
this.log.push(message);
}
warn(message: string): void {
this.log.push(message);
}
error(message: string): void {
this.log.push(message);
}
debug(message: string): void {
this.log.push(message);
}
}

0 comments on commit 676438b

Please sign in to comment.