histoire 0.1.5 → 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 (53) hide show
  1. package/dist/client/app/components/app/AppHeader.vue +1 -0
  2. package/dist/client/app/components/base/BaseCheckbox.vue +76 -0
  3. package/dist/client/app/components/search/SearchItem.vue +4 -1
  4. package/dist/client/app/components/search/SearchModal.vue +4 -4
  5. package/dist/client/app/components/story/StoryControls.vue +1 -1
  6. package/dist/client/app/components/story/StoryDocs.vue +1 -0
  7. package/dist/client/app/components/story/StoryListFolder.vue +1 -1
  8. package/dist/client/app/components/story/StoryListItem.vue +4 -1
  9. package/dist/client/app/components/story/StoryResponsivePreview.vue +1 -0
  10. package/dist/client/app/components/story/StorySidePanel.vue +1 -0
  11. package/dist/client/app/components/story/StorySourceCode.vue +4 -1
  12. package/dist/client/app/components/story/StoryVariantListItem.vue +4 -1
  13. package/dist/client/app/components/story/StoryVariantSingleView.vue +9 -9
  14. package/dist/client/app/components/story/StoryViewer.vue +19 -1
  15. package/dist/client/app/stores/story.d.ts +6 -6
  16. package/dist/client/app/style/main.pcss +5 -5
  17. package/dist/client/app/types.d.ts +1 -1
  18. package/dist/client/app/types.ts +1 -1
  19. package/dist/node/bin.js +7 -0
  20. package/dist/node/build.js +1 -1
  21. package/dist/node/{collect.d.ts → collect/index.d.ts} +2 -2
  22. package/dist/node/{collect.js → collect/index.js} +37 -27
  23. package/dist/node/collect/worker.d.ts +14 -0
  24. package/dist/node/collect/worker.js +36 -0
  25. package/dist/node/commands/preview.d.ts +4 -0
  26. package/dist/node/commands/preview.js +36 -0
  27. package/dist/node/server.js +1 -1
  28. package/dist/node/types.d.ts +1 -1
  29. package/dist/style.css +86 -50
  30. package/package.json +7 -3
  31. package/src/client/app/components/app/AppHeader.vue +1 -0
  32. package/src/client/app/components/base/BaseCheckbox.vue +76 -0
  33. package/src/client/app/components/search/SearchItem.vue +4 -1
  34. package/src/client/app/components/search/SearchModal.vue +4 -4
  35. package/src/client/app/components/story/StoryControls.vue +1 -1
  36. package/src/client/app/components/story/StoryDocs.vue +1 -0
  37. package/src/client/app/components/story/StoryListFolder.vue +1 -1
  38. package/src/client/app/components/story/StoryListItem.vue +4 -1
  39. package/src/client/app/components/story/StoryResponsivePreview.vue +1 -0
  40. package/src/client/app/components/story/StorySidePanel.vue +1 -0
  41. package/src/client/app/components/story/StorySourceCode.vue +4 -1
  42. package/src/client/app/components/story/StoryVariantListItem.vue +4 -1
  43. package/src/client/app/components/story/StoryVariantSingleView.vue +9 -9
  44. package/src/client/app/components/story/StoryViewer.vue +19 -1
  45. package/src/client/app/style/main.pcss +5 -5
  46. package/src/client/app/types.ts +1 -1
  47. package/src/node/bin.ts +8 -0
  48. package/src/node/build.ts +1 -1
  49. package/src/node/{collect.ts → collect/index.ts} +48 -30
  50. package/src/node/collect/worker.ts +61 -0
  51. package/src/node/commands/preview.ts +50 -0
  52. package/src/node/server.ts +1 -1
  53. package/src/node/types.ts +1 -1
@@ -40,6 +40,7 @@ onKeyboardShortcut(['ctrl+shift+d', 'meta+shift+d'], (event) => {
40
40
  <a
41
41
  v-tooltip="makeTooltip('Search', ({ isMac }) => isMac ? 'meta+k' : 'ctrl+k')"
42
42
  class="htw-p-2 sm:htw-p-1 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-cursor-pointer"
43
+ data-test-id="search-btn"
43
44
  @click="$emit('search')"
44
45
  >
45
46
  <Icon
@@ -0,0 +1,76 @@
1
+ <script lang="ts" setup>
2
+ import { computed, ref, watch } from 'vue'
3
+
4
+ const props = defineProps({
5
+ modelValue: {
6
+ type: Boolean,
7
+ default: false,
8
+ },
9
+ })
10
+
11
+ const emit = defineEmits({
12
+ 'update:modelValue': (newValue: boolean) => true,
13
+ })
14
+
15
+ function toggle () {
16
+ emit('update:modelValue', !props.modelValue)
17
+ animationEnabled.value = true
18
+ }
19
+
20
+ // SVG check
21
+
22
+ const path = ref<SVGPathElement>()
23
+ const dasharray = ref(0)
24
+ const progress = computed(() => props.modelValue ? 1 : 0)
25
+ const dashoffset = computed(() => (1 - progress.value) * dasharray.value)
26
+ const animationEnabled = ref(false)
27
+
28
+ watch(path, value => {
29
+ dasharray.value = path.value.getTotalLength?.() ?? 21.21
30
+ })
31
+ </script>
32
+
33
+ <template>
34
+ <div
35
+ role="checkbox"
36
+ tabindex="0"
37
+ class="htw-flex htw-items-center htw-gap-2 htw-select-none htw-px-4 htw-py-3 htw-cursor-pointer hover:htw-bg-primary-100 dark:hover:htw-bg-primary-700"
38
+ @click="toggle()"
39
+ @keydown.enter.prevent="toggle()"
40
+ @keydown.space.prevent="toggle()"
41
+ >
42
+ <div class="htw-text-white htw-w-[16px] htw-h-[16px] htw-relative">
43
+ <div
44
+ class="htw-border group-active:htw-bg-gray-500/20 htw-rounded-sm htw-box-border htw-absolute htw-inset-0 htw-transition-border htw-duration-150 htw-ease-out"
45
+ :class="[
46
+ modelValue
47
+ ? 'htw-border-primary-500 htw-border-8'
48
+ : 'htw-border-black/25 dark:htw-border-white/25 htw-delay-150',
49
+ ]"
50
+ />
51
+ <svg
52
+ width="16"
53
+ height="16"
54
+ viewBox="0 0 24 24"
55
+ class="htw-relative htw-z-10"
56
+ >
57
+ <path
58
+ ref="path"
59
+ d="m 4 12 l 5 5 l 10 -10"
60
+ fill="none"
61
+ class="htw-stroke-white htw-stroke-2 htw-duration-200 htw-ease-in-out"
62
+ :class="[
63
+ animationEnabled ? 'htw-transition-all' : 'htw-transition-none',
64
+ {
65
+ 'htw-delay-150': modelValue,
66
+ },
67
+ ]"
68
+ :stroke-dasharray="dasharray"
69
+ :stroke-dashoffset="dashoffset"
70
+ />
71
+ </svg>
72
+ </div>
73
+
74
+ <slot />
75
+ </div>
76
+ </template>
@@ -48,7 +48,10 @@ const kindLabels = {
48
48
  </script>
49
49
 
50
50
  <template>
51
- <div ref="el">
51
+ <div
52
+ ref="el"
53
+ data-test-id="search-item"
54
+ >
52
55
  <BaseListItemLink
53
56
  :to="result.route"
54
57
  :is-active="selected"
@@ -25,8 +25,7 @@ function close () {
25
25
  // Autofocus
26
26
 
27
27
  const input = ref<HTMLInputElement>()
28
- const { focused } = useFocus({
29
- target: input,
28
+ const { focused } = useFocus(input, {
30
29
  initialValue: true,
31
30
  })
32
31
 
@@ -132,6 +131,7 @@ function selectPrevious () {
132
131
  <div
133
132
  v-show="shown"
134
133
  class="htw-fixed htw-inset-0 htw-bg-white/80 dark:htw-bg-gray-700/80 htw-z-20"
134
+ data-test-id="search-modal"
135
135
  >
136
136
  <div
137
137
  class="htw-absolute htw-inset-0"
@@ -152,8 +152,8 @@ function selectPrevious () {
152
152
  v-model="search"
153
153
  placeholder="Search for stories, variants..."
154
154
  class="htw-bg-transparent htw-w-full htw-flex-1 htw-pl-0 htw-pr-6 htw-py-4 htw-outline-none"
155
- @keydown.down="selectNext()"
156
- @keydown.up="selectPrevious()"
155
+ @keydown.down.prevent="selectNext()"
156
+ @keydown.up.prevent="selectPrevious()"
157
157
  @keydown.escape="close()"
158
158
  >
159
159
  </div>
@@ -18,7 +18,7 @@ defineProps({
18
18
  </script>
19
19
 
20
20
  <template>
21
- <div>
21
+ <div data-test-id="story-controls">
22
22
  <!-- Custom controls -->
23
23
  <SandboxVue3
24
24
  v-if="variant.slots().controls"
@@ -32,6 +32,7 @@ watchEffect(async () => {
32
32
  <div
33
33
  v-else
34
34
  class="htw-prose dark:htw-prose-invert htw-p-4 htw-max-w-none"
35
+ data-test-id="story-docs"
35
36
  v-html="renderedDoc"
36
37
  />
37
38
  </template>
@@ -30,7 +30,7 @@ const folderPadding = computed(() => {
30
30
  </script>
31
31
 
32
32
  <template>
33
- <div class="toast">
33
+ <div data-test-id="story-list-folder">
34
34
  <div
35
35
  role="button"
36
36
  tabindex="0"
@@ -24,7 +24,10 @@ useScrollOnActive(active, el)
24
24
  </script>
25
25
 
26
26
  <template>
27
- <div ref="el">
27
+ <div
28
+ ref="el"
29
+ data-test-id="story-list-item"
30
+ >
28
31
  <BaseListItemLink
29
32
  v-slot="{ active }"
30
33
  :to="{
@@ -255,6 +255,7 @@ const finalHeight = computed(() => props.settings.rotate ? props.settings.respon
255
255
  'htw-invisible': !isIframeLoaded,
256
256
  'htw-pointer-events-none': resizing,
257
257
  }"
258
+ data-test-id="preview-iframe"
258
259
  @load="onIframeLoad()"
259
260
  />
260
261
  </div>
@@ -29,6 +29,7 @@ const route = useRoute()
29
29
  save-id="story-sidepane"
30
30
  orientation="portrait"
31
31
  class="htw-h-full"
32
+ data-test-id="story-side-panel"
32
33
  >
33
34
  <template #first>
34
35
  <div class="htw-flex htw-flex-col htw-h-full">
@@ -67,7 +67,10 @@ const copySourceCode = () => copy(sourceCode.value)
67
67
  </script>
68
68
 
69
69
  <template>
70
- <div class="htw-bg-gray-100 dark:htw-bg-gray-800 htw-h-full htw-overflow-hidden htw-relative">
70
+ <div
71
+ class="htw-bg-gray-100 dark:htw-bg-gray-800 htw-h-full htw-overflow-hidden htw-relative"
72
+ data-test-id="story-source-code"
73
+ >
71
74
  <div
72
75
  v-if="error"
73
76
  class="htw-text-red-500 htw-h-full htw-p-2 htw-overflow-auto htw-font-mono htw-text-sm"
@@ -20,7 +20,10 @@ useScrollOnActive(isActive, el)
20
20
  </script>
21
21
 
22
22
  <template>
23
- <div ref="el">
23
+ <div
24
+ ref="el"
25
+ data-test-id="story-variant-list-item"
26
+ >
24
27
  <BaseListItemLink
25
28
  v-slot="{ active }"
26
29
  :to="targetRoute"
@@ -5,6 +5,7 @@ import type { Story, Variant } from '../../types'
5
5
  import { histoireConfig } from '../../util/config'
6
6
  import { usePreviewSettings } from '../../util/preview-settings'
7
7
  import StoryResponsivePreview from './StoryResponsivePreview.vue'
8
+ import BaseCheckbox from '../base/BaseCheckbox.vue'
8
9
  import { isMobile } from '../../util/responsive'
9
10
 
10
11
  defineProps({
@@ -23,7 +24,10 @@ const settings = usePreviewSettings()
23
24
  </script>
24
25
 
25
26
  <template>
26
- <div class="htw-h-full htw-flex htw-flex-col">
27
+ <div
28
+ class="htw-h-full htw-flex htw-flex-col"
29
+ data-test-id="story-variant-single-view"
30
+ >
27
31
  <!-- Toolbar -->
28
32
  <div
29
33
  v-if="!isMobile"
@@ -67,11 +71,9 @@ const settings = usePreviewSettings()
67
71
 
68
72
  <template #popper="{ hide }">
69
73
  <div class="htw-flex htw-flex-col htw-items-stretch">
70
- <HstCheckbox
71
- v-model="settings.rotate"
72
- >
74
+ <BaseCheckbox v-model="settings.rotate">
73
75
  Rotate
74
- </HstCheckbox>
76
+ </BaseCheckbox>
75
77
 
76
78
  <div class="htw-flex htw-gap-2 htw-px-4 htw-py-3">
77
79
  <input
@@ -137,11 +139,9 @@ const settings = usePreviewSettings()
137
139
 
138
140
  <template #popper="{ hide }">
139
141
  <div class="htw-flex htw-flex-col htw-items-stretch">
140
- <HstCheckbox
141
- v-model="settings.checkerboard"
142
- >
142
+ <BaseCheckbox v-model="settings.checkerboard">
143
143
  Checkerboard
144
- </HstCheckbox>
144
+ </BaseCheckbox>
145
145
 
146
146
  <button
147
147
  v-for="(option, index) in histoireConfig.backgroundPresets"
@@ -33,6 +33,24 @@ function closeMenu () {
33
33
  watch(variant, () => {
34
34
  isMenuOpened.value = false
35
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
+ })
36
54
  </script>
37
55
 
38
56
  <template>
@@ -44,7 +62,7 @@ watch(variant, () => {
44
62
  <div
45
63
  class="htw-grid htw-gap-4 htw-m-4"
46
64
  :style="{
47
- gridTemplateColumns: `repeat(auto-fill, ${storyStore.currentStory.layout.width ?? 200}px)`,
65
+ gridTemplateColumns: `repeat(auto-fill, ${templateWidth})`,
48
66
  }"
49
67
  >
50
68
  <StoryVariantGridItem
@@ -18,7 +18,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
18
18
  type: "single";
19
19
  } | {
20
20
  type: "grid";
21
- width: number;
21
+ width?: string | number;
22
22
  };
23
23
  icon?: string;
24
24
  iconColor?: string;
@@ -60,7 +60,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
60
60
  type: "single";
61
61
  } | {
62
62
  type: "grid";
63
- width: number;
63
+ width?: string | number;
64
64
  };
65
65
  icon?: string;
66
66
  iconColor?: string;
@@ -115,7 +115,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
115
115
  type: "single";
116
116
  } | {
117
117
  type: "grid";
118
- width: number;
118
+ width?: string | number;
119
119
  };
120
120
  icon?: string;
121
121
  iconColor?: string;
@@ -157,7 +157,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
157
157
  type: "single";
158
158
  } | {
159
159
  type: "grid";
160
- width: number;
160
+ width?: string | number;
161
161
  };
162
162
  icon?: string;
163
163
  iconColor?: string;
@@ -212,7 +212,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
212
212
  type: "single";
213
213
  } | {
214
214
  type: "grid";
215
- width: number;
215
+ width?: string | number;
216
216
  };
217
217
  icon?: string;
218
218
  iconColor?: string;
@@ -254,7 +254,7 @@ export declare const useStoryStore: import("pinia").StoreDefinition<"story", imp
254
254
  type: "single";
255
255
  } | {
256
256
  type: "grid";
257
- width: number;
257
+ width?: string | number;
258
258
  };
259
259
  icon?: string;
260
260
  iconColor?: string;
@@ -48,14 +48,14 @@ body,
48
48
  font-family: 'Lato', sans-serif;
49
49
  }
50
50
 
51
- #app {
52
- font-size: 18px;
51
+ body {
52
+ font-size: 18px;
53
53
  }
54
54
 
55
55
  @media (min-width: 640px) {
56
- #app {
57
- font-size: 14px;
58
- }
56
+ body {
57
+ font-size: 14px;
58
+ }
59
59
  }
60
60
 
61
61
  .__histoire-sandbox {
@@ -14,7 +14,7 @@ export interface Story {
14
14
  type: 'single';
15
15
  } | {
16
16
  type: 'grid';
17
- width: number;
17
+ width?: number | string;
18
18
  };
19
19
  icon?: string;
20
20
  iconColor?: string;
@@ -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
package/dist/node/bin.js CHANGED
@@ -19,4 +19,11 @@ program.command('build')
19
19
  const { buildCommand } = await import('./commands/build.js');
20
20
  return buildCommand();
21
21
  });
22
+ program.command('preview')
23
+ .describe('preview the built directory')
24
+ .option('-p, --port <port>', 'Listening port of the server')
25
+ .action(async (options) => {
26
+ const { previewCommand } = await import('./commands/preview.js');
27
+ return previewCommand(options);
28
+ });
22
29
  program.parse(process.argv);
@@ -7,7 +7,7 @@ import { performance } from 'perf_hooks';
7
7
  import { APP_PATH } from './alias.js';
8
8
  import { createVitePlugins } from './plugin.js';
9
9
  import { findAllStories } from './stories.js';
10
- import { useCollectStories } from './collect.js';
10
+ import { useCollectStories } from './collect/index.js';
11
11
  const PRELOAD_MODULES = [
12
12
  'vendor',
13
13
  ];
@@ -1,6 +1,6 @@
1
1
  import type { ViteDevServer } from 'vite';
2
- import type { StoryFile } from './types.js';
3
- import type { Context } from './context.js';
2
+ import type { StoryFile } from '../types.js';
3
+ import type { Context } from '../context.js';
4
4
  export interface UseCollectStoriesOptions {
5
5
  server: ViteDevServer;
6
6
  throws?: boolean;
@@ -1,14 +1,12 @@
1
- import { fileURLToPath } from 'url';
1
+ import { MessageChannel } from 'worker_threads';
2
2
  import { ViteNodeServer } from 'vite-node/server';
3
- import { ViteNodeRunner } from 'vite-node/client';
4
- import { dirname, resolve, relative } from 'pathe';
3
+ import { relative } from 'pathe';
5
4
  import pc from 'picocolors';
6
- import { createDomEnv } from './dom/env.js';
7
- import { createPath } from './tree.js';
8
- const __dirname = dirname(fileURLToPath(import.meta.url));
5
+ import Tinypool from 'tinypool';
6
+ import { createBirpc } from 'birpc';
7
+ import { createPath } from '../tree.js';
9
8
  export function useCollectStories(options, ctx) {
10
9
  const { server } = options;
11
- const { destroy: destroyDomEnv } = createDomEnv();
12
10
  const node = new ViteNodeServer(server, {
13
11
  deps: {
14
12
  inline: [
@@ -16,31 +14,43 @@ export function useCollectStories(options, ctx) {
16
14
  ],
17
15
  },
18
16
  });
19
- const runner = new ViteNodeRunner({
20
- root: server.config.root,
21
- base: server.config.base,
22
- fetchModule(id) {
23
- // Alias
24
- if (id.endsWith('vue.runtime.esm-bundler.js')) {
25
- id = 'vue';
26
- }
27
- return node.fetchModule(id);
28
- },
29
- resolveId(id, importer) {
30
- return node.resolveId(id, importer);
31
- },
17
+ const threadPool = new Tinypool({
18
+ filename: new URL('./worker.js', import.meta.url).href,
19
+ // WebContainer compatibility (Stackblitz)
20
+ useAtomics: false,
32
21
  });
33
22
  function clearCache() {
34
23
  node.fetchCache.clear();
35
- runner.moduleCache.clear();
24
+ }
25
+ function createChannel() {
26
+ const channel = new MessageChannel();
27
+ const port = channel.port2;
28
+ const workerPort = channel.port1;
29
+ createBirpc({
30
+ fetchModule: (id) => node.fetchModule(id),
31
+ resolveId: (id, importer) => node.resolveId(id, importer),
32
+ }, {
33
+ post: data => port.postMessage(data),
34
+ on: data => port.on('message', data),
35
+ });
36
+ return {
37
+ port,
38
+ workerPort,
39
+ };
36
40
  }
37
41
  async function executeStoryFile(storyFile) {
38
42
  try {
39
- // Mount app to collect stories/variants
40
- const el = window.document.createElement('div');
41
- const { run } = await runner.executeFile(resolve(__dirname, '../client/server/index.js'));
42
- const storyData = [];
43
- await run(storyFile, storyData, el);
43
+ const { workerPort } = createChannel();
44
+ const { storyData } = await threadPool.run({
45
+ root: server.config.root,
46
+ base: server.config.base,
47
+ storyFile,
48
+ port: workerPort,
49
+ }, {
50
+ transferList: [
51
+ workerPort,
52
+ ],
53
+ });
44
54
  if (storyData.length === 0) {
45
55
  console.warn(pc.yellow(`⚠️ No story found for ${storyFile.path}`));
46
56
  return;
@@ -64,7 +74,7 @@ export function useCollectStories(options, ctx) {
64
74
  }
65
75
  }
66
76
  async function destroy() {
67
- destroyDomEnv();
77
+ await threadPool.destroy();
68
78
  }
69
79
  return {
70
80
  clearCache,
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import type { MessagePort } from 'worker_threads';
3
+ import type { StoryFile, Story } from '../types.js';
4
+ export interface Payload {
5
+ root: string;
6
+ base: string;
7
+ port: MessagePort;
8
+ storyFile: StoryFile;
9
+ }
10
+ export interface ReturnData {
11
+ storyData: Story[];
12
+ }
13
+ declare const _default: (payload: Payload) => Promise<ReturnData>;
14
+ export default _default;
@@ -0,0 +1,36 @@
1
+ import { fileURLToPath } from 'url';
2
+ import { ViteNodeRunner } from 'vite-node/client';
3
+ import { createBirpc } from 'birpc';
4
+ import { dirname, resolve } from 'pathe';
5
+ import { createDomEnv } from '../dom/env.js';
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+ export default async (payload) => {
8
+ const rpc = createBirpc({}, {
9
+ post: data => payload.port.postMessage(data),
10
+ on: data => payload.port.on('message', data),
11
+ });
12
+ const runner = new ViteNodeRunner({
13
+ root: payload.root,
14
+ base: payload.base,
15
+ fetchModule(id) {
16
+ // Alias
17
+ if (id.endsWith('vue.runtime.esm-bundler.js')) {
18
+ id = 'vue';
19
+ }
20
+ return rpc.fetchModule(id);
21
+ },
22
+ resolveId(id, importer) {
23
+ return rpc.resolveId(id, importer);
24
+ },
25
+ });
26
+ const { destroy: destroyDomEnv } = createDomEnv();
27
+ const el = window.document.createElement('div');
28
+ // Mount app to collect stories/variants
29
+ const { run } = await runner.executeFile(resolve(__dirname, '../../client/server/index.js'));
30
+ const storyData = [];
31
+ await run(payload.storyFile, storyData, el);
32
+ destroyDomEnv();
33
+ return {
34
+ storyData,
35
+ };
36
+ };
@@ -0,0 +1,4 @@
1
+ export interface PreviewOptions {
2
+ port?: number;
3
+ }
4
+ export declare function previewCommand(options: PreviewOptions): Promise<void>;
@@ -0,0 +1,36 @@
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
+ export async function previewCommand(options) {
9
+ const ctx = await createContext({
10
+ mode: 'build',
11
+ });
12
+ const viteConfig = await resolveViteConfig({
13
+ plugins: await createVitePlugins(ctx),
14
+ }, 'build');
15
+ const app = connect();
16
+ app.use(viteConfig.base, sirv(ctx.config.outDir, {
17
+ dev: true,
18
+ etag: true,
19
+ single: true,
20
+ }));
21
+ let port = options.port ?? 3000;
22
+ const httpServer = http.createServer(app);
23
+ function onError(e) {
24
+ if (e.code === 'EADDRINUSE') {
25
+ httpServer.listen(++port);
26
+ }
27
+ else {
28
+ throw e;
29
+ }
30
+ }
31
+ httpServer.on('error', onError);
32
+ httpServer.listen(port, () => {
33
+ httpServer.off('error', onError);
34
+ console.log(`Preview server listening on ${pc.cyan(`http://localhost:${port}`)}`);
35
+ });
36
+ }
@@ -1,7 +1,7 @@
1
1
  import { createServer as createViteServer } from 'vite';
2
2
  import { createVitePlugins, RESOLVED_STORIES_ID } from './plugin.js';
3
3
  import { notifyStoryChange, onStoryChange, watchStories } from './stories.js';
4
- import { useCollectStories } from './collect.js';
4
+ import { useCollectStories } from './collect/index.js';
5
5
  export async function createServer(ctx, port) {
6
6
  await watchStories(ctx);
7
7
  const server = await createViteServer({
@@ -25,7 +25,7 @@ export interface Story {
25
25
  type: 'single';
26
26
  } | {
27
27
  type: 'grid';
28
- width: number;
28
+ width?: number | string;
29
29
  };
30
30
  icon?: string;
31
31
  iconColor?: string;