histoire 0.9.0 → 0.9.3

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 (34) hide show
  1. package/dist/node/bin.js +1 -0
  2. package/dist/node/build.js +12 -0
  3. package/dist/node/builtin-plugins/tailwind-tokens.js +390 -383
  4. package/dist/node/builtin-plugins/vanilla-support/MountStory.d.ts +14 -0
  5. package/dist/node/builtin-plugins/vanilla-support/MountStory.js +39 -0
  6. package/dist/node/builtin-plugins/vanilla-support/RenderStory.d.ts +38 -0
  7. package/dist/node/builtin-plugins/vanilla-support/RenderStory.js +131 -0
  8. package/dist/node/builtin-plugins/vanilla-support/client.d.ts +3 -0
  9. package/dist/node/builtin-plugins/vanilla-support/client.js +5 -0
  10. package/dist/node/builtin-plugins/vanilla-support/collect.d.ts +2 -0
  11. package/dist/node/builtin-plugins/vanilla-support/collect.js +33 -0
  12. package/dist/node/builtin-plugins/vanilla-support/plugin.d.ts +2 -0
  13. package/dist/node/builtin-plugins/vanilla-support/plugin.js +29 -0
  14. package/dist/node/builtin-plugins/vanilla-support/types.d.ts +35 -0
  15. package/dist/node/builtin-plugins/vanilla-support/types.js +3 -0
  16. package/dist/node/builtin-plugins/vanilla-support/vanilla.story.d.ts +3 -0
  17. package/dist/node/builtin-plugins/vanilla-support/vanilla.story.js +55 -0
  18. package/dist/node/config.js +2 -0
  19. package/dist/node/server.js +1 -1
  20. package/dist/node/vite.js +30 -14
  21. package/package.json +8 -6
  22. package/src/node/bin.ts +1 -0
  23. package/src/node/build.ts +12 -0
  24. package/src/node/builtin-plugins/tailwind-tokens.ts +393 -383
  25. package/src/node/builtin-plugins/vanilla-support/MountStory.ts +50 -0
  26. package/src/node/builtin-plugins/vanilla-support/RenderStory.ts +166 -0
  27. package/src/node/builtin-plugins/vanilla-support/client.ts +6 -0
  28. package/src/node/builtin-plugins/vanilla-support/collect.ts +42 -0
  29. package/src/node/builtin-plugins/vanilla-support/plugin.ts +35 -0
  30. package/src/node/builtin-plugins/vanilla-support/types.ts +40 -0
  31. package/src/node/builtin-plugins/vanilla-support/vanilla.story.ts +63 -0
  32. package/src/node/config.ts +2 -0
  33. package/src/node/server.ts +2 -2
  34. package/src/node/vite.ts +30 -14
@@ -0,0 +1,50 @@
1
+ import {
2
+ defineComponent as _defineComponent,
3
+ PropType as _PropType,
4
+ } from '@histoire/vendors/vue'
5
+ import type { Story } from '@histoire/shared'
6
+ import type { StoryOptions, VariantOptions } from './types'
7
+
8
+ export default _defineComponent({
9
+ name: 'MountStory',
10
+
11
+ props: {
12
+ story: {
13
+ type: Object as _PropType<Story>,
14
+ required: true,
15
+ },
16
+ },
17
+
18
+ setup (props) {
19
+ const options = props.story.file.component as StoryOptions
20
+
21
+ let rawVariants: VariantOptions[] = []
22
+
23
+ if (options.onMount) {
24
+ rawVariants = [{
25
+ id: '_default',
26
+ title: 'default',
27
+ onMount: options.onMount,
28
+ onMountControls: options.onMountControls,
29
+ }]
30
+ } else {
31
+ rawVariants = options.variants
32
+ }
33
+
34
+ for (const index in props.story.variants) {
35
+ const rawVariant = rawVariants[index]
36
+ Object.assign(props.story.variants[index], {
37
+ slots: () => ({ default: rawVariant.onMount, controls: rawVariant.onMountControls }),
38
+ source: rawVariant.source ?? options.source,
39
+ responsiveDisabled: rawVariant.responsiveDisabled ?? options.responsiveDisabled,
40
+ autoPropsDisabled: rawVariant.autoPropsDisabled ?? options.autoPropsDisabled,
41
+ setupApp: rawVariant.setupApp ?? options.setupApp,
42
+ configReady: true,
43
+ })
44
+ }
45
+ },
46
+
47
+ render () {
48
+ return null
49
+ },
50
+ })
@@ -0,0 +1,166 @@
1
+ /* eslint-disable vue/one-component-per-file */
2
+
3
+ import {
4
+ defineComponent as _defineComponent,
5
+ onBeforeUnmount as _onBeforeUnmount,
6
+ onMounted as _onMounted,
7
+ PropType as _PropType,
8
+ ref as _ref,
9
+ watch as _watch,
10
+ h as _h,
11
+ } from '@histoire/vendors/vue'
12
+ import type { Story, Variant } from '@histoire/shared'
13
+ // @ts-expect-error virtual module id
14
+ import * as setup from '$histoire-setup'
15
+ // @ts-expect-error virtual module id
16
+ import * as generatedSetup from '$histoire-generated-global-setup'
17
+ import type { App, MountApi, VanillaApi } from './types'
18
+
19
+ export default _defineComponent({
20
+ name: 'RenderStory',
21
+
22
+ props: {
23
+ variant: {
24
+ type: Object as _PropType<Variant>,
25
+ required: true,
26
+ },
27
+
28
+ story: {
29
+ type: Object as _PropType<Story>,
30
+ required: true,
31
+ },
32
+
33
+ slotName: {
34
+ type: String,
35
+ default: 'default',
36
+ },
37
+ },
38
+
39
+ emits: {
40
+ ready: () => true,
41
+ },
42
+
43
+ setup (props, { emit }) {
44
+ const sandbox = _ref<HTMLDivElement>()
45
+ let mounting = false
46
+ let app: App
47
+ let appHooks: Record<'onUpdate' | 'onUnmount', (() => unknown)[]>
48
+
49
+ async function unmountVariant () {
50
+ if (app) {
51
+ await app.onUnmount?.()
52
+ if (appHooks) {
53
+ for (const hook of appHooks.onUnmount) {
54
+ await hook()
55
+ }
56
+ }
57
+
58
+ app.el.parentNode.removeChild(app.el)
59
+ app = null
60
+ }
61
+ }
62
+
63
+ async function mountVariant () {
64
+ if (mounting) return
65
+ mounting = true
66
+
67
+ await unmountVariant()
68
+
69
+ app = {
70
+ el: document.createElement('div'),
71
+ }
72
+
73
+ if (typeof generatedSetup?.setupVanilla === 'function') {
74
+ await generatedSetup.setupVanilla({
75
+ app,
76
+ story: props.story,
77
+ variant: props.variant,
78
+ })
79
+ }
80
+
81
+ if (typeof setup?.setupVanilla === 'function') {
82
+ await setup.setupVanilla({
83
+ app,
84
+ story: props.story,
85
+ variant: props.variant,
86
+ })
87
+ }
88
+
89
+ if (typeof props.variant.setupApp === 'function') {
90
+ await props.variant.setupApp({
91
+ app,
92
+ story: props.story,
93
+ variant: props.variant,
94
+ })
95
+ }
96
+
97
+ await app.onMount?.()
98
+
99
+ appHooks = {
100
+ onUpdate: [],
101
+ onUnmount: [],
102
+ }
103
+
104
+ const api: MountApi = {
105
+ el: app.el,
106
+ state: props.variant.state,
107
+ onUpdate: (cb) => {
108
+ appHooks.onUpdate.push(cb)
109
+ },
110
+ onUnmount: (cb) => {
111
+ appHooks.onUnmount.push(cb)
112
+ },
113
+ }
114
+
115
+ const onMount = props.variant.slots()[props.slotName] as VanillaApi['onMount'] | VanillaApi['onMountControls']
116
+ await onMount(api)
117
+
118
+ sandbox.value.appendChild(app.el)
119
+
120
+ emit('ready')
121
+ }
122
+
123
+ _onMounted(async () => {
124
+ if (props.variant.configReady) {
125
+ await mountVariant()
126
+ }
127
+ })
128
+
129
+ _watch(() => props.variant, async value => {
130
+ if (value.configReady && !mounting) {
131
+ if (!app) {
132
+ await mountVariant()
133
+ } else {
134
+ // @TODO check if need to refresh here
135
+ }
136
+ }
137
+ }, {
138
+ deep: true,
139
+ })
140
+
141
+ _watch(() => props.variant.state, async () => {
142
+ if (appHooks) {
143
+ for (const hook of appHooks.onUpdate) {
144
+ await hook()
145
+ }
146
+ }
147
+ }, {
148
+ deep: true,
149
+ })
150
+
151
+ _onBeforeUnmount(() => {
152
+ unmountVariant()
153
+ })
154
+
155
+ return {
156
+ sandbox,
157
+ }
158
+ },
159
+
160
+ render () {
161
+ return _h('div', {
162
+ ref: 'sandbox',
163
+ class: '__histoire-sandbox htw-overflow-auto',
164
+ })
165
+ },
166
+ })
@@ -0,0 +1,6 @@
1
+ export { default as MountStory } from './MountStory'
2
+ export { default as RenderStory } from './RenderStory'
3
+
4
+ export function generateSourceCode () {
5
+ // noop
6
+ }
@@ -0,0 +1,42 @@
1
+ import type { ServerRunPayload, ServerStory, ServerVariant } from '@histoire/shared'
2
+ import type { StoryOptions, VariantOptions } from './types'
3
+
4
+ export async function run ({ file, storyData }: ServerRunPayload) {
5
+ const { default: Comp } = await import(file.moduleId)
6
+
7
+ const options = Comp as StoryOptions
8
+
9
+ let rawVariants: VariantOptions[] = []
10
+
11
+ if (options.onMount) {
12
+ // Implicit variant
13
+ rawVariants = [{
14
+ id: '_default',
15
+ title: 'default',
16
+ }]
17
+ } else {
18
+ rawVariants = options.variants
19
+ }
20
+
21
+ const story: ServerStory = {
22
+ id: options.id ?? file.id,
23
+ title: options.title ?? file.fileName,
24
+ group: options.group,
25
+ layout: options.layout ?? { type: 'single', iframe: true },
26
+ icon: options.icon,
27
+ iconColor: options.iconColor,
28
+ docsOnly: options.docsOnly ?? false,
29
+ variants: null,
30
+ }
31
+
32
+ const variants: ServerVariant[] = rawVariants.map((v, index) => ({
33
+ id: v.id ?? `${story.id}-${index}`,
34
+ title: v.title ?? 'untitled',
35
+ icon: v.icon ?? options.icon,
36
+ iconColor: v.iconColor ?? options.iconColor,
37
+ }))
38
+
39
+ story.variants = variants
40
+
41
+ storyData.push(story)
42
+ }
@@ -0,0 +1,35 @@
1
+ import { dirname } from 'pathe'
2
+ import { fileURLToPath } from 'url'
3
+ import { Plugin } from '../../plugin.js'
4
+
5
+ const __dirname = dirname(fileURLToPath(import.meta.url))
6
+
7
+ export function vanillaSupport (): Plugin {
8
+ return {
9
+ name: 'builtin:vanilla-support',
10
+
11
+ config () {
12
+ return {
13
+ supportMatch: [
14
+ {
15
+ id: 'vanilla',
16
+ patterns: ['**/*.js'],
17
+ pluginIds: ['vanilla'],
18
+ },
19
+ ],
20
+ }
21
+ },
22
+
23
+ supportPlugin: {
24
+ id: 'vanilla',
25
+ moduleName: __dirname,
26
+ setupFn: 'setupVanilla',
27
+ importStoryComponent: (file, index) => `import Comp${index} from '${file.path}'`,
28
+ },
29
+
30
+ // onDev (api) {
31
+ // // Test vanilla story
32
+ // api.addStoryFile(resolve(__dirname, './vanilla.story.js'))
33
+ // },
34
+ }
35
+ }
@@ -0,0 +1,40 @@
1
+ import type { StoryProps, VariantProps, Story, Variant, Awaitable } from '@histoire/shared'
2
+
3
+ export interface App {
4
+ el: HTMLElement
5
+ onMount?: () => void
6
+ onUpdate?: () => void
7
+ onUnmount?: () => void
8
+ }
9
+
10
+ export interface MountApi {
11
+ el: HTMLElement
12
+ state: Record<string, any>
13
+ onUpdate: (cb: () => unknown) => Awaitable<void>
14
+ onUnmount: (cb: () => unknown) => Awaitable<void>
15
+ }
16
+
17
+ export interface VanillaApi {
18
+ onMount?: (api: MountApi) => Awaitable<void>
19
+ onMountControls?: (api: MountApi) => Awaitable<void>
20
+ }
21
+
22
+ interface CommonProps {
23
+ setupApp?: (payload: { app: App, story: Story, variant: Variant }) => unknown
24
+ }
25
+
26
+ export type StoryOptions = Omit<StoryProps, 'setupApp'> & CommonProps & ((VanillaApi & {
27
+ variants?: never
28
+ }) | {
29
+ variants?: VariantOptions[]
30
+ onMount?: never
31
+ onMountControls?: never
32
+ })
33
+
34
+ export interface VariantOptions extends Omit<VariantProps, 'setupApp'>, CommonProps, VanillaApi {
35
+
36
+ }
37
+
38
+ export function defineStory (story: StoryOptions) {
39
+ return story
40
+ }
@@ -0,0 +1,63 @@
1
+ import type { StoryOptions } from './types'
2
+
3
+ export default {
4
+ title: 'Vanilla story',
5
+ icon: 'ion:logo-nodejs',
6
+ variants: [
7
+ {
8
+ title: 'Hello',
9
+ onMount: ({ el, state, onUpdate }) => {
10
+ state.count = 0
11
+
12
+ const div = document.createElement('div')
13
+ div.innerHTML = '<h1>Hello World</h1>'
14
+ el.appendChild(div)
15
+
16
+ const counter = document.createElement('pre')
17
+ counter.innerHTML = `Counter: ${state.counter}`
18
+ div.appendChild(counter)
19
+
20
+ onUpdate(() => {
21
+ counter.innerHTML = `Counter: ${state.count}`
22
+ })
23
+ },
24
+ onMountControls: ({ el, state, onUnmount }) => {
25
+ const add = () => {
26
+ state.count++
27
+ }
28
+
29
+ Object.assign(el.style, {
30
+ padding: '8px',
31
+ })
32
+
33
+ const btn = document.createElement('button')
34
+ btn.innerHTML = '+1'
35
+ Object.assign(btn.style, {
36
+ padding: '6px',
37
+ borderRadius: '3px',
38
+ backgroundColor: '#a7f3d0',
39
+ })
40
+ btn.addEventListener('click', add)
41
+ el.appendChild(btn)
42
+
43
+ onUnmount(() => {
44
+ btn.removeEventListener('click', add)
45
+ })
46
+ },
47
+ },
48
+ {
49
+ title: 'Meow',
50
+ onMount: ({ el, onUpdate, onUnmount }) => {
51
+ const div = document.createElement('div')
52
+ div.innerHTML = '<h1>Meow</h1>'
53
+ el.appendChild(div)
54
+ onUpdate(() => {
55
+ console.log('update')
56
+ })
57
+ onUnmount(() => {
58
+ el.removeChild(div)
59
+ })
60
+ },
61
+ },
62
+ ],
63
+ } as StoryOptions
@@ -16,6 +16,7 @@ import { defaultColors } from './colors.js'
16
16
  import { Plugin } from './plugin.js'
17
17
  import { findUp } from './util/find-up.js'
18
18
  import { tailwindTokens } from './builtin-plugins/tailwind-tokens.js'
19
+ import { vanillaSupport } from './builtin-plugins/vanilla-support/plugin.js'
19
20
 
20
21
  export interface SupportMatchPattern {
21
22
  id: string
@@ -196,6 +197,7 @@ export type ConfigMode = 'build' | 'dev'
196
197
  export function getDefaultConfig (): HistoireConfig {
197
198
  return {
198
199
  plugins: [
200
+ vanillaSupport(),
199
201
  tailwindTokens(),
200
202
  ],
201
203
  outDir: '.histoire/dist',
@@ -19,6 +19,8 @@ export async function createServer (ctx: Context, port: number) {
19
19
  server: nodeServer,
20
20
  })
21
21
 
22
+ await watchStories(ctx)
23
+
22
24
  const pluginOnCleanups: (() => void | Promise<void>)[] = []
23
25
  for (const plugin of ctx.config.plugins) {
24
26
  if (plugin.onDev) {
@@ -30,8 +32,6 @@ export async function createServer (ctx: Context, port: number) {
30
32
  }
31
33
  }
32
34
 
33
- await watchStories(ctx)
34
-
35
35
  // Wait for pre-bundling (in `listen()`)
36
36
  await server.listen(port)
37
37
 
package/src/node/vite.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createRequire } from 'module'
2
- import { relative, dirname, resolve, join } from 'pathe'
2
+ import { relative, resolve, join, dirname } from 'pathe'
3
3
  import {
4
4
  resolveConfig as resolveViteConfigInternal,
5
5
  Plugin as VitePlugin,
@@ -72,6 +72,18 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
72
72
  const inlineConfig = await mergeHistoireViteConfig(userViteConfig?.config ?? {}, ctx)
73
73
  const plugins: VitePlugin[] = []
74
74
 
75
+ function optimizeDeps (deps: string[]): string[] {
76
+ const result = []
77
+ for (const dep of deps) {
78
+ try {
79
+ result.push(dirname(require.resolve(`${dep}/package.json`)))
80
+ } catch (e) {
81
+ // Noop
82
+ }
83
+ }
84
+ return result
85
+ }
86
+
75
87
  plugins.push({
76
88
  name: 'histoire-vite-plugin',
77
89
 
@@ -92,14 +104,13 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
92
104
  `${APP_PATH}/bundle-sandbox.js`,
93
105
  // `${APP_PATH}/server/index.js`,
94
106
  ],
95
- include: [
96
- 'vue',
97
- dirname(require.resolve('@vue/runtime-core/package.json')),
98
- dirname(require.resolve('shiki/package.json')),
99
- dirname(require.resolve('vscode-textmate/package.json')),
100
- dirname(require.resolve('vscode-oniguruma/package.json')),
101
- dirname(require.resolve('case/package.json')),
102
- ],
107
+ include: optimizeDeps([
108
+ 'case',
109
+ 'shiki',
110
+ // Shiki dependencies
111
+ 'vscode-oniguruma',
112
+ 'vscode-textmate',
113
+ ]),
103
114
  exclude: [
104
115
  'histoire',
105
116
  '@histoire',
@@ -107,7 +118,12 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
107
118
  },
108
119
  server: {
109
120
  fs: {
110
- allow: [APP_PATH, TEMP_PATH, resolvedViteConfig.root, process.cwd()],
121
+ allow: [
122
+ APP_PATH,
123
+ TEMP_PATH,
124
+ resolvedViteConfig.root,
125
+ process.cwd(),
126
+ ],
111
127
  },
112
128
  watch: {
113
129
  ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
@@ -265,18 +281,18 @@ if (import.meta.hot) {
265
281
  }
266
282
 
267
283
  if (id === RESOLVED_SUPPORT_PLUGINS_CLIENT) {
268
- const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import('${require.resolve(`${p.moduleName}/client`, {
284
+ const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/client`, {
269
285
  paths: [ctx.root, import.meta.url],
270
- })}')`)
286
+ }))})`)
271
287
  return `export const clientSupportPlugins = {
272
288
  ${plugins.join(',\n ')}
273
289
  }`
274
290
  }
275
291
 
276
292
  if (id === RESOLVED_SUPPORT_PLUGINS_COLLECT) {
277
- const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import('${require.resolve(`${p.moduleName}/collect`, {
293
+ const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/collect`, {
278
294
  paths: [ctx.root, import.meta.url],
279
- })}')`)
295
+ }))})`)
280
296
  return `export const collectSupportPlugins = {
281
297
  ${plugins.join(',\n ')}
282
298
  }`