@d1vij/jassm 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,15 @@
1
1
  # Just another static site maker
2
2
 
3
- Create content driven sites using mdx (markdown + react) with added support of route safety.
3
+ Create content driven sites using mdx (markdown + react) with added support typescript based autocompletion for the pages.
4
4
 
5
5
  ## What is this ??
6
6
 
7
7
  JASSM is a simple abstraction layer over [mdx-js](https://mdxjs.com/) and its vite plugin for creating a route/file aware loader for mdx file.
8
8
 
9
+ Each page is configured at a single source of truth (registry) and thereby provides typesafe access throughout.
10
+
11
+ (ps the DX is similar to that of TanstackRouter or Elysia)
12
+
9
13
  ## Usage
10
14
 
11
15
  0. Initialize a react app using vite
@@ -26,7 +30,7 @@ bun add @d1vij/jassm
26
30
  import { defineConfig } from "vite";
27
31
  import react from "@vitejs/plugin-react";
28
32
 
29
- import jassm from "jassm/plugin";
33
+ import jassm from "@d1vij/jassm/plugin";
30
34
 
31
35
  export default defineConfig({
32
36
  plugins: [
@@ -49,9 +53,9 @@ echo "# This is a Heading" > sample.mdx
49
53
  ```ts
50
54
  // src/Registry.tsx
51
55
 
52
- import { generateRegistry } from "jassm";
56
+ import { Registry } from "@d1vij/jassm";
53
57
 
54
- export const registry = generateRegistry({
58
+ export const registry = new Registry({
55
59
  modules: import.meta.glob("/src/assets/mdx/**/*.mdx"),
56
60
  source: "/src/assets/mdx",
57
61
  mountOn: "/root",
@@ -100,10 +104,20 @@ import {stylesmap} from "./stylesmap";
100
104
 
101
105
  import {MDXFromComponent} from "jassm";
102
106
 
107
+ import {use} from "react";
108
+
109
+ type ExportType = {
110
+ meta: {
111
+ title: string
112
+ }
113
+ };
114
+
103
115
  export default function Content() {
104
- const Component = registry["/root/sample"];
116
+ const Component = registry.getComponent("/root/sample");
117
+ const exports = use(registry.getExport<ExportType>("/root/sample")) // consuming the 'import' promise using use
105
118
  return (
106
119
  <div>
120
+ {exports.meta.title}
107
121
  <MDXFromComponent
108
122
  SourceComponent={Component}
109
123
  styles={stylesmap}
@@ -121,7 +135,7 @@ Using `MDXSourceComponent` automatically sets up the required enclosing StyleCon
121
135
  The setup can also be done manually as follows
122
136
 
123
137
  ```tsx
124
- import { StyleContext, Elements } from "jassm";
138
+ import { StyleContext, Elements } from "@d1vij/jassm";
125
139
 
126
140
  import { registry } from "./Registry";
127
141
  import { stylesmap } from "./stylesmap";
@@ -142,3 +156,4 @@ export default function MyLoader() {
142
156
  );
143
157
  }
144
158
  ```
159
+ ---
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ type MustNotEndWithSlash<T extends string> = T extends `${string}/` ? never : T;
12
12
  /**
13
13
  * Options passed to {@link generateRegistries}
14
14
  */
15
- type MDXRegistryOptions<
15
+ type RegistryOptions<
16
16
  S extends string,
17
17
  M extends string,
18
18
  R extends Record<string, string>
@@ -23,15 +23,15 @@ type MDXRegistryOptions<
23
23
  */
24
24
  modules: Record<string, () => Promise<unknown>>;
25
25
  /**
26
- * Directory from which to resolve the source paths in {@link MDXRegistryOptions.records}
26
+ * Directory from which to resolve the source paths in {@link RegistryOptions.records}
27
27
  */
28
28
  source: MustNotEndWithSlash<S> & MustStartWithSlash<S>;
29
29
  /**
30
- * Virtual base path on which to mount the key of {@link MDXRegistryOptions.records}
30
+ * Virtual base path on which to mount the key of {@link RegistryOptions.records}
31
31
  */
32
32
  mountOn: MustNotEndWithSlash<M> & MustStartWithSlash<M>;
33
33
  /**
34
- * Mappings of virtual path to file paths under {@link MDXRegistryOptions.source}
34
+ * Mappings of virtual path to file paths under {@link RegistryOptions.source}
35
35
  */
36
36
  records: { [K in keyof R] : K extends string ? MustNotEndWithSlash<K> & MustStartWithSlash<K> extends never ? never : R[K] extends MustStartWithSlash<R[K]> & MDXFile ? R[K] : never : never };
37
37
  };
@@ -41,14 +41,14 @@ type RegistryKey<
41
41
  R extends Record<string, string>
42
42
  > = keyof RegistryOf<unknown, S, M, R>;
43
43
  /**
44
- * Constructor for any generic registry with keys in the format of `mount-path/virtual-path` for each virual path passed in {@link MDXRegistryOptions.records}
44
+ * Constructor for any generic registry with keys in the format of `mount-path/virtual-path` for each virual path passed in {@link RegistryOptions.records}
45
45
  */
46
46
  type RegistryOf<
47
47
  T,
48
48
  S extends string,
49
49
  M extends string,
50
50
  R extends Record<string, string>
51
- > = { [K in keyof MDXRegistryOptions<S, M, R>["records"] as `${MDXRegistryOptions<S, M, R>["mountOn"]}${Extract<K, string>}`] : T };
51
+ > = { [K in keyof RegistryOptions<S, M, R>["records"] as `${RegistryOptions<S, M, R>["mountOn"]}${Extract<K, string>}`] : T };
52
52
  /**
53
53
  * Registry of react components
54
54
  */
@@ -74,14 +74,28 @@ declare function generateRegistries<
74
74
  S extends string,
75
75
  M extends string,
76
76
  R extends Record<string, string>
77
- >({ modules, source, mountOn, records }: MDXRegistryOptions<S, M, R>): [ComponentRegistry<S, M, R>, ExportsRegistry<S, M, R>];
77
+ >({ modules, source, mountOn, records }: RegistryOptions<S, M, R>): [ComponentRegistry<S, M, R>, ExportsRegistry<S, M, R>];
78
78
  /**
79
79
  * The returned object has the type of {@link React.ComponentType} + whatever user passes
80
80
  */
81
- type ExportSingleType<T> = T & {
81
+ type ExportSingleType<T> = Promise<T & {
82
82
  default: React.ComponentType;
83
- };
83
+ }>;
84
84
  type ExportAllType<T> = { [K in keyof T] : ExportSingleType<T[K]> };
85
+ declare abstract class AbstractRegistry<
86
+ C extends Record<string, React.LazyExoticComponent<React.ComponentType>>,
87
+ E extends Record<string, unknown>
88
+ > {
89
+ abstract components: C;
90
+ abstract exports: E;
91
+ getComponent<K extends keyof C>(key: K): C[K];
92
+ getComponents(): C;
93
+ getExport<
94
+ T extends object,
95
+ K extends keyof E
96
+ >(key: K): ExportSingleType<T>;
97
+ getExports<T extends Record<keyof C, object> = Record<keyof C, object>>(): ExportAllType<T>;
98
+ }
85
99
  /**
86
100
  * Wrapper class over {@link generateRegistries}. Provides methods to access components and exports from typesafe keys
87
101
  */
@@ -89,14 +103,19 @@ declare class Registry<
89
103
  S extends string,
90
104
  M extends string,
91
105
  R extends Record<string, string>
92
- > {
93
- private readonly components;
94
- private readonly exports;
95
- constructor(registryOpts: MDXRegistryOptions<S, M, R>);
96
- getComponent(key: RegistryKey<S, M, R>): ComponentRegistry<S, M, R>[RegistryKey<S, M, R>];
97
- getComponents(): ComponentRegistry<S, M, R>;
98
- getExport<T extends object = object>(key: RegistryKey<S, M, R>): ExportSingleType<T>;
99
- getExports<T extends Record<RegistryKey<S, M, R>, object> = Record<RegistryKey<S, M, R>, object>>(): ExportAllType<T>;
106
+ > extends AbstractRegistry<ComponentRegistry<S, M, R>, ExportsRegistry<S, M, R>> {
107
+ readonly components: ComponentRegistry<S, M, R>;
108
+ readonly exports: ExportsRegistry<S, M, R>;
109
+ constructor(registryOpts: RegistryOptions<S, M, R>);
110
+ }
111
+ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
112
+ /**
113
+ * Registry created by coalesing multiple {@link Registry} instances
114
+ */
115
+ declare class CoalescedRegistry<R extends readonly AbstractRegistry<Record<string, React.LazyExoticComponent<React.ComponentType>>, Record<string, unknown>>[]> extends AbstractRegistry<Record<keyof UnionToIntersection<R[number]["components"]>, React.LazyExoticComponent<React.ComponentType>>, Record<keyof UnionToIntersection<R[number]["exports"]>, unknown>> {
116
+ readonly components: Record<keyof UnionToIntersection<R[number]["components"]>, React.LazyExoticComponent<React.ComponentType>>;
117
+ readonly exports: Record<keyof UnionToIntersection<R[number]["exports"]>, unknown>;
118
+ constructor(...registries: R);
100
119
  }
101
120
  /**
102
121
  * List of default style classes
@@ -123,4 +142,4 @@ type MDXFromComponentProps = {
123
142
  * Simple way to directly load a component from the Registry
124
143
  */
125
144
  declare function MDXFromComponent({ SourceComponent, styles, fallback, elements }: MDXFromComponentProps): JSX;
126
- export { useStyles, generateRegistries, StyleContext, StyleClassesMap, StyleClassesList, StyleClasses, RegistryOf, RegistryKey, Registry, MDXRegistryOptions, MDXFromComponentProps, MDXFromComponent, MDXFile, HeaderLevels, ExportsRegistry, ExportSingleType, ExportAllType, Elements, ComponentRegistry };
145
+ export { useStyles, generateRegistries, StyleContext, StyleClassesMap, StyleClassesList, StyleClasses, RegistryOptions, RegistryOf, RegistryKey, Registry, MDXFromComponentProps, MDXFromComponent, MDXFile, HeaderLevels, ExportsRegistry, ExportSingleType, ExportAllType, Elements, ComponentRegistry, CoalescedRegistry };
package/dist/index.js CHANGED
@@ -316,12 +316,7 @@ function generateRegistries({
316
316
  return [Object.fromEntries(components), Object.fromEntries(exports)];
317
317
  }
318
318
 
319
- class Registry {
320
- components;
321
- exports;
322
- constructor(registryOpts) {
323
- [this.components, this.exports] = generateRegistries(registryOpts);
324
- }
319
+ class AbstractRegistry {
325
320
  getComponent(key) {
326
321
  return this.components[key];
327
322
  }
@@ -335,6 +330,25 @@ class Registry {
335
330
  return this.exports;
336
331
  }
337
332
  }
333
+
334
+ class Registry extends AbstractRegistry {
335
+ components;
336
+ exports;
337
+ constructor(registryOpts) {
338
+ super();
339
+ [this.components, this.exports] = generateRegistries(registryOpts);
340
+ }
341
+ }
342
+
343
+ class CoalescedRegistry extends AbstractRegistry {
344
+ components;
345
+ exports;
346
+ constructor(...registries) {
347
+ super();
348
+ this.components = Object.assign({}, ...registries.map((r) => r.getComponents()));
349
+ this.exports = Object.assign({}, ...registries.map((r) => r.getExports()));
350
+ }
351
+ }
338
352
  // src/components/Loader.tsx
339
353
  import { jsx as jsx14 } from "react/jsx-runtime";
340
354
  function MDXFromComponent({
@@ -360,5 +374,6 @@ export {
360
374
  StyleClassesList,
361
375
  Registry,
362
376
  MDXFromComponent,
363
- Elements
377
+ Elements,
378
+ CoalescedRegistry
364
379
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d1vij/jassm",
3
3
  "description": "Just another static site maker. Create simple content driven sites using MDX and React along with Typescript safety.",
4
- "version": "0.1.6",
4
+ "version": "0.1.8",
5
5
  "type": "module",
6
6
  "files": [
7
7
  "dist"