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(modular-station): fix global hooks state on astro dev #127

Merged
merged 1 commit into from
Jul 10, 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
5 changes: 5 additions & 0 deletions .changeset/green-cheetahs-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@inox-tools/modular-station': patch
---

Fixes global hooks state not being properly set during `astro dev`
10 changes: 9 additions & 1 deletion packages/content-utils/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ export default defineConfig({
clean: true,
splitting: true,
minify: false,
external: ['astro', 'astro-integration-kit', './virtual.d.ts', 'vite', /^@it-astro:/, /^astro:/],
external: [
'astro',
'astro-integration-kit',
'./virtual.d.ts',
'vite',
/^@inox-tools\//,
/^@it-astro:/,
/^astro:/,
],
noExternal: [],
treeshake: 'smallest',
tsconfig: 'tsconfig.json',
Expand Down
16 changes: 12 additions & 4 deletions packages/modular-station/src/globalHooks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import type { AstroIntegration, AstroIntegrationLogger } from 'astro';
import { runHook, type PluginApi } from './hooks.js';

let logger: AstroIntegrationLogger;
let integrations: AstroIntegration[];
type Reference = {
logger?: AstroIntegrationLogger;
integrations?: AstroIntegration[];
};

const referenceState: Reference = ((globalThis as any)[
Symbol.for('@inox-tools/modular-station:globalHooks')
] ??= {});

export const setGlobal = (
newLogger: AstroIntegrationLogger,
newIntegrations: AstroIntegration[]
) => {
logger = newLogger;
integrations = newIntegrations;
referenceState.logger = newLogger;
referenceState.integrations = newIntegrations;
};

export const hooks: PluginApi['hooks'] = {
run: (hook, params) => {
const { logger, integrations } = referenceState;
if (logger === undefined || integrations === undefined || integrations.length === 0) {
return Promise.reject(new Error('Cannot run hook at this point'));
}
return runHook(integrations, logger, hook, params);
},
getTrigger: (hook) => (params) => {
const { logger, integrations } = referenceState;
if (logger === undefined || integrations === undefined || integrations.length === 0) {
return Promise.resolve();
}
Expand Down
1 change: 0 additions & 1 deletion packages/modular-station/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export const hookProviderPlugin = allHooksPlugin({
},
});

const pregisterOnce = new Once();
const globalHookIntegrationName = '@inox-tools/modular-station/global-hooks';
const versionMarker = Symbol(globalHookIntegrationName);

Expand Down
2 changes: 1 addition & 1 deletion packages/modular-station/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default defineConfig({
clean: true,
splitting: false,
minify: false,
external: ['astro', 'astro-integration-kit', 'vite'],
external: ['astro', 'astro-integration-kit', 'vite', /^@inox-tools\//],
noExternal: [],
treeshake: 'smallest',
tsconfig: 'tsconfig.json',
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-logger/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineConfig({
clean: true,
splitting: true,
minify: false,
external: ['astro', 'astro-integration-kit', './virtual.d.ts', 'vite', '@inox-tools/utils'],
external: ['astro', 'astro-integration-kit', './virtual.d.ts', 'vite', /^@inox-tools\//],
noExternal: [],
treeshake: 'smallest',
tsconfig: 'tsconfig.json',
Expand Down
Loading