Skip to content

fix(astro): resolve Astro's private APIs properly #238

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

Closed
wants to merge 9 commits into from
Closed
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
2 changes: 2 additions & 0 deletions packages/astro/src/default/env-default.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ declare module 'tutorialkit:override-components' {
export { topBar as TopBar };
}

declare module 'tutorialkit:astro-swap-functions';

declare const __ENTERPRISE__: boolean;
declare const __WC_CONFIG__: WebContainerConfig | undefined;
2 changes: 1 addition & 1 deletion packages/astro/src/default/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const baseURL = import.meta.env.BASE_URL;
}
</script>
<script>
import * as builtInSwap from 'node_modules/astro/dist/transitions/swap-functions';
import * as builtInSwap from 'tutorialkit:astro-swap-functions';

declare global {
function setTutorialKitTheme(): void;
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fileURLToPath } from 'node:url';
import { extraIntegrations } from './integrations.js';
import { updateMarkdownConfig } from './remark/index.js';
import { tutorialkitCore } from './vite-plugins/core.js';
import { astroSwapFunctions } from './vite-plugins/astro-swap-functions.js';
import { userlandCSS, watchUserlandCSS } from './vite-plugins/css.js';
import { tutorialkitStore } from './vite-plugins/store.js';
import { overrideComponents, type OverrideComponentsOptions } from './vite-plugins/override-components.js';
Expand Down Expand Up @@ -108,6 +109,7 @@ export default function createPlugin({
tutorialkitStore,
tutorialkitCore,
overrideComponents({ components, defaultRoutes: !!defaultRoutes }),
astroSwapFunctions(),
process.env.TUTORIALKIT_DEV ? (await import('vite-plugin-inspect')).default() : null,
],
},
Expand Down
35 changes: 35 additions & 0 deletions packages/astro/src/vite-plugins/astro-swap-functions.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* A plugin for importing Astro's private 'transitions/swap-functions.js' module.
* This is temporary solution and can break at any time when end-user's update their `astro` version.
*/
import { createRequire } from 'node:module';
import { resolve, sep } from 'node:path';
import type { VitePlugin } from '../types.js';

const require = createRequire(import.meta.url);
const astroDist = resolve(require.resolve('astro/package.json'), '..');
const swapFunctionEntry = resolve(astroDist, 'dist/transitions/swap-functions.js').replaceAll(sep, '/');

const virtualModuleId = 'tutorialkit:astro-swap-functions';
const resolvedId = `\0${virtualModuleId}`;

export function astroSwapFunctions(): VitePlugin {
return {
name: 'tutorialkit-astro-swap-functions-plugin',
resolveId(id) {
if (id === virtualModuleId) {
return resolvedId;
}

return undefined;
},

async load(id) {
if (id === resolvedId) {
return `export * from '${swapFunctionEntry}';`;
}

return undefined;
},
};
}