From 8deef7b1d6d2dc6d08f829d0c1cdab511a38f18c Mon Sep 17 00:00:00 2001 From: Nikhil Saraf Date: Fri, 18 Aug 2023 23:43:05 +0530 Subject: [PATCH] fix vinxi solid --- packages/vinxi-solid/invariant.js | 33 +++++++++++++++++++++++++++++ packages/vinxi-solid/lazy-route.jsx | 26 ++++++++++++++++++----- packages/vinxi-solid/package.json | 4 +++- 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 packages/vinxi-solid/invariant.js diff --git a/packages/vinxi-solid/invariant.js b/packages/vinxi-solid/invariant.js new file mode 100644 index 00000000..e24c1630 --- /dev/null +++ b/packages/vinxi-solid/invariant.js @@ -0,0 +1,33 @@ +const genericMessage = "Invariant Violation"; +const { + setPrototypeOf = function (obj, proto) { + obj.__proto__ = proto; + return obj; + }, +} = Object; + +export class InvariantError extends Error { + framesToPop = 1; + name = genericMessage; + constructor(/** @type {string | number} */ message = genericMessage) { + super( + typeof message === "number" + ? `${genericMessage}: ${message} (see https://github.com/apollographql/invariant-packages)` + : message, + ); + setPrototypeOf(this, InvariantError.prototype); + } +} + +/** + * @param {any} condition + * @param {string | number} message + * @returns {asserts condition} + */ +export function invariant(condition, message) { + if (!condition) { + throw new InvariantError(message); + } +} + +export default invariant; diff --git a/packages/vinxi-solid/lazy-route.jsx b/packages/vinxi-solid/lazy-route.jsx index e5586386..6e173fce 100644 --- a/packages/vinxi-solid/lazy-route.jsx +++ b/packages/vinxi-solid/lazy-route.jsx @@ -4,17 +4,30 @@ import { createComponent, lazy, onCleanup } from "solid-js"; import { appendStyles, cleanupStyles, updateStyles } from "vinxi/lib/style"; +import invariant from "./invariant"; import { renderAsset } from "./render-asset"; -export default function lazyRoute(component, clientManifest, serverManifest) { +export default function lazyRoute( + component, + clientManifest, + serverManifest, + exported = "default", +) { return lazy(async () => { if (import.meta.env.DEV) { let manifest = import.meta.env.SSR ? serverManifest : clientManifest; - const { default: Component } = await import( + + const mod = await import( /* @vite-ignore */ manifest.inputs[component.src].output.path ); + invariant( + mod[exported], + `Module ${component.src} does not export ${exported}`, + ); + const Component = mod[exported]; let assets = await clientManifest.inputs?.[component.src].assets(); const styles = assets.filter((asset) => asset.tag === "style"); + if (typeof window !== "undefined" && import.meta.hot) { import.meta.hot.on("css-update", (data) => { updateStyles(styles, data); @@ -27,8 +40,10 @@ export default function lazyRoute(component, clientManifest, serverManifest) { } onCleanup(() => { - // remove style tags added by vite when a CSS file is imported - cleanupStyles(styles); + if (typeof window !== "undefined") { + // remove style tags added by vite when a CSS file is imported + cleanupStyles(styles); + } }); return [ ...assets.map((asset) => renderAsset(asset)), @@ -37,7 +52,8 @@ export default function lazyRoute(component, clientManifest, serverManifest) { }; return { default: Comp }; } else { - const { default: Component } = await component.import(); + const mod = await component.import(); + const Component = mod[exported]; let assets = await clientManifest.inputs?.[component.src].assets(); const Comp = (props) => { return [ diff --git a/packages/vinxi-solid/package.json b/packages/vinxi-solid/package.json index f8755011..86a1bd7e 100644 --- a/packages/vinxi-solid/package.json +++ b/packages/vinxi-solid/package.json @@ -1,10 +1,12 @@ { "name": "@vinxi/solid", - "version": "0.0.2", + "version": "0.0.7", "type": "module", "author": "Nikhil Saraf ", "files": [ "src", + "*.jsx", + "*.js", "*.d.ts" ], "types": "./index.d.ts",