histoire 0.4.3 → 0.4.6
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/dist/bundled/StoryView.js +376 -136
- package/dist/bundled/global-components.js +11 -1
- package/dist/bundled/index2.js +3 -0
- package/dist/bundled/sandbox.js +2 -10
- package/dist/bundled/{reactivity.js → state.js} +27 -1
- package/dist/bundled/story.js +1 -1
- package/dist/client/app/components/base/BaseSelect.vue +72 -0
- package/dist/client/app/components/panel/StatePresets.vue +193 -0
- package/dist/client/app/components/panel/StoryControls.vue +15 -1
- package/dist/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
- package/dist/client/app/index.js +5 -0
- package/dist/client/app/sandbox.js +2 -11
- package/dist/client/app/util/mapping.js +11 -1
- package/dist/client/app/util/state.d.ts +4 -0
- package/dist/client/app/util/state.js +46 -0
- package/dist/client/app/util/variant.d.ts +1 -1
- package/dist/node/build.js +3 -3
- package/dist/node/collect/worker.js +0 -4
- package/dist/node/commands/preview.js +1 -1
- package/dist/node/server.js +53 -45
- package/dist/node/vite.d.ts +1 -1
- package/dist/node/vite.js +11 -2
- package/dist/style.css +70 -37
- package/package.json +4 -4
- package/src/client/app/components/base/BaseSelect.vue +72 -0
- package/src/client/app/components/panel/StatePresets.vue +193 -0
- package/src/client/app/components/panel/StoryControls.vue +15 -1
- package/src/client/app/components/story/StoryVariantSinglePreviewRemote.vue +2 -14
- package/src/client/app/index.ts +6 -0
- package/src/client/app/sandbox.ts +2 -10
- package/src/client/app/util/mapping.ts +12 -1
- package/src/client/app/util/state.ts +51 -0
- package/src/client/app/util/variant.ts +1 -1
- package/src/node/build.ts +3 -3
- package/src/node/collect/worker.ts +0 -4
- package/src/node/commands/preview.ts +1 -1
- package/src/node/server.ts +55 -47
- package/src/node/vite.ts +12 -2
- package/vite.config.ts +25 -0
- package/dist/client/app/util/reactivity.d.ts +0 -1
- package/dist/client/app/util/reactivity.js +0 -16
- package/src/client/app/util/reactivity.ts +0 -22
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
<script lang="ts" setup>
|
|
2
|
+
import { ref, computed, nextTick, onMounted } from 'vue'
|
|
3
|
+
import { Icon } from '@iconify/vue'
|
|
4
|
+
import { useStorage, onClickOutside, useTimeoutFn } from '@vueuse/core'
|
|
5
|
+
import BaseSelect from '../base/BaseSelect.vue'
|
|
6
|
+
import { applyStateToVariant, clone, toRawDeep } from '../../util/state'
|
|
7
|
+
import type { Story, Variant } from '../../types'
|
|
8
|
+
|
|
9
|
+
const DEFAULT_ID = 'default'
|
|
10
|
+
|
|
11
|
+
const props = defineProps<{
|
|
12
|
+
story: Story
|
|
13
|
+
variant: Variant
|
|
14
|
+
}>()
|
|
15
|
+
|
|
16
|
+
const saveId = computed(() => `${props.story.id}:${props.variant.id}`)
|
|
17
|
+
|
|
18
|
+
const defaultState = clone(toRawDeep(props.variant.state))
|
|
19
|
+
|
|
20
|
+
const selectedOption = useStorage<string>(
|
|
21
|
+
`_histoire-presets/${saveId.value}/selected`,
|
|
22
|
+
DEFAULT_ID,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
const presetStates = useStorage<Map<string, { state: Record<string, unknown>, label: string }>>(
|
|
26
|
+
`_histoire-presets/${saveId.value}/states`,
|
|
27
|
+
new Map(),
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const presetsOptions = computed(() => {
|
|
31
|
+
const options = { [DEFAULT_ID]: 'Initial state' }
|
|
32
|
+
presetStates.value.forEach((value, key) => {
|
|
33
|
+
options[key] = value.label
|
|
34
|
+
})
|
|
35
|
+
return options
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
function resetState () {
|
|
39
|
+
selectedOption.value = DEFAULT_ID
|
|
40
|
+
applyStateToVariant(props.variant, defaultState)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function applyPreset (id) {
|
|
44
|
+
if (id === DEFAULT_ID) {
|
|
45
|
+
resetState()
|
|
46
|
+
} else if (presetStates.value.has(id)) {
|
|
47
|
+
applyStateToVariant(props.variant, presetStates.value.get(id).state)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
onMounted(() => {
|
|
52
|
+
if (selectedOption.value !== DEFAULT_ID) {
|
|
53
|
+
applyPreset(selectedOption.value)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const input = ref<HTMLInputElement>()
|
|
58
|
+
const select = ref<HTMLInputElement>()
|
|
59
|
+
const canEdit = computed(() => selectedOption.value !== DEFAULT_ID)
|
|
60
|
+
const isEditing = ref(false)
|
|
61
|
+
|
|
62
|
+
async function createPreset () {
|
|
63
|
+
const id = (new Date()).getTime().toString()
|
|
64
|
+
|
|
65
|
+
presetStates.value.set(id, { state: clone(toRawDeep(props.variant.state)), label: 'New preset' })
|
|
66
|
+
selectedOption.value = id
|
|
67
|
+
isEditing.value = true
|
|
68
|
+
await nextTick()
|
|
69
|
+
input.value.select()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const savedNotif = ref(false)
|
|
73
|
+
const savedTimeout = useTimeoutFn(() => {
|
|
74
|
+
savedNotif.value = false
|
|
75
|
+
}, 1000)
|
|
76
|
+
|
|
77
|
+
async function savePreset () {
|
|
78
|
+
if (!canEdit.value) return
|
|
79
|
+
|
|
80
|
+
const preset = presetStates.value.get(selectedOption.value)
|
|
81
|
+
preset.state = clone(toRawDeep(props.variant.state))
|
|
82
|
+
savedNotif.value = true
|
|
83
|
+
savedTimeout.start()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function deletePreset (id) {
|
|
87
|
+
if (!confirm('Are you sure you want to delete this preset?')) {
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (selectedOption.value === id) {
|
|
92
|
+
resetState()
|
|
93
|
+
}
|
|
94
|
+
presetStates.value.delete(id)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function startEditing () {
|
|
98
|
+
if (!canEdit.value) {
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
isEditing.value = true
|
|
103
|
+
await nextTick()
|
|
104
|
+
input.value.select()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function stopEditing () {
|
|
108
|
+
isEditing.value = false
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
onClickOutside(select, stopEditing)
|
|
112
|
+
</script>
|
|
113
|
+
|
|
114
|
+
<template>
|
|
115
|
+
<div class="htw-flex htw-gap-2 htw-w-full htw-items-center">
|
|
116
|
+
<div
|
|
117
|
+
ref="select"
|
|
118
|
+
class="htw-flex-1 htw-min-w-0"
|
|
119
|
+
>
|
|
120
|
+
<BaseSelect
|
|
121
|
+
v-model="selectedOption"
|
|
122
|
+
:options="presetsOptions"
|
|
123
|
+
@dblclick="startEditing()"
|
|
124
|
+
@keydown.enter="stopEditing()"
|
|
125
|
+
@keydown.escape="stopEditing()"
|
|
126
|
+
@select="id => applyPreset(id)"
|
|
127
|
+
>
|
|
128
|
+
<template #default="{ label }">
|
|
129
|
+
<input
|
|
130
|
+
v-if="isEditing"
|
|
131
|
+
ref="input"
|
|
132
|
+
v-model="presetStates.get(selectedOption).label"
|
|
133
|
+
type="text"
|
|
134
|
+
class="htw-text-inherit htw-bg-transparent htw-w-full htw-h-full htw-outline-none"
|
|
135
|
+
@click.stop.prevent
|
|
136
|
+
>
|
|
137
|
+
|
|
138
|
+
<div
|
|
139
|
+
v-else
|
|
140
|
+
class="htw-flex htw-items-center htw-gap-2"
|
|
141
|
+
>
|
|
142
|
+
<span class="htw-flex-1 htw-truncate">
|
|
143
|
+
{{ label }}
|
|
144
|
+
</span>
|
|
145
|
+
<Icon
|
|
146
|
+
v-if="canEdit"
|
|
147
|
+
v-tooltip="'Rename this preset'"
|
|
148
|
+
icon="carbon:edit"
|
|
149
|
+
class="htw-flex-none htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
150
|
+
@click.stop="startEditing()"
|
|
151
|
+
/>
|
|
152
|
+
</div>
|
|
153
|
+
</template>
|
|
154
|
+
|
|
155
|
+
<template #option="{ label, value }">
|
|
156
|
+
<div class="htw-flex htw-gap-2 htw-items-center">
|
|
157
|
+
<span
|
|
158
|
+
class="htw-flex-1 htw-truncate"
|
|
159
|
+
>{{ label }}</span>
|
|
160
|
+
<Icon
|
|
161
|
+
v-if="value !== DEFAULT_ID"
|
|
162
|
+
v-tooltip="'Delete this preset'"
|
|
163
|
+
icon="carbon:trash-can"
|
|
164
|
+
class="htw-flex-none htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
165
|
+
@click.stop="deletePreset(value)"
|
|
166
|
+
/>
|
|
167
|
+
</div>
|
|
168
|
+
</template>
|
|
169
|
+
</BaseSelect>
|
|
170
|
+
</div>
|
|
171
|
+
<Icon
|
|
172
|
+
v-tooltip="savedNotif ? 'Saved!' : canEdit ? 'Save to preset' : null"
|
|
173
|
+
:icon="savedNotif ? 'carbon:checkmark' : 'carbon:save'"
|
|
174
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
175
|
+
:class="[
|
|
176
|
+
canEdit ? 'htw-opacity-50 hover:htw-opacity-100' : 'htw-opacity-25 htw-pointer-events-none',
|
|
177
|
+
]"
|
|
178
|
+
@click="savePreset()"
|
|
179
|
+
/>
|
|
180
|
+
<Icon
|
|
181
|
+
v-tooltip="'Create new preset'"
|
|
182
|
+
icon="carbon:add-alt"
|
|
183
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
184
|
+
@click="createPreset()"
|
|
185
|
+
/>
|
|
186
|
+
<Icon
|
|
187
|
+
v-tooltip="'Reset to initial state'"
|
|
188
|
+
icon="carbon:reset"
|
|
189
|
+
class="htw-cursor-pointer htw-w-4 htw-h-4 hover:htw-text-primary-500 htw-opacity-50 hover:htw-opacity-100 dark:hover:htw-text-primary-400 htw-text-gray-900 dark:htw-text-gray-100"
|
|
190
|
+
@click="resetState()"
|
|
191
|
+
/>
|
|
192
|
+
</div>
|
|
193
|
+
</template>
|
|
@@ -4,6 +4,7 @@ import { Icon } from '@iconify/vue'
|
|
|
4
4
|
import type { Story, Variant } from '../../types'
|
|
5
5
|
import SandboxVue3 from '../sandbox/SandboxVue3.vue'
|
|
6
6
|
import BaseEmpty from '../base/BaseEmpty.vue'
|
|
7
|
+
import StatePresets from './StatePresets.vue'
|
|
7
8
|
|
|
8
9
|
defineProps({
|
|
9
10
|
variant: {
|
|
@@ -19,7 +20,20 @@ defineProps({
|
|
|
19
20
|
</script>
|
|
20
21
|
|
|
21
22
|
<template>
|
|
22
|
-
<div
|
|
23
|
+
<div
|
|
24
|
+
data-test-id="story-controls"
|
|
25
|
+
class="htw-flex htw-flex-col"
|
|
26
|
+
>
|
|
27
|
+
<!-- Toolbar -->
|
|
28
|
+
<div
|
|
29
|
+
class="htw-h-9 htw-flex-none htw-border-b htw-border-solid htw-border-gray-100 dark:htw-border-gray-750 htw-px-2 htw-flex htw-items-center"
|
|
30
|
+
>
|
|
31
|
+
<StatePresets
|
|
32
|
+
:story="story"
|
|
33
|
+
:variant="variant"
|
|
34
|
+
/>
|
|
35
|
+
</div>
|
|
36
|
+
|
|
23
37
|
<!-- Custom controls -->
|
|
24
38
|
<SandboxVue3
|
|
25
39
|
v-if="variant.slots().controls || story.slots().controls"
|
|
@@ -6,11 +6,11 @@ import { STATE_SYNC, PREVIEW_SETTINGS_SYNC, SANDBOX_READY, EVENT_SEND } from '..
|
|
|
6
6
|
import type { Story, Variant } from '../../types'
|
|
7
7
|
import HatchedPattern from '../misc/HatchedPattern.vue'
|
|
8
8
|
import CheckerboardPattern from '../misc/CheckerboardPattern.vue'
|
|
9
|
-
import { toRawDeep } from '../../util/reactivity'
|
|
10
9
|
import { getSandboxUrl } from '../sandbox/lib'
|
|
11
10
|
import { usePreviewSettingsStore } from '../../stores/preview-settings'
|
|
12
11
|
import { HstEvent, useEventsStore } from '../../stores/events'
|
|
13
12
|
import StoryResponsivePreview from './StoryResponsivePreview.vue'
|
|
13
|
+
import { applyStateToVariant, toRawDeep } from '../../util/state'
|
|
14
14
|
|
|
15
15
|
const props = defineProps<{
|
|
16
16
|
story: Story
|
|
@@ -65,19 +65,7 @@ useEventListener(window, 'message', (event) => {
|
|
|
65
65
|
|
|
66
66
|
function updateVariantState (state: any) {
|
|
67
67
|
synced = true
|
|
68
|
-
|
|
69
|
-
for (const key in state) {
|
|
70
|
-
if (typeof props.variant.state[key] === 'object') {
|
|
71
|
-
Object.assign(props.variant.state[key], state[key])
|
|
72
|
-
} else {
|
|
73
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
74
|
-
props.variant.state[key] = state[key]
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
} else {
|
|
78
|
-
// eslint-disable-next-line vue/no-mutating-props
|
|
79
|
-
props.variant.state = state
|
|
80
|
-
}
|
|
68
|
+
applyStateToVariant(props.variant, state)
|
|
81
69
|
}
|
|
82
70
|
|
|
83
71
|
function logEvent (event: HstEvent) {
|
package/src/client/app/index.ts
CHANGED
|
@@ -29,4 +29,10 @@ export function mountMainApp () {
|
|
|
29
29
|
app.use(router)
|
|
30
30
|
registerGlobalComponents(app)
|
|
31
31
|
app.mount('#app')
|
|
32
|
+
|
|
33
|
+
// @ts-expect-error vite hot api
|
|
34
|
+
if (import.meta.hot) {
|
|
35
|
+
// @ts-expect-error vite hot api
|
|
36
|
+
import.meta.hot.send('histoire:mount', {})
|
|
37
|
+
}
|
|
32
38
|
}
|
|
@@ -11,7 +11,7 @@ import { PREVIEW_SETTINGS_SYNC, STATE_SYNC, SANDBOX_READY } from './util/const.j
|
|
|
11
11
|
import { applyPreviewSettings } from './util/preview-settings.js'
|
|
12
12
|
import { isDark } from './util/dark.js'
|
|
13
13
|
import { histoireConfig } from './util/config.js'
|
|
14
|
-
import { toRawDeep } from './util/
|
|
14
|
+
import { applyStateToVariant, toRawDeep } from './util/state'
|
|
15
15
|
|
|
16
16
|
const query = parseQuery(window.location.search)
|
|
17
17
|
const file = ref<StoryFile>(mapFile(files.find(f => f.id === query.storyId)))
|
|
@@ -30,15 +30,7 @@ const app = createApp({
|
|
|
30
30
|
if (event.data?.type === STATE_SYNC) {
|
|
31
31
|
if (!mounted) return
|
|
32
32
|
synced = true
|
|
33
|
-
|
|
34
|
-
for (const key in event.data.state) {
|
|
35
|
-
if (typeof variant.value.state[key] === 'object') {
|
|
36
|
-
Object.assign(variant.value.state[key], event.data.state[key])
|
|
37
|
-
} else {
|
|
38
|
-
variant.value.state[key] = event.data.state[key]
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
33
|
+
applyStateToVariant(variant.value, event.data.state)
|
|
42
34
|
} else if (event.data?.type === PREVIEW_SETTINGS_SYNC) {
|
|
43
35
|
applyPreviewSettings(event.data.settings)
|
|
44
36
|
}
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { markRaw } from 'vue'
|
|
2
2
|
import type { StoryFile, Variant } from '../types'
|
|
3
3
|
|
|
4
|
+
const copiedFromExistingVariant = [
|
|
5
|
+
'state',
|
|
6
|
+
'slots',
|
|
7
|
+
'initState',
|
|
8
|
+
'source',
|
|
9
|
+
'responsiveDisabled',
|
|
10
|
+
'setupApp',
|
|
11
|
+
'configReady',
|
|
12
|
+
'previewReady',
|
|
13
|
+
]
|
|
14
|
+
|
|
4
15
|
export function mapFile (file: StoryFile, existingFile?: StoryFile): StoryFile {
|
|
5
16
|
const result: StoryFile = {
|
|
6
17
|
...file,
|
|
@@ -19,7 +30,7 @@ export function mapFile (file: StoryFile, existingFile?: StoryFile): StoryFile {
|
|
|
19
30
|
const variant = result.story.variants[index]
|
|
20
31
|
const existingVariant = existingFile.story.variants[index]
|
|
21
32
|
if (existingVariant) {
|
|
22
|
-
for (const key
|
|
33
|
+
for (const key of copiedFromExistingVariant) {
|
|
23
34
|
if (typeof existingVariant[key] !== 'undefined') {
|
|
24
35
|
if (key === 'state') {
|
|
25
36
|
variant.state = existingVariant.state
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { unref, isRef } from 'vue'
|
|
2
|
+
import type { Variant } from '../types'
|
|
3
|
+
|
|
4
|
+
const isObject = (val) => val !== null && typeof val === 'object'
|
|
5
|
+
|
|
6
|
+
export function toRawDeep (val) {
|
|
7
|
+
const unwrappedValue = isRef(val) ? unref(val) : val
|
|
8
|
+
|
|
9
|
+
if (!isObject(unwrappedValue)) {
|
|
10
|
+
return unwrappedValue
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (Array.isArray(unwrappedValue)) {
|
|
14
|
+
return unwrappedValue.map(toRawDeep)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return toRawObject(unwrappedValue)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const toRawObject = (obj: Record<any, any>) => Object.keys(obj).reduce((raw, key) => {
|
|
21
|
+
raw[key] = toRawDeep(obj[key])
|
|
22
|
+
return raw
|
|
23
|
+
}, {})
|
|
24
|
+
|
|
25
|
+
export function clone (data) {
|
|
26
|
+
try {
|
|
27
|
+
return structuredClone(data)
|
|
28
|
+
} catch (e) {
|
|
29
|
+
console.error(e)
|
|
30
|
+
try {
|
|
31
|
+
return JSON.parse(JSON.stringify(data))
|
|
32
|
+
} catch (e) {
|
|
33
|
+
console.error(e)
|
|
34
|
+
}
|
|
35
|
+
return data
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function applyStateToVariant (variant: Variant, state: any) {
|
|
40
|
+
if (variant.state) {
|
|
41
|
+
for (const key in state) {
|
|
42
|
+
if (typeof variant.state[key] === 'object') {
|
|
43
|
+
Object.assign(variant.state[key], state[key])
|
|
44
|
+
} else {
|
|
45
|
+
variant.state[key] = state[key]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
variant.state = state
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/node/build.ts
CHANGED
|
@@ -29,7 +29,7 @@ export async function build (ctx: Context) {
|
|
|
29
29
|
await findAllStories(ctx)
|
|
30
30
|
|
|
31
31
|
const server = await createViteServer({
|
|
32
|
-
plugins: await createVitePlugins(ctx),
|
|
32
|
+
plugins: await createVitePlugins(true, ctx),
|
|
33
33
|
})
|
|
34
34
|
await server.pluginContainer.buildStart({})
|
|
35
35
|
|
|
@@ -63,7 +63,7 @@ export async function build (ctx: Context) {
|
|
|
63
63
|
const emptyStoryCount = ctx.storyFiles.length - storyCount
|
|
64
64
|
|
|
65
65
|
const results = await viteBuild({
|
|
66
|
-
plugins: await createVitePlugins(ctx),
|
|
66
|
+
plugins: await createVitePlugins(false, ctx),
|
|
67
67
|
build: {
|
|
68
68
|
rollupOptions: {
|
|
69
69
|
input: [
|
|
@@ -87,7 +87,7 @@ export async function build (ctx: Context) {
|
|
|
87
87
|
const result = Array.isArray(results) ? results[0] : results as RollupOutput
|
|
88
88
|
|
|
89
89
|
const resolvedViteConfig = await resolveViteConfig({
|
|
90
|
-
plugins: await createVitePlugins(ctx),
|
|
90
|
+
plugins: await createVitePlugins(false, ctx),
|
|
91
91
|
}, 'build')
|
|
92
92
|
|
|
93
93
|
const styleOutput = result.output.find(o => o.name === 'style.css' && o.type === 'asset')
|
|
@@ -34,10 +34,6 @@ export default async (payload: Payload): Promise<ReturnData> => {
|
|
|
34
34
|
root: payload.root,
|
|
35
35
|
base: payload.base,
|
|
36
36
|
fetchModule (id) {
|
|
37
|
-
// Alias
|
|
38
|
-
if (id.endsWith('vue.runtime.esm-bundler.js')) {
|
|
39
|
-
id = 'vue'
|
|
40
|
-
}
|
|
41
37
|
return rpc.fetchModule(id)
|
|
42
38
|
},
|
|
43
39
|
resolveId (id, importer) {
|
|
@@ -13,7 +13,7 @@ export async function previewCommand (options: PreviewOptions) {
|
|
|
13
13
|
mode: 'build',
|
|
14
14
|
})
|
|
15
15
|
const viteConfig = await resolveViteConfig({
|
|
16
|
-
plugins: await createVitePlugins(ctx),
|
|
16
|
+
plugins: await createVitePlugins(false, ctx),
|
|
17
17
|
}, 'build')
|
|
18
18
|
|
|
19
19
|
const { baseUrl } = await startPreview(viteConfig, options.port, ctx)
|
package/src/node/server.ts
CHANGED
|
@@ -9,17 +9,17 @@ import { useModuleLoader } from './load.js'
|
|
|
9
9
|
|
|
10
10
|
export async function createServer (ctx: Context, port: number) {
|
|
11
11
|
const server = await createViteServer({
|
|
12
|
-
plugins: await createVitePlugins(ctx),
|
|
13
|
-
server: {
|
|
14
|
-
watch: {
|
|
15
|
-
usePolling: true,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
12
|
+
plugins: await createVitePlugins(false, ctx),
|
|
18
13
|
})
|
|
19
14
|
await server.pluginContainer.buildStart({})
|
|
20
15
|
|
|
16
|
+
const nodeServer = await createViteServer({
|
|
17
|
+
plugins: await createVitePlugins(true, ctx),
|
|
18
|
+
})
|
|
19
|
+
await nodeServer.pluginContainer.buildStart({})
|
|
20
|
+
|
|
21
21
|
const moduleLoader = useModuleLoader({
|
|
22
|
-
server,
|
|
22
|
+
server: nodeServer,
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
const pluginOnCleanups: (() => void | Promise<void>)[] = []
|
|
@@ -43,7 +43,7 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
43
43
|
executeStoryFile,
|
|
44
44
|
destroy: destroyCollectStories,
|
|
45
45
|
} = useCollectStories({
|
|
46
|
-
server,
|
|
46
|
+
server: nodeServer,
|
|
47
47
|
}, ctx)
|
|
48
48
|
|
|
49
49
|
// onStoryChange debouncing
|
|
@@ -51,11 +51,44 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
51
51
|
let queueResolve: () => void
|
|
52
52
|
let queued = false
|
|
53
53
|
let queuedFiles: StoryFile[] = []
|
|
54
|
+
let queuedAll = false
|
|
55
|
+
let didAllStoriesYet = false
|
|
56
|
+
|
|
57
|
+
// Invalidate modules
|
|
58
|
+
const invalidateModule = (id: string) => {
|
|
59
|
+
const mod = server.moduleGraph.getModuleById(id)
|
|
60
|
+
if (!mod) {
|
|
61
|
+
return
|
|
62
|
+
}
|
|
63
|
+
server.moduleGraph.invalidateModule(mod)
|
|
64
|
+
|
|
65
|
+
// Send HMR update
|
|
66
|
+
const timestamp = Date.now()
|
|
67
|
+
mod.lastHMRTimestamp = timestamp
|
|
68
|
+
server.ws.send({
|
|
69
|
+
type: 'update',
|
|
70
|
+
updates: [
|
|
71
|
+
{
|
|
72
|
+
type: 'js-update',
|
|
73
|
+
acceptedPath: mod.url,
|
|
74
|
+
path: mod.url,
|
|
75
|
+
timestamp: timestamp,
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
})
|
|
79
|
+
}
|
|
54
80
|
|
|
55
81
|
onStoryChange(async (changedFile) => {
|
|
82
|
+
if (changedFile && !didAllStoriesYet) {
|
|
83
|
+
return
|
|
84
|
+
}
|
|
56
85
|
if (queue) {
|
|
57
|
-
if (queued
|
|
58
|
-
|
|
86
|
+
if (queued) {
|
|
87
|
+
if (changedFile) {
|
|
88
|
+
queuedFiles.push(changedFile)
|
|
89
|
+
} else {
|
|
90
|
+
queuedAll = true
|
|
91
|
+
}
|
|
59
92
|
return
|
|
60
93
|
} else {
|
|
61
94
|
queued = true
|
|
@@ -70,47 +103,29 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
70
103
|
|
|
71
104
|
clearCache()
|
|
72
105
|
|
|
73
|
-
|
|
106
|
+
console.log('Collect stories start', changedFile?.path ?? 'all')
|
|
107
|
+
const time = Date.now()
|
|
108
|
+
if (changedFile && !queuedAll) {
|
|
74
109
|
// Granular update
|
|
75
110
|
await executeStoryFile(changedFile)
|
|
111
|
+
|
|
112
|
+
// Queued updates
|
|
113
|
+
for (const storyFile of queuedFiles) {
|
|
114
|
+
await executeStoryFile(storyFile)
|
|
115
|
+
}
|
|
76
116
|
} else {
|
|
77
117
|
// Full update
|
|
78
118
|
for (const storyFile of ctx.storyFiles) {
|
|
79
119
|
await executeStoryFile(storyFile)
|
|
80
120
|
}
|
|
121
|
+
didAllStoriesYet = true
|
|
81
122
|
}
|
|
82
|
-
|
|
83
|
-
for (const storyFile of queuedFiles) {
|
|
84
|
-
await executeStoryFile(storyFile)
|
|
85
|
-
}
|
|
123
|
+
console.log('Collect stories end', Date.now() - time, 'ms')
|
|
86
124
|
|
|
87
125
|
queuedFiles = []
|
|
126
|
+
queuedAll = false
|
|
88
127
|
queueResolve()
|
|
89
128
|
|
|
90
|
-
// Invalidate modules
|
|
91
|
-
function invalidateModule (id: string) {
|
|
92
|
-
const mod = server.moduleGraph.getModuleById(id)
|
|
93
|
-
if (!mod) {
|
|
94
|
-
return
|
|
95
|
-
}
|
|
96
|
-
server.moduleGraph.invalidateModule(mod)
|
|
97
|
-
|
|
98
|
-
// Send HMR update
|
|
99
|
-
const timestamp = Date.now()
|
|
100
|
-
mod.lastHMRTimestamp = timestamp
|
|
101
|
-
server.ws.send({
|
|
102
|
-
type: 'update',
|
|
103
|
-
updates: [
|
|
104
|
-
{
|
|
105
|
-
type: 'js-update',
|
|
106
|
-
acceptedPath: mod.url,
|
|
107
|
-
path: mod.url,
|
|
108
|
-
timestamp: timestamp,
|
|
109
|
-
},
|
|
110
|
-
],
|
|
111
|
-
})
|
|
112
|
-
}
|
|
113
|
-
|
|
114
129
|
invalidateModule(RESOLVED_STORIES_ID)
|
|
115
130
|
invalidateModule(RESOLVED_SEARCH_TITLE_DATA_ID)
|
|
116
131
|
})
|
|
@@ -120,17 +135,10 @@ export async function createServer (ctx: Context, port: number) {
|
|
|
120
135
|
await cb()
|
|
121
136
|
}
|
|
122
137
|
await server.close()
|
|
138
|
+
await nodeServer.close()
|
|
123
139
|
await destroyCollectStories()
|
|
124
140
|
}
|
|
125
141
|
|
|
126
|
-
// On page refresh, refresh stories
|
|
127
|
-
// Useful when vite optimizes new dependencies
|
|
128
|
-
server.ws.on('connection', (socket) => {
|
|
129
|
-
if (socket.protocol === 'vite-hmr') {
|
|
130
|
-
notifyStoryChange()
|
|
131
|
-
}
|
|
132
|
-
})
|
|
133
|
-
|
|
134
142
|
return {
|
|
135
143
|
server,
|
|
136
144
|
close,
|
package/src/node/vite.ts
CHANGED
|
@@ -25,7 +25,7 @@ export const RESOLVED_SEARCH_TITLE_DATA_ID = `/${SEARCH_TITLE_DATA_ID}-resolved`
|
|
|
25
25
|
export const SEARCH_DOCS_DATA_ID = '$histoire-search-docs-data'
|
|
26
26
|
export const RESOLVED_SEARCH_DOCS_DATA_ID = `/${SEARCH_DOCS_DATA_ID}-resolved`
|
|
27
27
|
|
|
28
|
-
export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
|
|
28
|
+
export async function createVitePlugins (server: boolean, ctx: Context): Promise<VitePlugin[]> {
|
|
29
29
|
const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build')
|
|
30
30
|
|
|
31
31
|
const plugins: VitePlugin[] = []
|
|
@@ -45,9 +45,15 @@ export async function createVitePlugins (ctx: Context): Promise<VitePlugin[]> {
|
|
|
45
45
|
config () {
|
|
46
46
|
return {
|
|
47
47
|
optimizeDeps: {
|
|
48
|
-
|
|
48
|
+
disabled: server,
|
|
49
|
+
entries: [
|
|
50
|
+
`${APP_PATH}/bundle-main.js`,
|
|
51
|
+
`${APP_PATH}/bundle-sandbox.js`,
|
|
52
|
+
`${APP_PATH}/server/index.js`,
|
|
53
|
+
],
|
|
49
54
|
include: [
|
|
50
55
|
'vue',
|
|
56
|
+
dirname(require.resolve('@vue/runtime-core/package.json')),
|
|
51
57
|
dirname(require.resolve('shiki/package.json')),
|
|
52
58
|
dirname(require.resolve('vscode-textmate/package.json')),
|
|
53
59
|
dirname(require.resolve('vscode-oniguruma/package.json')),
|
|
@@ -170,6 +176,10 @@ if (import.meta.hot) {
|
|
|
170
176
|
},
|
|
171
177
|
|
|
172
178
|
configureServer (server) {
|
|
179
|
+
server.ws.on('histoire:mount', () => {
|
|
180
|
+
notifyStoryChange()
|
|
181
|
+
})
|
|
182
|
+
|
|
173
183
|
server.middlewares.use(async (req, res, next) => {
|
|
174
184
|
if (req.url!.startsWith(`${server.config.base}__sandbox`)) {
|
|
175
185
|
res.statusCode = 200
|
package/vite.config.ts
CHANGED
|
@@ -1,9 +1,34 @@
|
|
|
1
1
|
import { defineConfig } from 'vite'
|
|
2
2
|
import vue from '@vitejs/plugin-vue'
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
import path from 'pathe'
|
|
3
5
|
|
|
4
6
|
export default defineConfig({
|
|
5
7
|
plugins: [
|
|
6
8
|
vue(),
|
|
9
|
+
{
|
|
10
|
+
name: 'histoire:preserve:import.meta',
|
|
11
|
+
enforce: 'pre',
|
|
12
|
+
transform (code) {
|
|
13
|
+
if (code.includes('import.meta')) {
|
|
14
|
+
return {
|
|
15
|
+
code: code.replace(/import\.meta/g, 'import__meta'),
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
closeBundle () {
|
|
20
|
+
const files = fs.readdirSync('./dist/bundled')
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
if (file.endsWith('.js')) {
|
|
23
|
+
const filePath = path.join('./dist/bundled', file)
|
|
24
|
+
const content = fs.readFileSync(filePath, 'utf-8')
|
|
25
|
+
if (content.includes('import__meta')) {
|
|
26
|
+
fs.writeFileSync(filePath, content.replace(/import__meta/g, 'import.meta'), 'utf-8')
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
},
|
|
7
32
|
],
|
|
8
33
|
|
|
9
34
|
build: {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function toRawDeep(val: any): any;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { unref, isRef } from 'vue';
|
|
2
|
-
const isObject = (val) => val !== null && typeof val === 'object';
|
|
3
|
-
export function toRawDeep(val) {
|
|
4
|
-
const unwrappedValue = isRef(val) ? unref(val) : val;
|
|
5
|
-
if (!isObject(unwrappedValue)) {
|
|
6
|
-
return unwrappedValue;
|
|
7
|
-
}
|
|
8
|
-
if (Array.isArray(unwrappedValue)) {
|
|
9
|
-
return unwrappedValue.map(toRawDeep);
|
|
10
|
-
}
|
|
11
|
-
return toRawObject(unwrappedValue);
|
|
12
|
-
}
|
|
13
|
-
const toRawObject = (obj) => Object.keys(obj).reduce((raw, key) => {
|
|
14
|
-
raw[key] = toRawDeep(obj[key]);
|
|
15
|
-
return raw;
|
|
16
|
-
}, {});
|