histoire 0.1.3 → 0.2.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 (76) hide show
  1. package/dist/client/app/App.vue +16 -1
  2. package/dist/client/app/components/app/AppHeader.vue +7 -6
  3. package/dist/client/app/components/app/Breadcrumb.vue +98 -0
  4. package/dist/client/app/components/app/MobileOverlay.vue +34 -0
  5. package/dist/client/app/components/base/BaseCheckbox.vue +76 -0
  6. package/dist/client/app/components/misc/HatchedPattern.vue +11 -23
  7. package/dist/client/app/components/search/SearchItem.vue +5 -2
  8. package/dist/client/app/components/search/SearchModal.vue +6 -6
  9. package/dist/client/app/components/story/StoryControls.vue +1 -1
  10. package/dist/client/app/components/story/StoryDocs.vue +1 -0
  11. package/dist/client/app/components/story/StoryListFolder.vue +3 -3
  12. package/dist/client/app/components/story/StoryListItem.vue +6 -3
  13. package/dist/client/app/components/story/StoryResponsivePreview.vue +53 -8
  14. package/dist/client/app/components/story/StorySidePanel.vue +69 -0
  15. package/dist/client/app/components/story/StorySourceCode.vue +4 -1
  16. package/dist/client/app/components/story/StoryVariantListItem.vue +6 -3
  17. package/dist/client/app/components/story/StoryVariantSingleView.vue +14 -10
  18. package/dist/client/app/components/story/StoryView.vue +10 -121
  19. package/dist/client/app/components/story/StoryViewer.vue +175 -0
  20. package/dist/client/app/stores/story.d.ts +6 -6
  21. package/dist/client/app/stores/story.js +2 -1
  22. package/dist/client/app/stores/story.ts +6 -2
  23. package/dist/client/app/style/{floating-vue.css → floating-vue.pcss} +0 -0
  24. package/dist/client/app/style/{main.css → main.pcss} +13 -3
  25. package/dist/client/app/style/transitions.pcss +26 -0
  26. package/dist/client/app/types.d.ts +1 -1
  27. package/dist/client/app/types.ts +1 -1
  28. package/dist/client/app/util/responsive.d.ts +1 -0
  29. package/dist/client/app/util/responsive.js +2 -0
  30. package/dist/client/app/util/responsive.ts +3 -0
  31. package/dist/node/bin.js +7 -0
  32. package/dist/node/build.js +1 -1
  33. package/dist/node/{collect.d.ts → collect/index.d.ts} +2 -2
  34. package/dist/node/{collect.js → collect/index.js} +37 -27
  35. package/dist/node/collect/worker.d.ts +14 -0
  36. package/dist/node/collect/worker.js +36 -0
  37. package/dist/node/commands/preview.d.ts +4 -0
  38. package/dist/node/commands/preview.js +36 -0
  39. package/dist/node/server.js +1 -1
  40. package/dist/node/types.d.ts +1 -1
  41. package/dist/style.css +231 -104
  42. package/package.json +8 -4
  43. package/src/client/app/App.vue +16 -1
  44. package/src/client/app/components/app/AppHeader.vue +7 -6
  45. package/src/client/app/components/app/Breadcrumb.vue +98 -0
  46. package/src/client/app/components/app/MobileOverlay.vue +34 -0
  47. package/src/client/app/components/base/BaseCheckbox.vue +76 -0
  48. package/src/client/app/components/misc/HatchedPattern.vue +11 -23
  49. package/src/client/app/components/search/SearchItem.vue +5 -2
  50. package/src/client/app/components/search/SearchModal.vue +6 -6
  51. package/src/client/app/components/story/StoryControls.vue +1 -1
  52. package/src/client/app/components/story/StoryDocs.vue +1 -0
  53. package/src/client/app/components/story/StoryListFolder.vue +3 -3
  54. package/src/client/app/components/story/StoryListItem.vue +6 -3
  55. package/src/client/app/components/story/StoryResponsivePreview.vue +53 -8
  56. package/src/client/app/components/story/StorySidePanel.vue +69 -0
  57. package/src/client/app/components/story/StorySourceCode.vue +4 -1
  58. package/src/client/app/components/story/StoryVariantListItem.vue +6 -3
  59. package/src/client/app/components/story/StoryVariantSingleView.vue +14 -10
  60. package/src/client/app/components/story/StoryView.vue +10 -121
  61. package/src/client/app/components/story/StoryViewer.vue +175 -0
  62. package/src/client/app/stores/story.ts +6 -2
  63. package/src/client/app/style/{floating-vue.css → floating-vue.pcss} +0 -0
  64. package/src/client/app/style/{main.css → main.pcss} +13 -3
  65. package/src/client/app/style/transitions.pcss +26 -0
  66. package/src/client/app/types.ts +1 -1
  67. package/src/client/app/util/responsive.ts +3 -0
  68. package/src/node/bin.ts +8 -0
  69. package/src/node/build.ts +1 -1
  70. package/src/node/{collect.ts → collect/index.ts} +48 -30
  71. package/src/node/collect/worker.ts +61 -0
  72. package/src/node/commands/preview.ts +50 -0
  73. package/src/node/server.ts +1 -1
  74. package/src/node/types.ts +1 -1
  75. package/dist/client/app/style/transitions.css +0 -12
  76. package/src/client/app/style/transitions.css +0 -12
@@ -1,27 +1,19 @@
1
1
  <script lang="ts" setup>
2
- import { computed, watch } from 'vue'
2
+ import { watch } from 'vue'
3
3
  import { useRoute, useRouter } from 'vue-router'
4
4
  import { useStoryStore } from '../../stores/story'
5
5
 
6
6
  import BaseSplitPane from '../base/BaseSplitPane.vue'
7
7
  import BaseEmpty from '../base/BaseEmpty.vue'
8
- import BaseTab from '../base/BaseTab.vue'
9
- import StoryVariantListItem from './StoryVariantListItem.vue'
10
- import StoryVariantGridItem from './StoryVariantGridItem.vue'
11
- import StoryVariantSingleView from './StoryVariantSingleView.vue'
12
- import StoryControls from './StoryControls.vue'
13
- import StoryDocs from './StoryDocs.vue'
14
- import StorySourceCode from './StorySourceCode.vue'
8
+ import { isMobile } from '../../util/responsive'
9
+ import StoryViewer from './StoryViewer.vue'
10
+ import StorySidePanel from './StorySidePanel.vue'
15
11
 
16
12
  const storyStore = useStoryStore()
17
13
 
18
14
  const router = useRouter()
19
15
  const route = useRoute()
20
16
 
21
- // Selected variant
22
-
23
- const hasSingleVariant = computed(() => (storyStore.currentStory?.variants.length === 1))
24
-
25
17
  // Restore variant selection
26
18
 
27
19
  watch(() => storyStore.currentVariant, value => {
@@ -67,7 +59,11 @@ function setVariant (variantId: string) {
67
59
  v-else
68
60
  class="htw-h-full"
69
61
  >
62
+ <template v-if="isMobile">
63
+ <StoryViewer />
64
+ </template>
70
65
  <BaseSplitPane
66
+ v-else
71
67
  save-id="story-main"
72
68
  :min="30"
73
69
  :max="95"
@@ -75,118 +71,11 @@ function setVariant (variantId: string) {
75
71
  class="htw-h-full"
76
72
  >
77
73
  <template #first>
78
- <div class="htw-bg-gray-50 htw-h-full dark:htw-bg-gray-750">
79
- <div
80
- v-if="storyStore.currentStory.layout.type === 'grid'"
81
- class="htw-h-full htw-overflow-y-auto"
82
- >
83
- <div
84
- class="htw-grid htw-gap-4 htw-m-4"
85
- :style="{
86
- gridTemplateColumns: `repeat(auto-fill, ${storyStore.currentStory.layout.width ?? 200}px)`,
87
- }"
88
- >
89
- <StoryVariantGridItem
90
- v-for="(variant, index) of storyStore.currentStory.variants"
91
- :key="index"
92
- :variant="variant"
93
- :story="storyStore.currentStory"
94
- />
95
- </div>
96
- </div>
97
-
98
- <template v-else-if="storyStore.currentStory.layout.type === 'single'">
99
- <div
100
- v-if="hasSingleVariant && storyStore.currentVariant"
101
- class="htw-p-2 htw-h-full"
102
- >
103
- <StoryVariantSingleView
104
- :variant="storyStore.currentVariant"
105
- :story="storyStore.currentStory"
106
- />
107
- </div>
108
- <BaseSplitPane
109
- v-else
110
- save-id="story-single-main-split"
111
- :min="5"
112
- :max="40"
113
- :default-split="17"
114
- >
115
- <template #first>
116
- <div class="htw-h-full htw-overflow-y-auto">
117
- <StoryVariantListItem
118
- v-for="(variant, index) of storyStore.currentStory.variants"
119
- :key="index"
120
- :variant="variant"
121
- />
122
- </div>
123
- </template>
124
- <template #last>
125
- <div
126
- v-if="storyStore.currentVariant"
127
- class="htw-p-2 htw-h-full"
128
- >
129
- <StoryVariantSingleView
130
- :variant="storyStore.currentVariant"
131
- :story="storyStore.currentStory"
132
- />
133
- </div>
134
- </template>
135
- </BaseSplitPane>
136
- </template>
137
- </div>
74
+ <StoryViewer />
138
75
  </template>
139
76
 
140
77
  <template #last>
141
- <BaseEmpty v-if="!storyStore.currentVariant">
142
- <span>Select a variant</span>
143
- </BaseEmpty>
144
-
145
- <BaseEmpty v-else-if="!storyStore.currentVariant.ready">
146
- <span>Loading...</span>
147
- </BaseEmpty>
148
-
149
- <BaseSplitPane
150
- v-else
151
- save-id="story-sidepane"
152
- orientation="portrait"
153
- class="htw-h-full"
154
- >
155
- <template #first>
156
- <div class="htw-flex htw-flex-col htw-h-full">
157
- <nav class="htw-h-10 htw-flex-none htw-border-b htw-border-gray-100 dark:htw-border-gray-750">
158
- <BaseTab
159
- :to="{ ...$route, query: { ...$route.query, tab: '' } }"
160
- :matched="!$route.query.tab"
161
- >
162
- Controls
163
- </BaseTab>
164
- <BaseTab
165
- :to="{ ...$route, query: { ...$route.query, tab: 'docs' } }"
166
- :matched="$route.query.tab === 'docs'"
167
- >
168
- Docs
169
- </BaseTab>
170
- </nav>
171
-
172
- <component
173
- :is="$route.query.tab === 'docs'
174
- ? StoryDocs
175
- : StoryControls"
176
- :story="storyStore.currentStory"
177
- :variant="storyStore.currentVariant"
178
- class="htw-h-full htw-overflow-auto"
179
- />
180
- </div>
181
- </template>
182
-
183
- <template #last>
184
- <StorySourceCode
185
- :variant="storyStore.currentVariant"
186
- class="htw-h-full"
187
- />
188
- </template>
189
- </BaseSplitPane>
78
+ <StorySidePanel />
190
79
  </template>
191
80
  </BaseSplitPane>
192
81
  </div>
@@ -0,0 +1,175 @@
1
+ <script lang="ts" setup>
2
+ import { computed, ref, watch } from 'vue'
3
+ import { useRoute, useRouter } from 'vue-router'
4
+ import { useStoryStore } from '../../stores/story'
5
+
6
+ import BaseSplitPane from '../base/BaseSplitPane.vue'
7
+ import StoryVariantListItem from './StoryVariantListItem.vue'
8
+ import StoryVariantGridItem from './StoryVariantGridItem.vue'
9
+ import StoryVariantSingleView from './StoryVariantSingleView.vue'
10
+ import { isMobile } from '../../util/responsive'
11
+ import { Icon } from '@iconify/vue'
12
+ import MobileOverlay from '../app/MobileOverlay.vue'
13
+
14
+ const storyStore = useStoryStore()
15
+
16
+ const router = useRouter()
17
+ const route = useRoute()
18
+
19
+ const hasSingleVariant = computed(() => (storyStore.currentStory?.variants.length === 1))
20
+
21
+ const variant = computed(() => storyStore.currentVariant)
22
+
23
+ const isMenuOpened = ref(false)
24
+
25
+ function openMenu () {
26
+ isMenuOpened.value = true
27
+ }
28
+
29
+ function closeMenu () {
30
+ isMenuOpened.value = false
31
+ }
32
+
33
+ watch(variant, () => {
34
+ isMenuOpened.value = false
35
+ })
36
+
37
+ const templateWidth = computed(() => {
38
+ if (storyStore.currentStory.layout.type !== 'grid') {
39
+ return
40
+ }
41
+
42
+ const layoutWidth = storyStore.currentStory.layout.width
43
+
44
+ if (!layoutWidth) {
45
+ return '200px'
46
+ }
47
+
48
+ if (typeof layoutWidth === 'number') {
49
+ return layoutWidth + 'px'
50
+ }
51
+
52
+ return layoutWidth
53
+ })
54
+ </script>
55
+
56
+ <template>
57
+ <div class="htw-bg-gray-50 htw-h-full dark:htw-bg-gray-750">
58
+ <div
59
+ v-if="storyStore.currentStory.layout.type === 'grid'"
60
+ class="htw-h-full htw-overflow-y-auto"
61
+ >
62
+ <div
63
+ class="htw-grid htw-gap-4 htw-m-4"
64
+ :style="{
65
+ gridTemplateColumns: `repeat(auto-fill, ${templateWidth})`,
66
+ }"
67
+ >
68
+ <StoryVariantGridItem
69
+ v-for="(variant, index) of storyStore.currentStory.variants"
70
+ :key="index"
71
+ :variant="variant"
72
+ :story="storyStore.currentStory"
73
+ />
74
+ </div>
75
+ </div>
76
+
77
+ <template v-else-if="storyStore.currentStory.layout.type === 'single'">
78
+ <div
79
+ v-if="hasSingleVariant && storyStore.currentVariant"
80
+ class="htw-p-2 htw-h-full"
81
+ >
82
+ <StoryVariantSingleView
83
+ :variant="storyStore.currentVariant"
84
+ :story="storyStore.currentStory"
85
+ />
86
+ </div>
87
+ <template v-else>
88
+ <div
89
+ v-if="isMobile"
90
+ class="htw-divide-y htw-divide-gray-100 dark:htw-divide-gray-800 htw-h-full htw-flex htw-flex-col"
91
+ >
92
+ <a
93
+ class="htw-px-6 htw-h-12 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer htw-flex htw-gap-2 htw-flex-wrap htw-w-full htw-items-center"
94
+ @click="openMenu"
95
+ >
96
+ <template v-if="variant">
97
+ <Icon
98
+ :icon="variant.icon ?? 'carbon:cube'"
99
+ class="base-list-item-link-icon htw-w-5 htw-h-5 htw-flex-none"
100
+ :class="{
101
+ 'htw-text-gray-500': !variant.iconColor,
102
+ 'bind-icon-color': variant.iconColor,
103
+ }"
104
+ />
105
+ {{ variant.title }}
106
+ </template>
107
+ <template v-else>
108
+ Select a variant...
109
+ </template>
110
+
111
+ <Icon
112
+ icon="carbon:chevron-sort"
113
+ class="htw-w-5 htw-h-5 htw-shrink-0 htw-ml-auto"
114
+ />
115
+ </a>
116
+ <div
117
+ v-if="storyStore.currentVariant"
118
+ class="htw-p-2 htw-h-full"
119
+ >
120
+ <StoryVariantSingleView
121
+ :variant="storyStore.currentVariant"
122
+ :story="storyStore.currentStory"
123
+ />
124
+ </div>
125
+ </div>
126
+ <BaseSplitPane
127
+ v-else
128
+ save-id="story-single-main-split"
129
+ :min="5"
130
+ :max="40"
131
+ :default-split="17"
132
+ >
133
+ <template #first>
134
+ <div class="htw-h-full htw-overflow-y-auto">
135
+ <StoryVariantListItem
136
+ v-for="(variant, index) of storyStore.currentStory.variants"
137
+ :key="index"
138
+ :variant="variant"
139
+ />
140
+ </div>
141
+ </template>
142
+ <template #last>
143
+ <div
144
+ v-if="storyStore.currentVariant"
145
+ class="htw-p-2 htw-h-full"
146
+ >
147
+ <StoryVariantSingleView
148
+ :variant="storyStore.currentVariant"
149
+ :story="storyStore.currentStory"
150
+ />
151
+ </div>
152
+ </template>
153
+ </BaseSplitPane>
154
+ </template>
155
+ </template>
156
+ </div>
157
+
158
+ <MobileOverlay
159
+ title="Select a variant"
160
+ :opened="isMenuOpened"
161
+ @close="closeMenu"
162
+ >
163
+ <StoryVariantListItem
164
+ v-for="(variant, index) of storyStore.currentStory.variants"
165
+ :key="index"
166
+ :variant="variant"
167
+ />
168
+ </MobileOverlay>
169
+ </template>
170
+
171
+ <style scoped>
172
+ .bind-icon-color {
173
+ color: v-bind('variant?.iconColor');
174
+ }
175
+ </style>
@@ -1,7 +1,8 @@
1
1
  import { defineStore } from 'pinia'
2
- import { computed, reactive, ref, watch } from 'vue'
2
+ import { computed, ref, watch } from 'vue'
3
3
  import { router } from '../router'
4
4
  import { Story } from '../types'
5
+ import { useStorage } from '@vueuse/core'
5
6
 
6
7
  export const useStoryStore = defineStore('story', () => {
7
8
  const stories = ref<Story[]>([])
@@ -13,7 +14,10 @@ export const useStoryStore = defineStore('story', () => {
13
14
 
14
15
  const currentVariant = computed(() => currentStory.value?.variants.find(v => v.id === router.currentRoute.value.query.variantId))
15
16
 
16
- const openedFolders = ref(new Map() as Map<string, boolean>)
17
+ const openedFolders = useStorage<Map<string, boolean>>(
18
+ '_histoire-tree-state',
19
+ new Map(),
20
+ )
17
21
 
18
22
  function getStringPath (path: Array<string>) {
19
23
  return path.join('␜')
@@ -1,7 +1,8 @@
1
1
  @import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
2
2
  @import 'floating-vue/dist/style.css';
3
- @import 'floating-vue.css';
4
- @import 'transitions.css';
3
+
4
+ @import 'floating-vue.pcss';
5
+ @import 'transitions.pcss';
5
6
 
6
7
  *,
7
8
  ::before,
@@ -45,7 +46,16 @@ body,
45
46
  #app {
46
47
  height: 100%;
47
48
  font-family: 'Lato', sans-serif;
48
- font-size: 14px;
49
+ }
50
+
51
+ body {
52
+ font-size: 18px;
53
+ }
54
+
55
+ @media (min-width: 640px) {
56
+ body {
57
+ font-size: 14px;
58
+ }
49
59
  }
50
60
 
51
61
  .__histoire-sandbox {
@@ -0,0 +1,26 @@
1
+ .__histoire-scale-x {
2
+ &-enter-active,
3
+ &-leave-active {
4
+ transition: transform .15s, opacity .15s;
5
+ }
6
+
7
+ &-enter-from,
8
+ &-leave-to {
9
+ transform: scaleX(0);
10
+ opacity: 0;
11
+ }
12
+ }
13
+
14
+ .__histoire-fade {
15
+ &-enter-active,
16
+ &-leave-active {
17
+ transition: transform .15s, opacity .15s;
18
+ transform: translateY(0px);
19
+ }
20
+
21
+ &-enter-from,
22
+ &-leave-to {
23
+ transform: translateY(32px);
24
+ opacity: 0;
25
+ }
26
+ }
@@ -16,7 +16,7 @@ export interface Story {
16
16
  type: 'single'
17
17
  } | {
18
18
  type: 'grid'
19
- width: number
19
+ width?: number | string
20
20
  }
21
21
  icon?: string
22
22
  iconColor?: string
@@ -0,0 +1,3 @@
1
+ import { useMediaQuery } from '@vueuse/core'
2
+
3
+ export const isMobile = useMediaQuery('(max-width: 640px)')
package/src/node/bin.ts CHANGED
@@ -25,4 +25,12 @@ program.command('build')
25
25
  return buildCommand()
26
26
  })
27
27
 
28
+ program.command('preview')
29
+ .describe('preview the built directory')
30
+ .option('-p, --port <port>', 'Listening port of the server')
31
+ .action(async (options) => {
32
+ const { previewCommand } = await import('./commands/preview.js')
33
+ return previewCommand(options)
34
+ })
35
+
28
36
  program.parse(process.argv)
package/src/node/build.ts CHANGED
@@ -9,7 +9,7 @@ import { Context } from './context.js'
9
9
  import { createVitePlugins } from './plugin.js'
10
10
  import { findAllStories } from './stories.js'
11
11
  import type { RollupOutput } from 'rollup'
12
- import { useCollectStories } from './collect.js'
12
+ import { useCollectStories } from './collect/index.js'
13
13
 
14
14
  const PRELOAD_MODULES = [
15
15
  'vendor',
@@ -1,15 +1,15 @@
1
- import { fileURLToPath } from 'url'
1
+ import { MessageChannel } from 'worker_threads'
2
2
  import type { ViteDevServer } from 'vite'
3
3
  import { ViteNodeServer } from 'vite-node/server'
4
- import { ViteNodeRunner } from 'vite-node/client'
5
- import { dirname, resolve, relative } from 'pathe'
4
+ import type { FetchFunction, ResolveIdFunction } from 'vite-node'
5
+ import { relative } from 'pathe'
6
6
  import pc from 'picocolors'
7
- import type { StoryFile, Story } from './types.js'
8
- import { createDomEnv } from './dom/env.js'
9
- import { createPath, TreeFile } from './tree.js'
10
- import type { Context } from './context.js'
11
-
12
- const __dirname = dirname(fileURLToPath(import.meta.url))
7
+ import Tinypool from 'tinypool'
8
+ import { createBirpc } from 'birpc'
9
+ import type { StoryFile } from '../types.js'
10
+ import { createPath, TreeFile } from '../tree.js'
11
+ import type { Context } from '../context.js'
12
+ import type { Payload, ReturnData } from './worker.js'
13
13
 
14
14
  export interface UseCollectStoriesOptions {
15
15
  server: ViteDevServer
@@ -18,7 +18,6 @@ export interface UseCollectStoriesOptions {
18
18
 
19
19
  export function useCollectStories (options: UseCollectStoriesOptions, ctx: Context) {
20
20
  const { server } = options
21
- const { destroy: destroyDomEnv } = createDomEnv()
22
21
 
23
22
  const node = new ViteNodeServer(server, {
24
23
  deps: {
@@ -27,33 +26,52 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
27
26
  ],
28
27
  },
29
28
  })
30
- const runner = new ViteNodeRunner({
31
- root: server.config.root,
32
- base: server.config.base,
33
- fetchModule (id) {
34
- // Alias
35
- if (id.endsWith('vue.runtime.esm-bundler.js')) {
36
- id = 'vue'
37
- }
38
- return node.fetchModule(id)
39
- },
40
- resolveId (id, importer) {
41
- return node.resolveId(id, importer)
42
- },
29
+
30
+ const threadPool = new Tinypool({
31
+ filename: new URL('./worker.js', import.meta.url).href,
32
+ // WebContainer compatibility (Stackblitz)
33
+ useAtomics: false,
43
34
  })
44
35
 
45
36
  function clearCache () {
46
37
  node.fetchCache.clear()
47
- runner.moduleCache.clear()
38
+ }
39
+
40
+ function createChannel () {
41
+ const channel = new MessageChannel()
42
+ const port = channel.port2
43
+ const workerPort = channel.port1
44
+
45
+ createBirpc<Record<string, never>, {
46
+ fetchModule: FetchFunction
47
+ resolveId: ResolveIdFunction
48
+ }>({
49
+ fetchModule: (id) => node.fetchModule(id),
50
+ resolveId: (id, importer) => node.resolveId(id, importer),
51
+ }, {
52
+ post: data => port.postMessage(data),
53
+ on: data => port.on('message', data),
54
+ })
55
+
56
+ return {
57
+ port,
58
+ workerPort,
59
+ }
48
60
  }
49
61
 
50
62
  async function executeStoryFile (storyFile: StoryFile) {
51
63
  try {
52
- // Mount app to collect stories/variants
53
- const el = window.document.createElement('div')
54
- const { run } = await runner.executeFile(resolve(__dirname, '../client/server/index.js'))
55
- const storyData: Story[] = []
56
- await run(storyFile, storyData, el)
64
+ const { workerPort } = createChannel()
65
+ const { storyData } = await threadPool.run({
66
+ root: server.config.root,
67
+ base: server.config.base,
68
+ storyFile,
69
+ port: workerPort,
70
+ } as Payload, {
71
+ transferList: [
72
+ workerPort,
73
+ ],
74
+ }) as ReturnData
57
75
  if (storyData.length === 0) {
58
76
  console.warn(pc.yellow(`⚠️ No story found for ${storyFile.path}`))
59
77
  return
@@ -76,7 +94,7 @@ export function useCollectStories (options: UseCollectStoriesOptions, ctx: Conte
76
94
  }
77
95
 
78
96
  async function destroy () {
79
- destroyDomEnv()
97
+ await threadPool.destroy()
80
98
  }
81
99
 
82
100
  return {
@@ -0,0 +1,61 @@
1
+ import { fileURLToPath } from 'url'
2
+ import type { MessagePort } from 'worker_threads'
3
+ import { ViteNodeRunner } from 'vite-node/client'
4
+ import { createBirpc } from 'birpc'
5
+ import type { FetchFunction, ResolveIdFunction } from 'vite-node'
6
+ import { dirname, resolve } from 'pathe'
7
+ import { createDomEnv } from '../dom/env.js'
8
+ import type { StoryFile, Story } from '../types.js'
9
+
10
+ const __dirname = dirname(fileURLToPath(import.meta.url))
11
+
12
+ export interface Payload {
13
+ root: string
14
+ base: string
15
+ port: MessagePort
16
+ storyFile: StoryFile
17
+ }
18
+
19
+ export interface ReturnData {
20
+ storyData: Story[]
21
+ }
22
+
23
+ export default async (payload: Payload): Promise<ReturnData> => {
24
+ const rpc = createBirpc<{
25
+ fetchModule: FetchFunction
26
+ resolveId: ResolveIdFunction
27
+ }>({}, {
28
+ post: data => payload.port.postMessage(data),
29
+ on: data => payload.port.on('message', data),
30
+ })
31
+
32
+ const runner = new ViteNodeRunner({
33
+ root: payload.root,
34
+ base: payload.base,
35
+ fetchModule (id) {
36
+ // Alias
37
+ if (id.endsWith('vue.runtime.esm-bundler.js')) {
38
+ id = 'vue'
39
+ }
40
+ return rpc.fetchModule(id)
41
+ },
42
+ resolveId (id, importer) {
43
+ return rpc.resolveId(id, importer)
44
+ },
45
+ })
46
+
47
+ const { destroy: destroyDomEnv } = createDomEnv()
48
+
49
+ const el = window.document.createElement('div')
50
+
51
+ // Mount app to collect stories/variants
52
+ const { run } = await runner.executeFile(resolve(__dirname, '../../client/server/index.js'))
53
+ const storyData: Story[] = []
54
+ await run(payload.storyFile, storyData, el)
55
+
56
+ destroyDomEnv()
57
+
58
+ return {
59
+ storyData,
60
+ }
61
+ }
@@ -0,0 +1,50 @@
1
+ import http from 'http'
2
+ import { resolveConfig as resolveViteConfig } from 'vite'
3
+ import connect from 'connect'
4
+ import sirv from 'sirv'
5
+ import pc from 'picocolors'
6
+ import { createContext } from '../context.js'
7
+ import { createVitePlugins } from '../plugin.js'
8
+
9
+ export interface PreviewOptions {
10
+ port?: number
11
+ }
12
+
13
+ export async function previewCommand (options: PreviewOptions) {
14
+ const ctx = await createContext({
15
+ mode: 'build',
16
+ })
17
+ const viteConfig = await resolveViteConfig({
18
+ plugins: await createVitePlugins(ctx),
19
+ }, 'build')
20
+
21
+ const app = connect()
22
+
23
+ app.use(
24
+ viteConfig.base,
25
+ sirv(ctx.config.outDir, {
26
+ dev: true,
27
+ etag: true,
28
+ single: true,
29
+ }),
30
+ )
31
+
32
+ let port = options.port ?? 3000
33
+
34
+ const httpServer = http.createServer(app)
35
+
36
+ function onError (e: Error & { code?: string }) {
37
+ if (e.code === 'EADDRINUSE') {
38
+ httpServer.listen(++port)
39
+ } else {
40
+ throw e
41
+ }
42
+ }
43
+
44
+ httpServer.on('error', onError)
45
+
46
+ httpServer.listen(port, () => {
47
+ httpServer.off('error', onError)
48
+ console.log(`Preview server listening on ${pc.cyan(`http://localhost:${port}`)}`)
49
+ })
50
+ }