histoire 0.0.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.
- package/bin.mjs +4 -0
- package/dist/client/app/App.vue +51 -0
- package/dist/client/app/components/HomeView.vue +3 -0
- package/dist/client/app/components/SandboxVue.vue +57 -0
- package/dist/client/app/components/StoryControls.vue +29 -0
- package/dist/client/app/components/StoryList.vue +22 -0
- package/dist/client/app/components/StoryListItem.vue +34 -0
- package/dist/client/app/components/StoryVariantGridItem.vue +56 -0
- package/dist/client/app/components/StoryVariantItem.vue +29 -0
- package/dist/client/app/components/StoryVariantSingleView.vue +43 -0
- package/dist/client/app/components/StoryView.vue +132 -0
- package/dist/client/app/components/base/BaseEmpty.vue +5 -0
- package/dist/client/app/components/base/BaseSplitPane.vue +203 -0
- package/dist/client/app/components/exposed/Story.vue +77 -0
- package/dist/client/app/components/exposed/Variant.vue +75 -0
- package/dist/client/app/composable/variant.d.ts +18 -0
- package/dist/client/app/composable/variant.js +18 -0
- package/dist/client/app/composable/variant.js.map +1 -0
- package/dist/client/app/global-components.d.ts +2 -0
- package/dist/client/app/global-components.js +9 -0
- package/dist/client/app/global-components.js.map +1 -0
- package/dist/client/app/index.d.ts +1 -0
- package/dist/client/app/index.js +14 -0
- package/dist/client/app/index.js.map +1 -0
- package/dist/client/app/router.d.ts +1 -0
- package/dist/client/app/router.js +17 -0
- package/dist/client/app/router.js.map +1 -0
- package/dist/client/app/stores/story.d.ts +161 -0
- package/dist/client/app/stores/story.js +29 -0
- package/dist/client/app/stores/story.js.map +1 -0
- package/dist/client/app/style/floating-vue.css +1 -0
- package/dist/client/app/style/main.css +37 -0
- package/dist/client/app/types.d.ts +20 -0
- package/dist/client/app/types.js +2 -0
- package/dist/client/app/types.js.map +1 -0
- package/dist/node/alias.d.ts +2 -0
- package/dist/node/alias.js +6 -0
- package/dist/node/alias.js.map +1 -0
- package/dist/node/bin.d.ts +1 -0
- package/dist/node/bin.js +17 -0
- package/dist/node/bin.js.map +1 -0
- package/dist/node/commands/dev.d.ts +4 -0
- package/dist/node/commands/dev.js +15 -0
- package/dist/node/commands/dev.js.map +1 -0
- package/dist/node/config.d.ts +4 -0
- package/dist/node/config.js +2 -0
- package/dist/node/config.js.map +1 -0
- package/dist/node/context.d.ts +4 -0
- package/dist/node/context.js +2 -0
- package/dist/node/context.js.map +1 -0
- package/dist/node/index.d.ts +0 -0
- package/dist/node/index.js +2 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/plugin.d.ts +5 -0
- package/dist/node/plugin.js +83 -0
- package/dist/node/plugin.js.map +1 -0
- package/dist/node/server.d.ts +2 -0
- package/dist/node/server.js +36 -0
- package/dist/node/server.js.map +1 -0
- package/dist/node/stories.d.ts +10 -0
- package/dist/node/stories.js +43 -0
- package/dist/node/stories.js.map +1 -0
- package/dist/style.css +607 -0
- package/package.json +60 -0
- package/postcss.config.cjs +8 -0
- package/scripts/watchAndCopy.mjs +15 -0
- package/src/client/app/App.vue +51 -0
- package/src/client/app/components/HomeView.vue +3 -0
- package/src/client/app/components/SandboxVue.vue +57 -0
- package/src/client/app/components/StoryControls.vue +29 -0
- package/src/client/app/components/StoryList.vue +22 -0
- package/src/client/app/components/StoryListItem.vue +34 -0
- package/src/client/app/components/StoryVariantGridItem.vue +56 -0
- package/src/client/app/components/StoryVariantItem.vue +29 -0
- package/src/client/app/components/StoryVariantSingleView.vue +43 -0
- package/src/client/app/components/StoryView.vue +132 -0
- package/src/client/app/components/base/BaseEmpty.vue +5 -0
- package/src/client/app/components/base/BaseSplitPane.vue +203 -0
- package/src/client/app/components/exposed/Story.vue +77 -0
- package/src/client/app/components/exposed/Variant.vue +75 -0
- package/src/client/app/composable/variant.ts +20 -0
- package/src/client/app/global-components.ts +10 -0
- package/src/client/app/index.ts +15 -0
- package/src/client/app/router.ts +17 -0
- package/src/client/app/stores/story.ts +34 -0
- package/src/client/app/style/floating-vue.css +1 -0
- package/src/client/app/style/main.css +37 -0
- package/src/client/app/types.ts +21 -0
- package/src/client/shim.d.ts +5 -0
- package/src/node/alias.ts +7 -0
- package/src/node/bin.ts +21 -0
- package/src/node/commands/dev.ts +21 -0
- package/src/node/config.ts +4 -0
- package/src/node/context.ts +5 -0
- package/src/node/index.ts +0 -0
- package/src/node/plugin.ts +92 -0
- package/src/node/server.ts +37 -0
- package/src/node/stories.ts +55 -0
- package/tailwind.config.cjs +9 -0
- package/tsconfig.json +39 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { defineComponent, onBeforeMount, onBeforeUnmount, PropType, provide, ref, useAttrs } from 'vue'
|
|
3
|
+
import { useStoryStore } from '../../stores/story.js'
|
|
4
|
+
import { Story, Variant } from '../../types.js'
|
|
5
|
+
|
|
6
|
+
export default defineComponent({
|
|
7
|
+
inheritAttrs: false,
|
|
8
|
+
|
|
9
|
+
props: {
|
|
10
|
+
title: {
|
|
11
|
+
type: String,
|
|
12
|
+
required: true,
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
layout: {
|
|
16
|
+
type: Object as PropType<Story['layout']>,
|
|
17
|
+
default: () => ({ type: 'single' }),
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
|
|
21
|
+
setup (props) {
|
|
22
|
+
const attrs = useAttrs() as {
|
|
23
|
+
data: {
|
|
24
|
+
id: string
|
|
25
|
+
reloadHandlers: (() => unknown)[]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Story
|
|
30
|
+
|
|
31
|
+
const storyStore = useStoryStore()
|
|
32
|
+
|
|
33
|
+
const story = ref<Story>(null)
|
|
34
|
+
|
|
35
|
+
onBeforeMount(() => {
|
|
36
|
+
story.value = storyStore.addStory({
|
|
37
|
+
id: attrs.data.id,
|
|
38
|
+
title: props.title,
|
|
39
|
+
layout: props.layout,
|
|
40
|
+
variants: [],
|
|
41
|
+
mountTime: Date.now(),
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
onBeforeUnmount(() => {
|
|
46
|
+
storyStore.removeStory(story.value)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// Variants
|
|
50
|
+
|
|
51
|
+
provide('story', story)
|
|
52
|
+
|
|
53
|
+
provide('addVariant', (variant: Variant) => {
|
|
54
|
+
story.value.variants.push(variant)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
provide('removeVariant', (variant: Variant) => {
|
|
58
|
+
const index = story.value.variants.indexOf(variant)
|
|
59
|
+
if (index !== -1) {
|
|
60
|
+
story.value.variants.splice(index, 1)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
story,
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
render () {
|
|
70
|
+
Object.assign(this.story, {
|
|
71
|
+
title: this.title,
|
|
72
|
+
layout: this.layout,
|
|
73
|
+
})
|
|
74
|
+
return this.$slots.default()
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
</script>
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { defineComponent, inject, onBeforeMount, onBeforeUnmount, PropType, reactive, Ref, ref } from 'vue'
|
|
3
|
+
import { Story, Variant } from '../../types'
|
|
4
|
+
|
|
5
|
+
export default defineComponent({
|
|
6
|
+
props: {
|
|
7
|
+
title: {
|
|
8
|
+
type: String,
|
|
9
|
+
required: true,
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
initState: {
|
|
13
|
+
type: Function as PropType<() => any | Promise<any>>,
|
|
14
|
+
default: null,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
setup (props) {
|
|
19
|
+
const story = inject<Ref<Story>>('story')
|
|
20
|
+
|
|
21
|
+
function generateId () {
|
|
22
|
+
let index = 0
|
|
23
|
+
const idMap = story.value.variants.reduce((acc, variant) => {
|
|
24
|
+
acc[variant.id] = true
|
|
25
|
+
return acc
|
|
26
|
+
}, {} as Record<string, boolean>)
|
|
27
|
+
while (idMap[`${story.value.id}-${index}`]) {
|
|
28
|
+
index++
|
|
29
|
+
}
|
|
30
|
+
return `${story.value.id}-${index}`
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getVariantData () {
|
|
34
|
+
return {
|
|
35
|
+
title: props.title,
|
|
36
|
+
initState: async () => {
|
|
37
|
+
if (typeof props.initState === 'function') {
|
|
38
|
+
variant.value.state = await props.initState()
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
slots: () => null,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const variant = ref<Variant>(null)
|
|
46
|
+
|
|
47
|
+
const addVariant = inject('addVariant') as (variant: Variant) => void
|
|
48
|
+
onBeforeMount(() => {
|
|
49
|
+
variant.value = {
|
|
50
|
+
id: generateId(),
|
|
51
|
+
state: reactive<any>({}),
|
|
52
|
+
...getVariantData(),
|
|
53
|
+
}
|
|
54
|
+
addVariant(variant.value)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const removeVariant = inject('removeVariant') as (variant: Variant) => void
|
|
58
|
+
onBeforeUnmount(() => {
|
|
59
|
+
removeVariant(variant.value)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
variant,
|
|
64
|
+
getVariantData,
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
render () {
|
|
69
|
+
Object.assign(this.variant, this.getVariantData(), {
|
|
70
|
+
slots: () => this.$slots,
|
|
71
|
+
})
|
|
72
|
+
return null
|
|
73
|
+
},
|
|
74
|
+
})
|
|
75
|
+
</script>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { computed, Ref } from 'vue'
|
|
2
|
+
import { useRoute } from 'vue-router'
|
|
3
|
+
import { Variant } from '../types'
|
|
4
|
+
|
|
5
|
+
export function useCurrentVariantRoute (variant: Ref<Variant>) {
|
|
6
|
+
const route = useRoute()
|
|
7
|
+
const isActive = computed(() => route.query.variantId === variant.value.id)
|
|
8
|
+
const targetRoute = computed(() => ({
|
|
9
|
+
...route,
|
|
10
|
+
query: {
|
|
11
|
+
...route.query,
|
|
12
|
+
variantId: variant.value.id,
|
|
13
|
+
},
|
|
14
|
+
}))
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
isActive,
|
|
18
|
+
targetRoute,
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { App } from 'vue'
|
|
2
|
+
import Story from './components/exposed/Story.vue'
|
|
3
|
+
import Variant from './components/exposed/Variant.vue'
|
|
4
|
+
|
|
5
|
+
export function registerGlobalComponents (app: App) {
|
|
6
|
+
// eslint-disable-next-line vue/multi-word-component-names
|
|
7
|
+
app.component('Story', Story)
|
|
8
|
+
// eslint-disable-next-line vue/multi-word-component-names
|
|
9
|
+
app.component('Variant', Variant)
|
|
10
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import '../../style.css'
|
|
2
|
+
|
|
3
|
+
import { createApp } from 'vue'
|
|
4
|
+
import { createPinia } from 'pinia'
|
|
5
|
+
import FloatingVue from 'floating-vue'
|
|
6
|
+
import App from './App.vue'
|
|
7
|
+
import { registerGlobalComponents } from './global-components.js'
|
|
8
|
+
import { router } from './router'
|
|
9
|
+
|
|
10
|
+
const app = createApp(App)
|
|
11
|
+
app.use(createPinia())
|
|
12
|
+
app.use(FloatingVue)
|
|
13
|
+
app.use(router)
|
|
14
|
+
registerGlobalComponents(app)
|
|
15
|
+
app.mount('#app')
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { createRouter, createWebHistory } from 'vue-router'
|
|
2
|
+
|
|
3
|
+
export const router = createRouter({
|
|
4
|
+
history: createWebHistory(),
|
|
5
|
+
routes: [
|
|
6
|
+
{
|
|
7
|
+
path: '/',
|
|
8
|
+
name: 'home',
|
|
9
|
+
component: () => import('./components/HomeView.vue'),
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
path: '/story/:storyId',
|
|
13
|
+
name: 'story',
|
|
14
|
+
component: () => import('./components/StoryView.vue'),
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
})
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineStore } from 'pinia'
|
|
2
|
+
import { computed, ref } from 'vue'
|
|
3
|
+
import { router } from '../router'
|
|
4
|
+
import { Story } from '../types'
|
|
5
|
+
|
|
6
|
+
export const useStoryStore = defineStore('story', () => {
|
|
7
|
+
const stories = ref<Story[]>([])
|
|
8
|
+
|
|
9
|
+
function addStory (story: Story) {
|
|
10
|
+
const existing = stories.value.find(s => s.id === story.id)
|
|
11
|
+
if (existing) {
|
|
12
|
+
Object.assign(existing, story)
|
|
13
|
+
return existing
|
|
14
|
+
}
|
|
15
|
+
stories.value.push(story)
|
|
16
|
+
return story
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function removeStory (story: Story) {
|
|
20
|
+
const index = stories.value.findIndex(s => s.id === story.id)
|
|
21
|
+
if (index !== -1) {
|
|
22
|
+
stories.value.splice(index, 1)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const currentStory = computed(() => stories.value.find(s => s.id === router.currentRoute.value.params.storyId))
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
stories,
|
|
30
|
+
addStory,
|
|
31
|
+
removeStory,
|
|
32
|
+
currentStory,
|
|
33
|
+
}
|
|
34
|
+
})
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/* @TODO custom themes */
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
@import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
|
|
2
|
+
@import 'floating-vue/dist/style.css';
|
|
3
|
+
@import 'floating-vue.css';
|
|
4
|
+
|
|
5
|
+
*,
|
|
6
|
+
::before,
|
|
7
|
+
::after {
|
|
8
|
+
box-sizing: border-box;
|
|
9
|
+
border-width: 0;
|
|
10
|
+
border-style: solid;
|
|
11
|
+
border-color: #e5e7eb;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
a {
|
|
15
|
+
color: inherit;
|
|
16
|
+
text-decoration: inherit;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@tailwind base;
|
|
20
|
+
@tailwind components;
|
|
21
|
+
@tailwind utilities;
|
|
22
|
+
|
|
23
|
+
body {
|
|
24
|
+
margin: 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
html,
|
|
28
|
+
body,
|
|
29
|
+
#app {
|
|
30
|
+
height: 100%;
|
|
31
|
+
font-family: 'Lato', sans-serif;
|
|
32
|
+
font-size: 14px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
.__histoire-sandbox {
|
|
36
|
+
contain: content;
|
|
37
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface Story {
|
|
2
|
+
id: string
|
|
3
|
+
title: string
|
|
4
|
+
variants: Variant[]
|
|
5
|
+
layout?: {
|
|
6
|
+
type: 'single'
|
|
7
|
+
} | {
|
|
8
|
+
type: 'grid'
|
|
9
|
+
width: number
|
|
10
|
+
}
|
|
11
|
+
lastSelectedVariant?: Variant
|
|
12
|
+
mountTime: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Variant {
|
|
16
|
+
id: string
|
|
17
|
+
title: string
|
|
18
|
+
initState: () => any
|
|
19
|
+
slots: () => Readonly<any>
|
|
20
|
+
state: any
|
|
21
|
+
}
|
package/src/node/bin.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import { dirname, resolve } from 'pathe'
|
|
3
|
+
import { fileURLToPath } from 'url'
|
|
4
|
+
import sade from 'sade'
|
|
5
|
+
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
7
|
+
|
|
8
|
+
const { version } = JSON.parse(fs.readFileSync(resolve(__dirname, '../../package.json'), 'utf8'))
|
|
9
|
+
|
|
10
|
+
const program = sade('histoire')
|
|
11
|
+
program.version(version)
|
|
12
|
+
|
|
13
|
+
program.command('dev')
|
|
14
|
+
.describe('open the stories in your browser for development')
|
|
15
|
+
.option('-p, --port <port>', 'Listening port of the server')
|
|
16
|
+
.action(async (options) => {
|
|
17
|
+
const { dev } = await import('./commands/dev.js')
|
|
18
|
+
return dev(options)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
program.parse(process.argv)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { join } from 'pathe'
|
|
2
|
+
import { HistoireConfig } from '../config.js'
|
|
3
|
+
import { Context } from '../context.js'
|
|
4
|
+
import { createServer } from '../server.js'
|
|
5
|
+
|
|
6
|
+
export interface DevOptions {
|
|
7
|
+
port: number
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function dev (options: DevOptions) {
|
|
11
|
+
const config: HistoireConfig = {
|
|
12
|
+
sourceDir: join(process.cwd(), 'src'),
|
|
13
|
+
storyMatch: ['**/*.story.vue'],
|
|
14
|
+
}
|
|
15
|
+
const ctx: Context = {
|
|
16
|
+
config,
|
|
17
|
+
}
|
|
18
|
+
const server = await createServer(ctx)
|
|
19
|
+
await server.listen(options.port)
|
|
20
|
+
server.printUrls()
|
|
21
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { defineConfig, mergeConfig, Plugin } from 'vite'
|
|
2
|
+
import { APP_PATH, DIST_CLIENT_PATH } from './alias.js'
|
|
3
|
+
import { Context } from './context.js'
|
|
4
|
+
import { stories } from './stories.js'
|
|
5
|
+
|
|
6
|
+
export const STORIES_ID = '$histoire-stories'
|
|
7
|
+
export const RESOLVED_STORIES_ID = `/${STORIES_ID}-resolved`
|
|
8
|
+
|
|
9
|
+
export async function createVitePlugins (ctx: Context): Promise<Plugin[]> {
|
|
10
|
+
const userViteConfig = {} // @TODO
|
|
11
|
+
|
|
12
|
+
const vuePlugin = (await import('@vitejs/plugin-vue')).default()
|
|
13
|
+
|
|
14
|
+
const vitePlugin: Plugin = {
|
|
15
|
+
name: 'histoire-vite-plugin',
|
|
16
|
+
|
|
17
|
+
config () {
|
|
18
|
+
const baseConfig = defineConfig({
|
|
19
|
+
|
|
20
|
+
optimizeDeps: {
|
|
21
|
+
// force include vue to avoid duplicated copies when linked + optimized
|
|
22
|
+
include: ['vue'],
|
|
23
|
+
},
|
|
24
|
+
server: {
|
|
25
|
+
fs: {
|
|
26
|
+
allow: [DIST_CLIENT_PATH, ctx.config.sourceDir, process.cwd()],
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
})
|
|
30
|
+
return userViteConfig
|
|
31
|
+
? mergeConfig(userViteConfig, baseConfig)
|
|
32
|
+
: baseConfig
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
resolveId (id) {
|
|
36
|
+
if (id.startsWith(STORIES_ID)) {
|
|
37
|
+
return RESOLVED_STORIES_ID
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
load (id) {
|
|
42
|
+
if (id === RESOLVED_STORIES_ID) {
|
|
43
|
+
return `${stories.map((story, index) => `import Comp${index} from '/${story.file}'`).join('\n')}
|
|
44
|
+
export let files = [${stories.map((story, index) => `{ id: '${story.id}', file: '/${story.file}', component: Comp${index} }`).join(',\n')}]
|
|
45
|
+
const handlers = []
|
|
46
|
+
export function onUpdate (cb) {
|
|
47
|
+
handlers.push(cb)
|
|
48
|
+
}
|
|
49
|
+
if (import.meta.hot) {
|
|
50
|
+
import.meta.hot.accept(newModule => {
|
|
51
|
+
files = newModule.files
|
|
52
|
+
handlers.forEach(h => {
|
|
53
|
+
h(newModule.files)
|
|
54
|
+
newModule.onUpdate(h)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
}`
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
configureServer (server) {
|
|
62
|
+
// serve our index.html after vite history fallback
|
|
63
|
+
return () => {
|
|
64
|
+
server.middlewares.use((req, res, next) => {
|
|
65
|
+
if (req.url!.endsWith('.html')) {
|
|
66
|
+
res.statusCode = 200
|
|
67
|
+
res.end(`
|
|
68
|
+
<!DOCTYPE html>
|
|
69
|
+
<html>
|
|
70
|
+
<head>
|
|
71
|
+
<title></title>
|
|
72
|
+
<meta charset="utf-8">
|
|
73
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
74
|
+
<meta name="description" content="">
|
|
75
|
+
</head>
|
|
76
|
+
<body>
|
|
77
|
+
<div id="app"></div>
|
|
78
|
+
<script type="module" src="/@fs/${APP_PATH}/index.js"></script>
|
|
79
|
+
</body>
|
|
80
|
+
</html>`)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
next()
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
return [
|
|
89
|
+
vitePlugin,
|
|
90
|
+
vuePlugin,
|
|
91
|
+
]
|
|
92
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createServer as createViteServer } from 'vite'
|
|
2
|
+
import { Context } from './context.js'
|
|
3
|
+
import { createVitePlugins, RESOLVED_STORIES_ID } from './plugin.js'
|
|
4
|
+
import { onStoryChange, watchStories } from './stories.js'
|
|
5
|
+
|
|
6
|
+
export async function createServer (ctx: Context) {
|
|
7
|
+
await watchStories(ctx)
|
|
8
|
+
const server = await createViteServer({
|
|
9
|
+
root: ctx.config.sourceDir,
|
|
10
|
+
plugins: await createVitePlugins(ctx),
|
|
11
|
+
server: {
|
|
12
|
+
watch: {
|
|
13
|
+
usePolling: true,
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
onStoryChange(() => {
|
|
18
|
+
const mod = server.moduleGraph.getModuleById(RESOLVED_STORIES_ID)
|
|
19
|
+
if (mod) {
|
|
20
|
+
server.moduleGraph.invalidateModule(mod)
|
|
21
|
+
const timestamp = Date.now()
|
|
22
|
+
mod.lastHMRTimestamp = timestamp
|
|
23
|
+
server.ws.send({
|
|
24
|
+
type: 'update',
|
|
25
|
+
updates: [
|
|
26
|
+
{
|
|
27
|
+
type: 'js-update',
|
|
28
|
+
acceptedPath: mod.url,
|
|
29
|
+
path: mod.url,
|
|
30
|
+
timestamp: timestamp,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
return server
|
|
37
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Context } from './context.js'
|
|
2
|
+
import chokidar from 'chokidar'
|
|
3
|
+
import { globby } from 'globby'
|
|
4
|
+
import Case from 'case'
|
|
5
|
+
|
|
6
|
+
export interface Story {
|
|
7
|
+
id: string
|
|
8
|
+
file: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const stories: Story[] = []
|
|
12
|
+
|
|
13
|
+
const handlers: (() => unknown)[] = []
|
|
14
|
+
|
|
15
|
+
export function onStoryChange (handler: () => unknown) {
|
|
16
|
+
handlers.push(handler)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function watchStories (ctx: Context) {
|
|
20
|
+
const watcher = chokidar.watch(ctx.config.storyMatch, {
|
|
21
|
+
cwd: ctx.config.sourceDir,
|
|
22
|
+
})
|
|
23
|
+
watcher.on('add', (file) => {
|
|
24
|
+
addStory(file)
|
|
25
|
+
notifyChange()
|
|
26
|
+
})
|
|
27
|
+
watcher.on('unlink', (file) => {
|
|
28
|
+
stories.splice(stories.findIndex((story) => story.file === file), 1)
|
|
29
|
+
notifyChange()
|
|
30
|
+
})
|
|
31
|
+
return watcher
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function notifyChange () {
|
|
35
|
+
for (const handler of handlers) {
|
|
36
|
+
handler()
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function addStory (file: string) {
|
|
41
|
+
stories.push({
|
|
42
|
+
id: Case.kebab(file),
|
|
43
|
+
file,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function findStories (ctx: Context) {
|
|
48
|
+
const files = await globby(ctx.config.storyMatch, {
|
|
49
|
+
cwd: ctx.config.sourceDir,
|
|
50
|
+
})
|
|
51
|
+
stories.splice(0, stories.length)
|
|
52
|
+
for (const file of files) {
|
|
53
|
+
addStory(file)
|
|
54
|
+
}
|
|
55
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"outDir": "dist",
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"allowSyntheticDefaultImports": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"removeComments": false,
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"types": [
|
|
14
|
+
"node"
|
|
15
|
+
],
|
|
16
|
+
"lib": [
|
|
17
|
+
"ESNext",
|
|
18
|
+
"DOM"
|
|
19
|
+
],
|
|
20
|
+
"sourceMap": false,
|
|
21
|
+
"preserveWatchOutput": true,
|
|
22
|
+
"preserveSymlinks": true,
|
|
23
|
+
// Strict
|
|
24
|
+
"noImplicitAny": false,
|
|
25
|
+
"noImplicitThis": true,
|
|
26
|
+
"alwaysStrict": true,
|
|
27
|
+
"strictBindCallApply": true,
|
|
28
|
+
"strictFunctionTypes": true,
|
|
29
|
+
},
|
|
30
|
+
"include": [
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"exclude": [
|
|
34
|
+
"node_modules",
|
|
35
|
+
"generated/**/*",
|
|
36
|
+
"dist/**/*",
|
|
37
|
+
"src/**/*.spec.ts"
|
|
38
|
+
]
|
|
39
|
+
}
|