histoire 0.2.5 → 0.3.0

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.
Files changed (156) hide show
  1. package/dist/client/app/App.vue +8 -3
  2. package/dist/client/app/codegen/const.d.ts +1 -0
  3. package/dist/client/app/codegen/const.js +3 -0
  4. package/dist/client/app/codegen/vue3.js +82 -21
  5. package/dist/client/app/components/base/BaseEmpty.vue +17 -1
  6. package/dist/client/app/components/exposed/Story.vue +22 -1
  7. package/dist/client/app/components/exposed/Variant.vue +13 -1
  8. package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
  9. package/dist/client/app/components/sandbox/lib.js +2 -1
  10. package/dist/client/app/components/search/SearchLoading.vue +16 -0
  11. package/dist/client/app/components/search/SearchModal.vue +12 -149
  12. package/dist/client/app/components/search/SearchPane.vue +210 -0
  13. package/dist/client/app/components/search/search-data.d.ts +2 -0
  14. package/dist/client/app/components/search/search-data.js +7 -0
  15. package/dist/client/app/components/story/StoryControls.vue +5 -0
  16. package/dist/client/app/components/story/StoryDocs.vue +18 -11
  17. package/dist/client/app/components/story/StoryEvent.vue +53 -0
  18. package/dist/client/app/components/story/StoryEvents.vue +45 -0
  19. package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
  20. package/dist/client/app/components/story/StorySidePanel.vue +28 -3
  21. package/dist/client/app/components/story/StorySourceCode.vue +24 -14
  22. package/dist/client/app/components/story/StoryView.vue +16 -2
  23. package/dist/client/app/components/tree/StoryGroup.vue +4 -4
  24. package/dist/client/app/components/tree/StoryListFolder.vue +4 -4
  25. package/dist/client/app/components/tree/StoryListItem.vue +4 -1
  26. package/dist/client/app/router.d.ts +1 -0
  27. package/dist/client/app/router.js +3 -1
  28. package/dist/client/app/sandbox.js +11 -1
  29. package/dist/client/app/stores/events.d.ts +29 -0
  30. package/dist/client/app/stores/events.js +25 -0
  31. package/dist/client/app/stores/folder.d.ts +10 -0
  32. package/dist/client/app/stores/folder.js +47 -0
  33. package/dist/client/app/stores/story.d.ts +14 -8
  34. package/dist/client/app/stores/story.js +20 -33
  35. package/dist/client/app/style/main.pcss +5 -0
  36. package/dist/client/app/style/sandbox.css +2 -0
  37. package/dist/client/app/types.d.ts +1 -0
  38. package/dist/client/app/util/const.d.ts +1 -0
  39. package/dist/client/app/util/const.js +1 -0
  40. package/dist/client/app/util/events.d.ts +1 -0
  41. package/dist/client/app/util/events.js +31 -0
  42. package/dist/client/app/util/mapping.js +2 -2
  43. package/dist/client/index.d.ts +2 -1
  44. package/dist/client/index.js +2 -1
  45. package/dist/client/server/Story.server.vue +13 -2
  46. package/dist/node/alias.d.ts +2 -1
  47. package/dist/node/alias.js +3 -2
  48. package/dist/node/build.js +74 -21
  49. package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
  50. package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
  51. package/dist/node/collect/index.js +3 -1
  52. package/dist/node/commands/preview.js +4 -25
  53. package/dist/node/config.d.ts +103 -3
  54. package/dist/node/config.js +40 -23
  55. package/dist/node/context.d.ts +3 -3
  56. package/dist/node/context.js +1 -1
  57. package/dist/node/index.d.ts +1 -0
  58. package/dist/node/index.js +1 -0
  59. package/dist/node/load.d.ts +12 -0
  60. package/dist/node/load.js +42 -0
  61. package/dist/node/plugin.d.ts +67 -11
  62. package/dist/node/plugin.js +42 -245
  63. package/dist/node/preview.d.ts +8 -0
  64. package/dist/node/preview.js +41 -0
  65. package/dist/node/search.d.ts +2 -0
  66. package/dist/node/search.js +68 -0
  67. package/dist/node/server.js +43 -21
  68. package/dist/node/stories.d.ts +2 -0
  69. package/dist/node/stories.js +4 -4
  70. package/dist/node/types.d.ts +1 -0
  71. package/dist/node/util/find-up.d.ts +1 -0
  72. package/dist/node/util/find-up.js +20 -0
  73. package/dist/node/vite.d.ts +13 -0
  74. package/dist/node/vite.js +278 -0
  75. package/dist/style.css +111 -47
  76. package/package.json +5 -3
  77. package/scripts/copy.mjs +2 -1
  78. package/src/client/app/App.vue +8 -3
  79. package/src/client/app/codegen/const.ts +3 -0
  80. package/src/client/app/codegen/vue3.ts +80 -22
  81. package/src/client/app/components/base/BaseEmpty.vue +17 -1
  82. package/src/client/app/components/exposed/Story.vue +22 -1
  83. package/src/client/app/components/exposed/Variant.vue +13 -1
  84. package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
  85. package/src/client/app/components/sandbox/lib.ts +3 -1
  86. package/src/client/app/components/search/SearchLoading.vue +16 -0
  87. package/src/client/app/components/search/SearchModal.vue +12 -149
  88. package/src/client/app/components/search/SearchPane.vue +210 -0
  89. package/src/client/app/components/search/search-data.ts +9 -0
  90. package/src/client/app/components/story/StoryControls.vue +5 -0
  91. package/src/client/app/components/story/StoryDocs.vue +18 -11
  92. package/src/client/app/components/story/StoryEvent.vue +53 -0
  93. package/src/client/app/components/story/StoryEvents.vue +45 -0
  94. package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
  95. package/src/client/app/components/story/StorySidePanel.vue +28 -3
  96. package/src/client/app/components/story/StorySourceCode.vue +24 -14
  97. package/src/client/app/components/story/StoryView.vue +16 -2
  98. package/src/client/app/components/tree/StoryGroup.vue +4 -4
  99. package/src/client/app/components/tree/StoryListFolder.vue +4 -4
  100. package/src/client/app/components/tree/StoryListItem.vue +4 -1
  101. package/src/client/app/router.ts +4 -1
  102. package/src/client/app/sandbox.ts +12 -2
  103. package/src/client/app/stores/events.ts +36 -0
  104. package/{dist/client/app/stores/story.ts → src/client/app/stores/folder.ts} +16 -23
  105. package/src/client/app/stores/story.ts +21 -40
  106. package/src/client/app/style/main.pcss +5 -0
  107. package/src/client/app/style/sandbox.css +2 -0
  108. package/src/client/app/types.ts +1 -0
  109. package/src/client/app/util/const.ts +1 -0
  110. package/src/client/app/util/events.ts +30 -0
  111. package/src/client/app/util/mapping.ts +2 -2
  112. package/src/client/index.ts +2 -1
  113. package/src/client/server/Story.server.vue +13 -2
  114. package/src/node/alias.ts +4 -2
  115. package/src/node/build.ts +86 -21
  116. package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
  117. package/src/node/collect/index.ts +3 -1
  118. package/src/node/commands/preview.ts +4 -33
  119. package/src/node/config.ts +53 -27
  120. package/src/node/context.ts +3 -3
  121. package/src/node/index.ts +1 -0
  122. package/src/node/load.ts +63 -0
  123. package/src/node/plugin.ts +75 -252
  124. package/src/node/preview.ts +56 -0
  125. package/src/node/search.ts +75 -0
  126. package/src/node/server.ts +49 -23
  127. package/src/node/stories.ts +4 -4
  128. package/src/node/types.ts +1 -0
  129. package/src/node/util/find-up.ts +24 -0
  130. package/src/node/vite.ts +296 -0
  131. package/tailwind.config.cjs +1 -0
  132. package/dist/client/app/codegen/serialize-js.ts +0 -119
  133. package/dist/client/app/codegen/util.ts +0 -79
  134. package/dist/client/app/codegen/vue3.ts +0 -276
  135. package/dist/client/app/components/sandbox/lib.ts +0 -8
  136. package/dist/client/app/global-components.ts +0 -13
  137. package/dist/client/app/index.ts +0 -28
  138. package/dist/client/app/router.ts +0 -17
  139. package/dist/client/app/sandbox.ts +0 -94
  140. package/dist/client/app/stores/preview-settings.ts +0 -17
  141. package/dist/client/app/types.ts +0 -78
  142. package/dist/client/app/util/config.ts +0 -6
  143. package/dist/client/app/util/const.ts +0 -3
  144. package/dist/client/app/util/dark.ts +0 -4
  145. package/dist/client/app/util/env.ts +0 -1
  146. package/dist/client/app/util/keyboard.ts +0 -99
  147. package/dist/client/app/util/mapping.ts +0 -48
  148. package/dist/client/app/util/preview-settings.ts +0 -8
  149. package/dist/client/app/util/reactivity.ts +0 -22
  150. package/dist/client/app/util/responsive.ts +0 -3
  151. package/dist/client/app/util/scroll.ts +0 -31
  152. package/dist/client/app/util/tooltip.ts +0 -13
  153. package/dist/client/app/util/variant.ts +0 -20
  154. package/dist/client/index.ts +0 -12
  155. package/dist/client/server/index.ts +0 -32
  156. package/dist/client/shim.d.ts +0 -5
@@ -1,6 +1,8 @@
1
1
  import { createRouter, createWebHistory } from 'vue-router';
2
+ // @ts-expect-error env not defined
3
+ export const base = import.meta.env.BASE_URL;
2
4
  export const router = createRouter({
3
- history: createWebHistory(),
5
+ history: createWebHistory(base),
4
6
  routes: [
5
7
  {
6
8
  path: '/',
@@ -15,6 +15,7 @@ import { toRawDeep } from './util/reactivity';
15
15
  const query = parseQuery(window.location.search);
16
16
  const file = ref(mapFile(files.find(f => f.id === query.storyId)));
17
17
  const app = createApp({
18
+ name: 'SandboxApp',
18
19
  setup() {
19
20
  const story = computed(() => file.value.story);
20
21
  const variant = computed(() => story.value?.variants.find(v => v.id === query.variantId));
@@ -25,7 +26,16 @@ const app = createApp({
25
26
  if (!mounted)
26
27
  return;
27
28
  synced = true;
28
- Object.assign(variant.value.state, event.data.state);
29
+ if (variant.value.state) {
30
+ for (const key in event.data.state) {
31
+ if (typeof variant.value.state[key] === 'object') {
32
+ Object.assign(variant.value.state[key], event.data.state[key]);
33
+ }
34
+ else {
35
+ variant.value.state[key] = event.data.state[key];
36
+ }
37
+ }
38
+ }
29
39
  }
30
40
  else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
31
41
  applyPreviewSettings(event.data.settings);
@@ -0,0 +1,29 @@
1
+ export declare type HstEvent = {
2
+ name: string;
3
+ argument: unknown;
4
+ };
5
+ export declare const useEventsStore: import("pinia").StoreDefinition<"events", import("pinia")._UnwrapAll<Pick<{
6
+ addEvent: (event: HstEvent) => void;
7
+ reset: () => void;
8
+ events: {
9
+ name: string;
10
+ argument: unknown;
11
+ }[];
12
+ unseen: import("vue").Ref<number>;
13
+ }, "events" | "unseen">>, import("pinia")._UnwrapAll<Pick<{
14
+ addEvent: (event: HstEvent) => void;
15
+ reset: () => void;
16
+ events: {
17
+ name: string;
18
+ argument: unknown;
19
+ }[];
20
+ unseen: import("vue").Ref<number>;
21
+ }, never>>, Pick<{
22
+ addEvent: (event: HstEvent) => void;
23
+ reset: () => void;
24
+ events: {
25
+ name: string;
26
+ argument: unknown;
27
+ }[];
28
+ unseen: import("vue").Ref<number>;
29
+ }, "addEvent" | "reset">>;
@@ -0,0 +1,25 @@
1
+ import { defineStore } from 'pinia';
2
+ import { ref, reactive, watch } from 'vue';
3
+ import { useStoryStore } from './story.js';
4
+ export const useEventsStore = defineStore('events', () => {
5
+ const storyStore = useStoryStore();
6
+ const events = reactive([]);
7
+ const unseen = ref(0);
8
+ function addEvent(event) {
9
+ events.push(event);
10
+ unseen.value++;
11
+ }
12
+ function reset() {
13
+ events.length = 0;
14
+ unseen.value = 0;
15
+ }
16
+ watch(() => storyStore.currentVariant, () => {
17
+ reset();
18
+ });
19
+ return {
20
+ addEvent,
21
+ reset,
22
+ events,
23
+ unseen,
24
+ };
25
+ });
@@ -0,0 +1,10 @@
1
+ export declare const useFolderStore: import("pinia").StoreDefinition<"folder", import("pinia")._UnwrapAll<Pick<{
2
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
3
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
4
+ }, never>>, import("pinia")._UnwrapAll<Pick<{
5
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
6
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
7
+ }, never>>, Pick<{
8
+ isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
9
+ toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
10
+ }, "isFolderOpened" | "toggleFolder">>;
@@ -0,0 +1,47 @@
1
+ import { defineStore } from 'pinia';
2
+ import { watch } from 'vue';
3
+ import { useStorage } from '@vueuse/core';
4
+ import { useStoryStore } from './story';
5
+ export const useFolderStore = defineStore('folder', () => {
6
+ const openedFolders = useStorage('_histoire-tree-state', new Map());
7
+ function getStringPath(path) {
8
+ return path.join('␜');
9
+ }
10
+ function toggleFolder(path, defaultToggleValue = true) {
11
+ const stringPath = getStringPath(path);
12
+ const currentValue = openedFolders.value.get(stringPath);
13
+ if (currentValue == null) {
14
+ setFolderOpen(stringPath, defaultToggleValue);
15
+ }
16
+ else if (currentValue) {
17
+ setFolderOpen(stringPath, false);
18
+ }
19
+ else {
20
+ setFolderOpen(stringPath, true);
21
+ }
22
+ }
23
+ function setFolderOpen(path, value) {
24
+ const stringPath = typeof path === 'string' ? path : getStringPath(path);
25
+ openedFolders.value.set(stringPath, value);
26
+ }
27
+ function isFolderOpened(path, defaultValue = false) {
28
+ const value = openedFolders.value.get(getStringPath(path));
29
+ if (value == null) {
30
+ return defaultValue;
31
+ }
32
+ return value;
33
+ }
34
+ function openFileFolders(path) {
35
+ for (let pathLength = 1; pathLength < path.length; pathLength++) {
36
+ setFolderOpen(path.slice(0, pathLength), true);
37
+ }
38
+ }
39
+ const storyStore = useStoryStore();
40
+ watch(() => storyStore.currentStory, (story) => {
41
+ openFileFolders(story.file.path);
42
+ });
43
+ return {
44
+ isFolderOpened,
45
+ toggleFolder,
46
+ };
47
+ });
@@ -1,4 +1,4 @@
1
- import { Story } from '../types';
1
+ import { Story, Variant } from '../types';
2
2
  export declare const useStoryStore: import("pinia").StoreDefinition<"story", import("pinia")._UnwrapAll<Pick<{
3
3
  stories: import("vue").Ref<{
4
4
  id: string;
@@ -25,6 +25,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
25
25
  };
26
26
  icon?: string;
27
27
  iconColor?: string;
28
+ docsOnly?: boolean;
28
29
  file?: {
29
30
  id: string;
30
31
  framework: string;
@@ -72,6 +73,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
72
73
  };
73
74
  icon?: string;
74
75
  iconColor?: string;
76
+ docsOnly?: boolean;
75
77
  file?: {
76
78
  id: string;
77
79
  framework: string;
@@ -106,8 +108,8 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
106
108
  configReady?: boolean;
107
109
  previewReady?: boolean;
108
110
  }>;
109
- isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
110
- toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
111
+ getStoryById: (id: string) => Story;
112
+ getVariantById: (idWithStoryId: string) => Variant;
111
113
  }, "stories">>, import("pinia")._UnwrapAll<Pick<{
112
114
  stories: import("vue").Ref<{
113
115
  id: string;
@@ -134,6 +136,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
134
136
  };
135
137
  icon?: string;
136
138
  iconColor?: string;
139
+ docsOnly?: boolean;
137
140
  file?: {
138
141
  id: string;
139
142
  framework: string;
@@ -181,6 +184,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
181
184
  };
182
185
  icon?: string;
183
186
  iconColor?: string;
187
+ docsOnly?: boolean;
184
188
  file?: {
185
189
  id: string;
186
190
  framework: string;
@@ -215,8 +219,8 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
215
219
  configReady?: boolean;
216
220
  previewReady?: boolean;
217
221
  }>;
218
- isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
219
- toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
222
+ getStoryById: (id: string) => Story;
223
+ getVariantById: (idWithStoryId: string) => Variant;
220
224
  }, "currentStory" | "currentVariant">>, Pick<{
221
225
  stories: import("vue").Ref<{
222
226
  id: string;
@@ -243,6 +247,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
243
247
  };
244
248
  icon?: string;
245
249
  iconColor?: string;
250
+ docsOnly?: boolean;
246
251
  file?: {
247
252
  id: string;
248
253
  framework: string;
@@ -290,6 +295,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
290
295
  };
291
296
  icon?: string;
292
297
  iconColor?: string;
298
+ docsOnly?: boolean;
293
299
  file?: {
294
300
  id: string;
295
301
  framework: string;
@@ -324,6 +330,6 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
324
330
  configReady?: boolean;
325
331
  previewReady?: boolean;
326
332
  }>;
327
- isFolderOpened: (path: Array<string>, defaultValue?: boolean) => boolean;
328
- toggleFolder: (path: Array<string>, defaultToggleValue?: boolean) => void;
329
- }, "setStories" | "isFolderOpened" | "toggleFolder">>;
333
+ getStoryById: (id: string) => Story;
334
+ getVariantById: (idWithStoryId: string) => Variant;
335
+ }, "setStories" | "getStoryById" | "getVariantById">>;
@@ -1,7 +1,6 @@
1
1
  import { defineStore } from 'pinia';
2
- import { computed, ref, watch } from 'vue';
2
+ import { computed, ref } from 'vue';
3
3
  import { router } from '../router';
4
- import { useStorage } from '@vueuse/core';
5
4
  export const useStoryStore = defineStore('story', () => {
6
5
  const stories = ref([]);
7
6
  function setStories(value) {
@@ -9,44 +8,32 @@ export const useStoryStore = defineStore('story', () => {
9
8
  }
10
9
  const currentStory = computed(() => stories.value.find(s => s.id === router.currentRoute.value.params.storyId));
11
10
  const currentVariant = computed(() => currentStory.value?.variants.find(v => v.id === router.currentRoute.value.query.variantId));
12
- const openedFolders = useStorage('_histoire-tree-state', new Map());
13
- function getStringPath(path) {
14
- return path.join('␜');
15
- }
16
- function toggleFolder(path, defaultToggleValue = true) {
17
- const stringPath = getStringPath(path);
18
- const currentValue = openedFolders.value.get(stringPath);
19
- if (currentValue == null) {
20
- openedFolders.value.set(stringPath, defaultToggleValue);
21
- }
22
- else if (currentValue) {
23
- openedFolders.value.set(stringPath, false);
24
- }
25
- else {
26
- openedFolders.value.set(stringPath, true);
27
- }
28
- }
29
- function isFolderOpened(path, defaultValue = false) {
30
- const value = openedFolders.value.get(getStringPath(path));
31
- if (value == null) {
32
- return defaultValue;
11
+ const maps = computed(() => {
12
+ const storyMap = new Map();
13
+ const variantMap = new Map();
14
+ for (const story of stories.value) {
15
+ storyMap.set(story.id, story);
16
+ for (const variant of story.variants) {
17
+ variantMap.set(`${story.id}:${variant.id}`, variant);
18
+ }
33
19
  }
34
- return value;
20
+ return {
21
+ stories: storyMap,
22
+ variants: variantMap,
23
+ };
24
+ });
25
+ function getStoryById(id) {
26
+ return maps.value.stories.get(id);
35
27
  }
36
- function openFileFolders(path) {
37
- for (let pathLength = 1; pathLength < path.length; pathLength++) {
38
- toggleFolder(path.slice(0, pathLength), true);
39
- }
28
+ function getVariantById(idWithStoryId) {
29
+ return maps.value.variants.get(idWithStoryId);
40
30
  }
41
- watch(currentStory, () => {
42
- openFileFolders(currentStory.value.file.path);
43
- });
44
31
  return {
45
32
  stories,
46
33
  setStories,
47
34
  currentStory,
48
35
  currentVariant,
49
- isFolderOpened,
50
- toggleFolder,
36
+ getStoryById,
37
+ getVariantById,
51
38
  };
52
39
  });
@@ -1,5 +1,6 @@
1
1
  @import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
2
2
  @import 'floating-vue/dist/style.css';
3
+ @import '@histoire/controls/dist/style.css';
3
4
 
4
5
  @import 'floating-vue.pcss';
5
6
  @import 'transitions.pcss';
@@ -78,4 +79,8 @@ body {
78
79
  }
79
80
  }
80
81
  }
82
+
83
+ hr {
84
+ margin: 1rem 0;
85
+ }
81
86
  }
@@ -1,3 +1,5 @@
1
+ @import '@histoire/controls/dist/style.css';
2
+
1
3
  html,
2
4
  body {
3
5
  background: transparent !important;
@@ -19,6 +19,7 @@ export interface Story {
19
19
  };
20
20
  icon?: string;
21
21
  iconColor?: string;
22
+ docsOnly?: boolean;
22
23
  file?: StoryFile;
23
24
  lastSelectedVariant?: Variant;
24
25
  }
@@ -1,3 +1,4 @@
1
1
  export declare const STATE_SYNC = "__histoire:state-sync";
2
2
  export declare const SANDBOX_READY = "__histoire:sandbox-ready";
3
+ export declare const EVENT_SEND = "__histoire:event";
3
4
  export declare const PREVIEW_SETTINGS_SYNC = "__histoire:preview-settings-sync";
@@ -1,3 +1,4 @@
1
1
  export const STATE_SYNC = '__histoire:state-sync';
2
2
  export const SANDBOX_READY = '__histoire:sandbox-ready';
3
+ export const EVENT_SEND = '__histoire:event';
3
4
  export const PREVIEW_SETTINGS_SYNC = '__histoire:preview-settings-sync';
@@ -0,0 +1 @@
1
+ export declare function hstEvent(name: string, argument: any): void;
@@ -0,0 +1,31 @@
1
+ import { useEventsStore } from '../stores/events.js';
2
+ import { EVENT_SEND } from './const';
3
+ export function hstEvent(name, argument) {
4
+ console.log('[histoire] Event fired', { name, argument });
5
+ const event = {
6
+ name,
7
+ argument: JSON.parse(stringifyEvent(argument)), // Needed for HTMLEvent that can't be cloned
8
+ };
9
+ if (location.href.includes('__sandbox')) {
10
+ window.parent?.postMessage({
11
+ type: EVENT_SEND,
12
+ event,
13
+ });
14
+ }
15
+ else {
16
+ useEventsStore().addEvent(event);
17
+ }
18
+ }
19
+ function stringifyEvent(e) {
20
+ const obj = {};
21
+ for (const k in e) {
22
+ obj[k] = e[k];
23
+ }
24
+ return JSON.stringify(obj, (k, v) => {
25
+ if (v instanceof Node)
26
+ return 'Node';
27
+ if (v instanceof Window)
28
+ return 'Window';
29
+ return v;
30
+ }, ' ');
31
+ }
@@ -18,7 +18,7 @@ export function mapFile(file, existingFile) {
18
18
  for (const key in existingVariant) {
19
19
  if (typeof existingVariant[key] !== 'undefined') {
20
20
  if (key === 'state') {
21
- Object.assign(variant.state, existingVariant.state);
21
+ variant.state = existingVariant.state;
22
22
  continue;
23
23
  }
24
24
  variant[key] = existingVariant[key];
@@ -35,7 +35,7 @@ export function mapFile(file, existingFile) {
35
35
  export function mapVariant(variant) {
36
36
  return {
37
37
  ...variant,
38
- state: {},
38
+ state: null,
39
39
  initState: null,
40
40
  slots: () => ({}),
41
41
  previewReady: false,
@@ -1,8 +1,9 @@
1
1
  import type { App } from 'vue';
2
2
  import type { Story, Variant } from './app/types.js';
3
+ export { hstEvent } from './app/util/events';
3
4
  export declare type Vue3StorySetupHandler = (payload: {
4
5
  app: App;
5
6
  story: Story;
6
7
  variant: Variant;
7
8
  }) => Promise<void> | void;
8
- export declare function defineVue3StorySetup(handler: Vue3StorySetupHandler): Vue3StorySetupHandler;
9
+ export declare function defineSetupVue3(handler: Vue3StorySetupHandler): Vue3StorySetupHandler;
@@ -1,3 +1,4 @@
1
- export function defineVue3StorySetup(handler) {
1
+ export { hstEvent } from './app/util/events';
2
+ export function defineSetupVue3(handler) {
2
3
  return handler;
3
4
  }
@@ -11,6 +11,11 @@ export default defineComponent({
11
11
  default: null,
12
12
  },
13
13
 
14
+ id: {
15
+ type: String,
16
+ default: null,
17
+ },
18
+
14
19
  group: {
15
20
  type: String,
16
21
  default: null,
@@ -30,6 +35,11 @@ export default defineComponent({
30
35
  type: String,
31
36
  default: null,
32
37
  },
38
+
39
+ docsOnly: {
40
+ type: Boolean,
41
+ default: false,
42
+ },
33
43
  },
34
44
 
35
45
  setup (props) {
@@ -40,12 +50,13 @@ export default defineComponent({
40
50
  // Story
41
51
 
42
52
  const story: Story = {
43
- id: attrs.data.id,
53
+ id: props.id ?? attrs.data.id,
44
54
  title: props.title ?? attrs.data.fileName,
45
55
  group: props.group,
46
56
  layout: props.layout,
47
57
  icon: props.icon,
48
58
  iconColor: props.iconColor,
59
+ docsOnly: props.docsOnly,
49
60
  variants: [],
50
61
  }
51
62
  const addStory = inject('addStory', null)
@@ -76,7 +87,7 @@ export default defineComponent({
76
87
  render () {
77
88
  let suppressError = false
78
89
  try {
79
- return this.$slots.default({
90
+ return this.$slots.default?.({
80
91
  get state () {
81
92
  // No variant tags
82
93
  suppressError = true
@@ -1,2 +1,3 @@
1
- export declare const DIST_CLIENT_PATH: string;
1
+ export declare const DIST_PATH: string;
2
2
  export declare const APP_PATH: string;
3
+ export declare const TEMP_PATH: string;
@@ -1,5 +1,6 @@
1
1
  import { dirname, join } from 'pathe';
2
2
  import { fileURLToPath } from 'url';
3
3
  const __dirname = dirname(fileURLToPath(import.meta.url));
4
- export const DIST_CLIENT_PATH = join(__dirname, '../client');
5
- export const APP_PATH = join(DIST_CLIENT_PATH, 'app');
4
+ export const DIST_PATH = join(__dirname, '../');
5
+ export const APP_PATH = join(DIST_PATH, 'client', 'app');
6
+ export const TEMP_PATH = join(process.cwd(), 'node_modules', '.histoire');
@@ -1,13 +1,16 @@
1
1
  import { join } from 'pathe';
2
- import { build as viteBuild, createServer as createViteServer } from 'vite';
2
+ import { build as viteBuild, createServer as createViteServer, resolveConfig as resolveViteConfig } from 'vite';
3
3
  import fs from 'fs-extra';
4
4
  import { lookup as lookupMime } from 'mrmime';
5
5
  import pc from 'picocolors';
6
6
  import { performance } from 'perf_hooks';
7
7
  import { APP_PATH } from './alias.js';
8
- import { createVitePlugins } from './plugin.js';
8
+ import { createVitePlugins } from './vite.js';
9
9
  import { findAllStories } from './stories.js';
10
10
  import { useCollectStories } from './collect/index.js';
11
+ import { BuildPluginApi } from './plugin.js';
12
+ import { useModuleLoader } from './load.js';
13
+ import { startPreview } from './preview.js';
11
14
  const PRELOAD_MODULES = [
12
15
  'vendor',
13
16
  ];
@@ -19,11 +22,25 @@ const PREFETCHED_MODULES = [
19
22
  export async function build(ctx) {
20
23
  const startTime = performance.now();
21
24
  await findAllStories(ctx);
22
- // Collect story data
23
25
  const server = await createViteServer({
24
26
  plugins: await createVitePlugins(ctx),
25
27
  });
26
28
  await server.pluginContainer.buildStart({});
29
+ const moduleLoader = useModuleLoader({
30
+ server,
31
+ throws: true,
32
+ });
33
+ const buildEndCallbacks = [];
34
+ const previewStoryCallbacks = [];
35
+ for (const plugin of ctx.config.plugins) {
36
+ if (plugin.onBuild) {
37
+ const api = new BuildPluginApi(ctx, plugin, moduleLoader);
38
+ await plugin.onBuild(api);
39
+ buildEndCallbacks.push(...api.buildEndCallbacks);
40
+ previewStoryCallbacks.push(...api.previewStoryCallbacks);
41
+ }
42
+ }
43
+ // Collect story data
27
44
  const { executeStoryFile, destroy: destroyCollectStories } = useCollectStories({
28
45
  server,
29
46
  throws: true,
@@ -31,7 +48,6 @@ export async function build(ctx) {
31
48
  for (const storyFile of ctx.storyFiles) {
32
49
  await executeStoryFile(storyFile);
33
50
  }
34
- await server.close();
35
51
  await destroyCollectStories();
36
52
  const storyCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ? 1 : 0), 0);
37
53
  const variantCount = ctx.storyFiles.reduce((sum, file) => sum + (file.story?.variants.length ?? 0), 0);
@@ -41,8 +57,8 @@ export async function build(ctx) {
41
57
  build: {
42
58
  rollupOptions: {
43
59
  input: [
44
- join(APP_PATH, 'index.ts'),
45
- join(APP_PATH, 'sandbox.ts'),
60
+ join(APP_PATH, 'index.js'),
61
+ join(APP_PATH, 'sandbox.js'),
46
62
  ],
47
63
  output: {
48
64
  manualChunks(id) {
@@ -55,49 +71,86 @@ export async function build(ctx) {
55
71
  outDir: ctx.config.outDir,
56
72
  emptyOutDir: true,
57
73
  cssCodeSplit: false,
74
+ minify: false,
58
75
  },
59
76
  });
60
77
  const result = Array.isArray(results) ? results[0] : results;
78
+ const resolvedViteConfig = await resolveViteConfig({
79
+ plugins: await createVitePlugins(ctx),
80
+ }, 'build');
61
81
  const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset');
62
82
  // Preload
63
83
  const preloadOutputs = result.output.filter(o => PRELOAD_MODULES.includes(o.name) && o.type === 'chunk');
64
- const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload');
84
+ const preloadHtml = generateScriptLinks(preloadOutputs.map(o => o.fileName), 'preload', ctx, resolvedViteConfig);
65
85
  // Prefetch
66
86
  const prefetchOutputs = result.output.filter(o => PREFETCHED_MODULES.includes(o.name) && o.type === 'chunk');
67
- const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch');
87
+ const prefetchHtml = generateScriptLinks(prefetchOutputs.map(o => o.fileName), 'prefetch', ctx, resolvedViteConfig);
68
88
  // Index
69
89
  const indexOutput = result.output.find(o => o.name === 'index' && o.type === 'chunk');
70
- await writeHtml(indexOutput.fileName, styleOutput.fileName, 'index.html', {
90
+ const indexHtml = generateEntryHtml(indexOutput.fileName, styleOutput.fileName, {
71
91
  HEAD: `${preloadHtml}${prefetchHtml}`,
72
- }, ctx);
92
+ }, ctx, resolvedViteConfig);
93
+ await writeFile('index.html', indexHtml, ctx);
73
94
  // Sandbox
74
95
  const sandboxOutput = result.output.find(o => o.name === 'sandbox' && o.type === 'chunk');
75
- await writeHtml(sandboxOutput.fileName, styleOutput.fileName, '__sandbox.html', {}, ctx);
96
+ const sandboxHtml = generateEntryHtml(sandboxOutput.fileName, styleOutput.fileName, {}, ctx, resolvedViteConfig);
97
+ await writeFile('__sandbox.html', sandboxHtml, ctx);
76
98
  const duration = performance.now() - startTime;
77
99
  if (emptyStoryCount) {
78
100
  console.warn(pc.yellow(`⚠️ ${emptyStoryCount} empty story file`));
79
101
  }
80
102
  console.log(pc.green(`✅ Built ${storyCount} stories (${variantCount} variants) in ${Math.round(duration / 1000 * 100) / 100}s`));
103
+ // Render
104
+ if (previewStoryCallbacks.length) {
105
+ const { baseUrl, close } = await startPreview(resolvedViteConfig, null, ctx);
106
+ for (const storyFile of ctx.storyFiles) {
107
+ const story = storyFile.story;
108
+ for (const variant of story.variants) {
109
+ const query = new URLSearchParams();
110
+ query.append('storyId', story.id);
111
+ query.append('variantId', variant.id);
112
+ const url = `${baseUrl}__sandbox?${query.toString()}`;
113
+ for (const fn of previewStoryCallbacks) {
114
+ await fn({
115
+ file: storyFile.path,
116
+ story,
117
+ variant,
118
+ url,
119
+ });
120
+ }
121
+ }
122
+ }
123
+ await close();
124
+ }
125
+ await server.close();
126
+ for (const fn of buildEndCallbacks) {
127
+ await fn();
128
+ }
81
129
  }
82
- async function writeHtml(jsEntryFile, cssEntryFile, htmlFileName, variables, ctx) {
83
- const code = `<!DOCTYPE html>
130
+ function generateBaseHtml(head, body, ctx) {
131
+ return `<!DOCTYPE html>
84
132
  <html>
85
133
  <head>
86
134
  <title>${ctx.config.theme.title}</title>
87
135
  <meta charset="utf-8">
88
136
  <meta name="viewport" content="width=device-width,initial-scale=1">
89
137
  <meta name="description" content="">
90
- <link rel="stylesheet" href="/${cssEntryFile}">
91
- ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
92
- ${variables.HEAD ?? ''}
138
+ ${head}
93
139
  </head>
94
140
  <body>
95
- <div id="app"></div>
96
- <script type="module" src="/${jsEntryFile}"></script>
141
+ ${body}
97
142
  </body>
98
143
  </html>`;
99
- await fs.writeFile(join(ctx.config.outDir, htmlFileName), code, 'utf8');
100
144
  }
101
- function generateScriptLinks(prefetchScripts, rel) {
102
- return prefetchScripts.map(s => `<link rel="${rel}" href="${s}" as="script" crossOrigin="anonymous">`).join('');
145
+ function generateEntryHtml(jsEntryFile, cssEntryFile, variables, ctx, resolvedViteConfig) {
146
+ return generateBaseHtml(`<link rel="stylesheet" href="${resolvedViteConfig.base}${cssEntryFile}">
147
+ ${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
148
+ ${variables.HEAD ?? ''}`, `<div id="app"></div>
149
+ <script type="module" src="${resolvedViteConfig.base}${jsEntryFile}"></script>`, ctx);
150
+ }
151
+ async function writeFile(fileName, content, ctx) {
152
+ await fs.writeFile(join(ctx.config.outDir, fileName), content, 'utf8');
153
+ }
154
+ function generateScriptLinks(prefetchScripts, rel, ctx, resolvedViteConfig) {
155
+ return prefetchScripts.map(s => `<link rel="${rel}" href="${resolvedViteConfig.base}${s}" as="script" crossOrigin="anonymous">`).join('');
103
156
  }
@@ -0,0 +1,5 @@
1
+ import { Plugin } from '../plugin.js';
2
+ export interface TailwindTokensOptions {
3
+ configFile?: string;
4
+ }
5
+ export declare function tailwindTokens(options?: TailwindTokensOptions): Plugin;