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
@@ -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
+ }
@@ -2,7 +2,7 @@ import { createServer as createViteServer } from 'vite'
2
2
  import { Context } from './context.js'
3
3
  import { createVitePlugins, RESOLVED_STORIES_ID } from './plugin.js'
4
4
  import { notifyStoryChange, onStoryChange, watchStories } from './stories.js'
5
- import { useCollectStories } from './collect.js'
5
+ import { useCollectStories } from './collect/index.js'
6
6
  import type { StoryFile } from './types.js'
7
7
 
8
8
  export async function createServer (ctx: Context, port: number) {
package/src/node/types.ts CHANGED
@@ -26,7 +26,7 @@ export interface Story {
26
26
  type: 'single'
27
27
  } | {
28
28
  type: 'grid'
29
- width: number
29
+ width?: number | string
30
30
  }
31
31
  icon?: string
32
32
  iconColor?: string