From b064c9ff8619fa69f5c2a439470337a86ac0f455 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 27 Sep 2025 20:17:10 -0500 Subject: [PATCH] Mock "sonner" and "next-themes" when building --- src/lib/build/index.ts | 2 + src/lib/build/shakespearePlugin.ts | 131 +++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/lib/build/shakespearePlugin.ts diff --git a/src/lib/build/index.ts b/src/lib/build/index.ts index fc9dbd1c..956aa6a7 100644 --- a/src/lib/build/index.ts +++ b/src/lib/build/index.ts @@ -1,6 +1,7 @@ import { getEsbuild } from "@/lib/esbuild"; import { copyFiles } from "@/lib/copyFiles"; +import { shakespearePlugin } from "./shakespearePlugin"; import { esmPlugin } from "./esmPlugin"; import { fsPlugin } from "./fsPlugin"; @@ -49,6 +50,7 @@ async function bundle( outdir: "/", jsx: "automatic", plugins: [ + shakespearePlugin(), // Takes precedence over esmPlugin fsPlugin(fs, `${projectPath}/src`), esmPlugin(JSON.parse(packageLockText), target), ], diff --git a/src/lib/build/shakespearePlugin.ts b/src/lib/build/shakespearePlugin.ts new file mode 100644 index 00000000..06841907 --- /dev/null +++ b/src/lib/build/shakespearePlugin.ts @@ -0,0 +1,131 @@ +import type { Plugin } from "esbuild-wasm"; + +/** + * Shakespeare plugin for dependency polyfills and workarounds. + * This plugin handles specific dependencies that need custom implementations + * or dummy APIs to work correctly in the browser environment. + */ +export function shakespearePlugin(): Plugin { + return { + name: "shakespeare", + + setup(build) { + // Handle "sonner" imports with a dummy API + build.onResolve({ filter: /^sonner$/ }, (args) => { + return { + path: args.path, + namespace: "shakespeare-sonner", + }; + }); + + // Provide dummy sonner implementation + build.onLoad({ filter: /.*/, namespace: "shakespeare-sonner" }, () => { + return { + contents: ` + // Dummy implementation of sonner toast library + + // Toast function that does nothing + const toast = (message, options) => { + console.log('[Shakespeare Sonner Mock]:', message, options); + return { id: Math.random().toString(36) }; + }; + + // Add common toast methods + toast.success = (message, options) => { + console.log('[Shakespeare Sonner Mock] Success:', message, options); + return { id: Math.random().toString(36) }; + }; + + toast.error = (message, options) => { + console.log('[Shakespeare Sonner Mock] Error:', message, options); + return { id: Math.random().toString(36) }; + }; + + toast.info = (message, options) => { + console.log('[Shakespeare Sonner Mock] Info:', message, options); + return { id: Math.random().toString(36) }; + }; + + toast.warning = (message, options) => { + console.log('[Shakespeare Sonner Mock] Warning:', message, options); + return { id: Math.random().toString(36) }; + }; + + toast.loading = (message, options) => { + console.log('[Shakespeare Sonner Mock] Loading:', message, options); + return { id: Math.random().toString(36) }; + }; + + toast.promise = (promise, options) => { + console.log('[Shakespeare Sonner Mock] Promise:', promise, options); + return promise; + }; + + toast.dismiss = (id) => { + console.log('[Shakespeare Sonner Mock] Dismiss:', id); + }; + + // Toaster component (dummy React component) + const Toaster = (props) => { + console.log('[Shakespeare Sonner Mock] Toaster component rendered:', props); + return null; + }; + + // Export both named and default exports to match sonner API + export { toast, Toaster }; + export default toast; + `, + loader: "js", + }; + }); + + // Handle "next-themes" imports with a dummy API + build.onResolve({ filter: /^next-themes$/ }, (args) => { + return { + path: args.path, + namespace: "shakespeare-next-themes", + }; + }); + + // Provide dummy next-themes implementation + build.onLoad({ filter: /.*/, namespace: "shakespeare-next-themes" }, () => { + return { + contents: ` + // Dummy implementation of next-themes + + // useTheme hook that returns system theme + const useTheme = () => { + return { theme: "system" }; + }; + + // ThemeProvider component (dummy React component) + const ThemeProvider = ({ children, ...props }) => { + console.log('[Shakespeare next-themes Mock] ThemeProvider rendered:', props); + return children; + }; + + // Export both named and default exports to match next-themes API + export { useTheme, ThemeProvider }; + `, + loader: "js", + }; + }); + + // Add more dependency polyfills here as needed + // Example structure for future dependencies: + // build.onResolve({ filter: /^some-other-package$/ }, (args) => { + // return { + // path: args.path, + // namespace: "shakespeare-other-package", + // }; + // }); + // + // build.onLoad({ filter: /.*/, namespace: "shakespeare-other-package" }, () => { + // return { + // contents: `// Custom implementation for some-other-package`, + // loader: "js", + // }; + // }); + }, + }; +} \ No newline at end of file