Skip to content

Commit

Permalink
feat(aik-mod): Add debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryuni committed Aug 24, 2024
1 parent a6228a5 commit 36180db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/new-ties-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@inox-tools/aik-mod': patch
---

Add debug logging
4 changes: 3 additions & 1 deletion packages/aik-mod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
"test": "echo 'No tests'"
},
"dependencies": {
"@inox-tools/inline-mod": "workspace:^"
"@inox-tools/inline-mod": "workspace:^",
"debug": "catalog:"
},
"devDependencies": {
"@types/debug": "catalog:",
"@types/node": "catalog:",
"astro": "catalog:",
"tsup": "catalog:",
Expand Down
30 changes: 22 additions & 8 deletions packages/aik-mod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type { HookParameters, MiddlewareHandler } from 'astro';
import { definePlugin } from 'astro-integration-kit';
import { AstroError } from 'astro/errors';
import type { PluginOption } from 'vite';
import debugC from 'debug';

const debug = debugC('inox-tools:aik-mod');

export {
asyncFactory,
Expand Down Expand Up @@ -40,13 +43,18 @@ function ensurePluginIsInstalled(
): () => void {
const { config, updateConfig } = options;
if (hasPlugin(config.vite?.plugins || [], '@inox-tools/inline-mod')) {
return () => {};
debug('Plugin is already installed, using no-op installation function.');
return () => { };
}

let done = false;

return () => {
if (done) return;
if (done) {
debug('Reusing installed plugin');
return;
}
debug('Installing inline-mod plugin');
done = true;
updateConfig({
vite: {
Expand All @@ -68,7 +76,10 @@ export default definePlugin({
return {
inlineModule: (options: ModuleOptions) => {
ensurePlugin();
return inlineModule(options);
debug('Inlining module');
const moduleId = inlineModule(options);
debug(`Module inlined as ${moduleId}`);
return moduleId;
},
defineModule: (name: string, options: ModuleOptions) => {
if (name.startsWith('astro:')) {
Expand All @@ -79,17 +90,20 @@ export default definePlugin({
}

ensurePlugin();
debug(`Defining module: ${name}`);
return defineModule(name, options);
},
defineMiddleware: (order: 'pre' | 'post', handler: MiddlewareHandler) => {
ensurePlugin();
const moduleId = inlineModule({
constExports: {
onRequest: handler,
},
});
debug(`Defining ${order} middleware as ${moduleId}`);
addMiddleware({
order,
entrypoint: inlineModule({
constExports: {
onRequest: handler,
},
}),
entrypoint: moduleId,
});
},
};
Expand Down
6 changes: 6 additions & 0 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 36180db

Please sign in to comment.