Skip to content

Commit

Permalink
fix vinxi solid
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Aug 18, 2023
1 parent f1f8f82 commit 8deef7b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
33 changes: 33 additions & 0 deletions packages/vinxi-solid/invariant.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 21 additions & 5 deletions packages/vinxi-solid/lazy-route.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)),
Expand All @@ -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 [
Expand Down
4 changes: 3 additions & 1 deletion packages/vinxi-solid/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"name": "@vinxi/solid",
"version": "0.0.2",
"version": "0.0.7",
"type": "module",
"author": "Nikhil Saraf <nsaraf98@gmail.com>",
"files": [
"src",
"*.jsx",
"*.js",
"*.d.ts"
],
"types": "./index.d.ts",
Expand Down

0 comments on commit 8deef7b

Please sign in to comment.