histoire 0.2.5 → 0.3.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/dist/client/app/App.vue +8 -3
- package/dist/client/app/codegen/const.d.ts +1 -0
- package/dist/client/app/codegen/const.js +3 -0
- package/dist/client/app/codegen/vue3.js +82 -21
- package/dist/client/app/components/base/BaseEmpty.vue +17 -1
- package/dist/client/app/components/exposed/Story.vue +22 -1
- package/dist/client/app/components/exposed/Variant.vue +13 -1
- package/dist/client/app/components/sandbox/SandboxVue3.vue +6 -4
- package/dist/client/app/components/sandbox/lib.js +2 -1
- package/dist/client/app/components/search/SearchLoading.vue +16 -0
- package/dist/client/app/components/search/SearchModal.vue +12 -149
- package/dist/client/app/components/search/SearchPane.vue +210 -0
- package/dist/client/app/components/search/search-data.d.ts +2 -0
- package/dist/client/app/components/search/search-data.js +7 -0
- package/dist/client/app/components/story/StoryControls.vue +5 -0
- package/dist/client/app/components/story/StoryDocs.vue +18 -11
- package/dist/client/app/components/story/StoryEvent.vue +53 -0
- package/dist/client/app/components/story/StoryEvents.vue +45 -0
- package/dist/client/app/components/story/StoryResponsivePreview.vue +41 -8
- package/dist/client/app/components/story/StorySidePanel.vue +28 -3
- package/dist/client/app/components/story/StorySourceCode.vue +24 -14
- package/dist/client/app/components/story/StoryView.vue +16 -2
- package/dist/client/app/components/tree/StoryGroup.vue +4 -4
- package/dist/client/app/components/tree/StoryListFolder.vue +4 -4
- package/dist/client/app/components/tree/StoryListItem.vue +4 -1
- package/dist/client/app/router.d.ts +1 -0
- package/dist/client/app/router.js +3 -1
- package/dist/client/app/sandbox.js +11 -1
- package/dist/client/app/stores/events.d.ts +29 -0
- package/dist/client/app/stores/events.js +25 -0
- package/dist/client/app/stores/folder.d.ts +10 -0
- package/dist/client/app/stores/folder.js +47 -0
- package/dist/client/app/stores/story.d.ts +14 -8
- package/dist/client/app/stores/story.js +20 -33
- package/dist/client/app/style/main.pcss +5 -0
- package/dist/client/app/style/sandbox.css +2 -0
- package/dist/client/app/types.d.ts +1 -0
- package/dist/client/app/util/const.d.ts +1 -0
- package/dist/client/app/util/const.js +1 -0
- package/dist/client/app/util/events.d.ts +1 -0
- package/dist/client/app/util/events.js +31 -0
- package/dist/client/app/util/mapping.js +2 -2
- package/dist/client/index.d.ts +2 -1
- package/dist/client/index.js +2 -1
- package/dist/client/server/Story.server.vue +13 -2
- package/dist/node/alias.d.ts +2 -1
- package/dist/node/alias.js +3 -2
- package/dist/node/build.js +74 -21
- package/dist/node/builtin-plugins/tailwind-tokens.d.ts +5 -0
- package/dist/node/builtin-plugins/tailwind-tokens.js +510 -0
- package/dist/node/collect/index.js +3 -1
- package/dist/node/commands/preview.js +4 -25
- package/dist/node/config.d.ts +103 -3
- package/dist/node/config.js +40 -23
- package/dist/node/context.d.ts +3 -3
- package/dist/node/context.js +1 -1
- package/dist/node/index.d.ts +1 -0
- package/dist/node/index.js +1 -0
- package/dist/node/load.d.ts +12 -0
- package/dist/node/load.js +42 -0
- package/dist/node/plugin.d.ts +67 -11
- package/dist/node/plugin.js +42 -245
- package/dist/node/preview.d.ts +8 -0
- package/dist/node/preview.js +41 -0
- package/dist/node/search.d.ts +2 -0
- package/dist/node/search.js +68 -0
- package/dist/node/server.js +43 -21
- package/dist/node/stories.d.ts +2 -0
- package/dist/node/stories.js +4 -4
- package/dist/node/types.d.ts +1 -0
- package/dist/node/util/find-up.d.ts +1 -0
- package/dist/node/util/find-up.js +20 -0
- package/dist/node/vite.d.ts +13 -0
- package/dist/node/vite.js +278 -0
- package/dist/style.css +111 -47
- package/package.json +5 -3
- package/scripts/copy.mjs +2 -1
- package/src/client/app/App.vue +8 -3
- package/src/client/app/codegen/const.ts +3 -0
- package/src/client/app/codegen/vue3.ts +80 -22
- package/src/client/app/components/base/BaseEmpty.vue +17 -1
- package/src/client/app/components/exposed/Story.vue +22 -1
- package/src/client/app/components/exposed/Variant.vue +13 -1
- package/src/client/app/components/sandbox/SandboxVue3.vue +6 -4
- package/src/client/app/components/sandbox/lib.ts +3 -1
- package/src/client/app/components/search/SearchLoading.vue +16 -0
- package/src/client/app/components/search/SearchModal.vue +12 -149
- package/src/client/app/components/search/SearchPane.vue +210 -0
- package/src/client/app/components/search/search-data.ts +9 -0
- package/src/client/app/components/story/StoryControls.vue +5 -0
- package/src/client/app/components/story/StoryDocs.vue +18 -11
- package/src/client/app/components/story/StoryEvent.vue +53 -0
- package/src/client/app/components/story/StoryEvents.vue +45 -0
- package/src/client/app/components/story/StoryResponsivePreview.vue +41 -8
- package/src/client/app/components/story/StorySidePanel.vue +28 -3
- package/src/client/app/components/story/StorySourceCode.vue +24 -14
- package/src/client/app/components/story/StoryView.vue +16 -2
- package/src/client/app/components/tree/StoryGroup.vue +4 -4
- package/src/client/app/components/tree/StoryListFolder.vue +4 -4
- package/src/client/app/components/tree/StoryListItem.vue +4 -1
- package/src/client/app/router.ts +4 -1
- package/src/client/app/sandbox.ts +12 -2
- package/src/client/app/stores/events.ts +36 -0
- package/{dist/client/app/stores/story.ts → src/client/app/stores/folder.ts} +16 -23
- package/src/client/app/stores/story.ts +21 -40
- package/src/client/app/style/main.pcss +5 -0
- package/src/client/app/style/sandbox.css +2 -0
- package/src/client/app/types.ts +1 -0
- package/src/client/app/util/const.ts +1 -0
- package/src/client/app/util/events.ts +30 -0
- package/src/client/app/util/mapping.ts +2 -2
- package/src/client/index.ts +2 -1
- package/src/client/server/Story.server.vue +13 -2
- package/src/node/alias.ts +4 -2
- package/src/node/build.ts +86 -21
- package/src/node/builtin-plugins/tailwind-tokens.ts +521 -0
- package/src/node/collect/index.ts +3 -1
- package/src/node/commands/preview.ts +4 -33
- package/src/node/config.ts +53 -27
- package/src/node/context.ts +3 -3
- package/src/node/index.ts +1 -0
- package/src/node/load.ts +63 -0
- package/src/node/plugin.ts +75 -252
- package/src/node/preview.ts +56 -0
- package/src/node/search.ts +75 -0
- package/src/node/server.ts +49 -23
- package/src/node/stories.ts +4 -4
- package/src/node/types.ts +1 -0
- package/src/node/util/find-up.ts +24 -0
- package/src/node/vite.ts +296 -0
- package/tailwind.config.cjs +1 -0
- package/dist/client/app/codegen/serialize-js.ts +0 -119
- package/dist/client/app/codegen/util.ts +0 -79
- package/dist/client/app/codegen/vue3.ts +0 -276
- package/dist/client/app/components/sandbox/lib.ts +0 -8
- package/dist/client/app/global-components.ts +0 -13
- package/dist/client/app/index.ts +0 -28
- package/dist/client/app/router.ts +0 -17
- package/dist/client/app/sandbox.ts +0 -94
- package/dist/client/app/stores/preview-settings.ts +0 -17
- package/dist/client/app/types.ts +0 -78
- package/dist/client/app/util/config.ts +0 -6
- package/dist/client/app/util/const.ts +0 -3
- package/dist/client/app/util/dark.ts +0 -4
- package/dist/client/app/util/env.ts +0 -1
- package/dist/client/app/util/keyboard.ts +0 -99
- package/dist/client/app/util/mapping.ts +0 -48
- package/dist/client/app/util/preview-settings.ts +0 -8
- package/dist/client/app/util/reactivity.ts +0 -22
- package/dist/client/app/util/responsive.ts +0 -3
- package/dist/client/app/util/scroll.ts +0 -31
- package/dist/client/app/util/tooltip.ts +0 -13
- package/dist/client/app/util/variant.ts +0 -20
- package/dist/client/index.ts +0 -12
- package/dist/client/server/index.ts +0 -32
- package/dist/client/shim.d.ts +0 -5
package/dist/node/plugin.js
CHANGED
|
@@ -1,252 +1,49 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export async function createVitePlugins(ctx) {
|
|
20
|
-
const viteConfig = await resolveViteConfig({}, ctx.mode === 'dev' ? 'serve' : 'build');
|
|
21
|
-
const hasVuePlugin = viteConfig.plugins.find(p => p.name === 'vite:vue');
|
|
22
|
-
const plugins = [];
|
|
23
|
-
if (!hasVuePlugin) {
|
|
24
|
-
plugins.push((await import('@vitejs/plugin-vue')).default());
|
|
1
|
+
import chokidar from 'chokidar';
|
|
2
|
+
import path from 'pathe';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import pc from 'picocolors';
|
|
5
|
+
import { TEMP_PATH } from './alias.js';
|
|
6
|
+
import { addStory, removeStory } from './stories.js';
|
|
7
|
+
export class BasePluginApi {
|
|
8
|
+
ctx;
|
|
9
|
+
plugin;
|
|
10
|
+
moduleLoader;
|
|
11
|
+
colors = pc;
|
|
12
|
+
path = path;
|
|
13
|
+
fs = fs;
|
|
14
|
+
// eslint-disable-next-line no-useless-constructor
|
|
15
|
+
constructor(ctx, plugin, moduleLoader) {
|
|
16
|
+
this.ctx = ctx;
|
|
17
|
+
this.plugin = plugin;
|
|
18
|
+
this.moduleLoader = moduleLoader;
|
|
25
19
|
}
|
|
26
|
-
|
|
27
|
-
plugins.
|
|
28
|
-
name: 'histoire-vite-config-override',
|
|
29
|
-
config(config, env) {
|
|
30
|
-
return typeof ctx.config.vite === 'function' ? ctx.config.vite(config, env) : ctx.config.vite;
|
|
31
|
-
},
|
|
32
|
-
});
|
|
20
|
+
get pluginTempDir() {
|
|
21
|
+
return path.resolve(TEMP_PATH, 'plugins', this.plugin.name);
|
|
33
22
|
}
|
|
34
|
-
|
|
35
|
-
name
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
dirname(require.resolve('@vue/runtime-core/package.json')),
|
|
43
|
-
dirname(require.resolve('shiki/package.json')),
|
|
44
|
-
dirname(require.resolve('vscode-textmate/package.json')),
|
|
45
|
-
dirname(require.resolve('vscode-oniguruma/package.json')),
|
|
46
|
-
dirname(require.resolve('case/package.json')),
|
|
47
|
-
],
|
|
48
|
-
},
|
|
49
|
-
server: {
|
|
50
|
-
fs: {
|
|
51
|
-
allow: [DIST_CLIENT_PATH, viteConfig.root, process.cwd()],
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
},
|
|
56
|
-
async resolveId(id, importer) {
|
|
57
|
-
if (id.startsWith(STORIES_ID)) {
|
|
58
|
-
return RESOLVED_STORIES_ID;
|
|
59
|
-
}
|
|
60
|
-
if (id.startsWith(SETUP_ID)) {
|
|
61
|
-
if (ctx.config.setupFile) {
|
|
62
|
-
return this.resolve(ctx.config.setupFile, importer, {
|
|
63
|
-
skipSelf: true,
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
return NOOP_ID;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (id.startsWith(CONFIG_ID)) {
|
|
71
|
-
return RESOLVED_CONFIG_ID;
|
|
72
|
-
}
|
|
73
|
-
if (id.startsWith(THEME_ID)) {
|
|
74
|
-
return RESOLVED_THEME_ID;
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
load(id) {
|
|
78
|
-
if (id === RESOLVED_STORIES_ID) {
|
|
79
|
-
const resolvedStories = ctx.storyFiles.filter(s => !!s.story);
|
|
80
|
-
const files = resolvedStories.map((file, index) => {
|
|
81
|
-
return {
|
|
82
|
-
id: file.id,
|
|
83
|
-
path: file.treePath,
|
|
84
|
-
story: file.story,
|
|
85
|
-
framework: 'vue3',
|
|
86
|
-
index,
|
|
87
|
-
};
|
|
88
|
-
});
|
|
89
|
-
return `import { defineAsyncComponent } from 'vue'
|
|
90
|
-
${resolvedStories.map((file, index) => `const Comp${index} = defineAsyncComponent(() => import('${file.path}'))`).join('\n')}
|
|
91
|
-
export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}}`).join(',\n')}]
|
|
92
|
-
export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
|
|
93
|
-
const handlers = []
|
|
94
|
-
export function onUpdate (cb) {
|
|
95
|
-
handlers.push(cb)
|
|
96
|
-
}
|
|
97
|
-
if (import.meta.hot) {
|
|
98
|
-
import.meta.hot.accept(newModule => {
|
|
99
|
-
files = newModule.files
|
|
100
|
-
tree = newModule.tree
|
|
101
|
-
handlers.forEach(h => {
|
|
102
|
-
h(newModule.files, newModule.tree)
|
|
103
|
-
newModule.onUpdate(h)
|
|
104
|
-
})
|
|
105
|
-
})
|
|
106
|
-
}`;
|
|
107
|
-
}
|
|
108
|
-
if (id === NOOP_ID) {
|
|
109
|
-
return `export default () => {}`;
|
|
110
|
-
}
|
|
111
|
-
if (id === RESOLVED_CONFIG_ID) {
|
|
112
|
-
let js = `export const config = ${JSON.stringify(ctx.config)}\n`;
|
|
113
|
-
if (ctx.config.theme?.logo) {
|
|
114
|
-
for (const key in ctx.config.theme.logo) {
|
|
115
|
-
js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`;
|
|
119
|
-
return js;
|
|
120
|
-
}
|
|
121
|
-
if (id === RESOLVED_THEME_ID) {
|
|
122
|
-
let css = '*, ::before, ::after {';
|
|
123
|
-
// Colors
|
|
124
|
-
for (const color in ctx.config.theme?.colors ?? {}) {
|
|
125
|
-
for (const key in ctx.config.theme.colors[color]) {
|
|
126
|
-
css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
css += '}';
|
|
130
|
-
return css;
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
handleHotUpdate(updateContext) {
|
|
134
|
-
const story = ctx.storyFiles.find(file => file.path === updateContext.file);
|
|
135
|
-
if (story) {
|
|
136
|
-
notifyStoryChange(story);
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
configureServer(server) {
|
|
140
|
-
server.middlewares.use(async (req, res, next) => {
|
|
141
|
-
if (req.url.startsWith('/__sandbox')) {
|
|
142
|
-
res.statusCode = 200;
|
|
143
|
-
let html = `
|
|
144
|
-
<!DOCTYPE html>
|
|
145
|
-
<html>
|
|
146
|
-
<head>
|
|
147
|
-
<title></title>
|
|
148
|
-
<meta charset="utf-8">
|
|
149
|
-
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
150
|
-
<meta name="description" content="">
|
|
151
|
-
</head>
|
|
152
|
-
<body>
|
|
153
|
-
<div id="app"></div>
|
|
154
|
-
<script>
|
|
155
|
-
// Hide spammy vite messages
|
|
156
|
-
const origConsoleLog = console.log
|
|
157
|
-
console.log = (...args) => {
|
|
158
|
-
if (typeof args[0] !== 'string' || !args[0].startsWith('[vite] connect')) {
|
|
159
|
-
origConsoleLog(...args)
|
|
23
|
+
log(...msg) {
|
|
24
|
+
console.log(this.colors.gray(`[Plugin:${this.plugin.name}]`), ...msg);
|
|
25
|
+
}
|
|
26
|
+
warn(...msg) {
|
|
27
|
+
console.warn(this.colors.yellow(`[Plugin:${this.plugin.name}]`), ...msg);
|
|
28
|
+
}
|
|
29
|
+
error(...msg) {
|
|
30
|
+
console.error(this.colors.red(`[Plugin:${this.plugin.name}]`), ...msg);
|
|
160
31
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
</body>
|
|
165
|
-
</html>`;
|
|
166
|
-
// Apply Vite HTML transforms. This injects the Vite HMR client, and
|
|
167
|
-
// also applies HTML transforms from Vite plugins
|
|
168
|
-
html = await server.transformIndexHtml(req.url, html);
|
|
169
|
-
res.end(html);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
next();
|
|
173
|
-
});
|
|
174
|
-
// serve our index.html after vite history fallback
|
|
175
|
-
return () => {
|
|
176
|
-
server.middlewares.use(async (req, res, next) => {
|
|
177
|
-
if (req.url.endsWith('.html')) {
|
|
178
|
-
res.statusCode = 200;
|
|
179
|
-
let html = `
|
|
180
|
-
<!DOCTYPE html>
|
|
181
|
-
<html>
|
|
182
|
-
<head>
|
|
183
|
-
<title></title>
|
|
184
|
-
<link rel="icon" href=""/>
|
|
185
|
-
<meta charset="utf-8">
|
|
186
|
-
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
187
|
-
<meta name="description" content="">
|
|
188
|
-
${ctx.config.theme?.favicon ? `<link rel="icon" type="${lookupMime(ctx.config.theme.favicon)}" href="${ctx.config.theme.favicon}"/>` : ''}
|
|
189
|
-
</head>
|
|
190
|
-
<body>
|
|
191
|
-
<div id="app"></div>
|
|
192
|
-
<script type="module" src="/@fs/${APP_PATH}/index.js"></script>
|
|
193
|
-
</body>
|
|
194
|
-
</html>`;
|
|
195
|
-
// Apply Vite HTML transforms. This injects the Vite HMR client, and
|
|
196
|
-
// also applies HTML transforms from Vite plugins
|
|
197
|
-
html = await server.transformIndexHtml(req.url, html);
|
|
198
|
-
res.end(html);
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
next();
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
},
|
|
205
|
-
});
|
|
206
|
-
// Custom blocks
|
|
207
|
-
plugins.push(...await createCustomBlocksPlugins(ctx));
|
|
208
|
-
if (ctx.mode === 'build') {
|
|
209
|
-
// Add file name in build mode to have components names instead of <Anonymous>
|
|
210
|
-
const include = [/\.vue$/];
|
|
211
|
-
const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/];
|
|
212
|
-
plugins.push({
|
|
213
|
-
name: 'histoire-file-name-plugin',
|
|
214
|
-
enforce: 'post',
|
|
215
|
-
transform(code, id) {
|
|
216
|
-
if (exclude.some(r => r.test(id)))
|
|
217
|
-
return;
|
|
218
|
-
if (include.some(r => r.test(id))) {
|
|
219
|
-
const file = relative(viteConfig.root, id);
|
|
220
|
-
const index = code.indexOf('export default');
|
|
221
|
-
const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`;
|
|
222
|
-
return result;
|
|
223
|
-
}
|
|
224
|
-
},
|
|
225
|
-
});
|
|
32
|
+
addStoryFile(file) {
|
|
33
|
+
removeStory(file);
|
|
34
|
+
addStory(file);
|
|
226
35
|
}
|
|
227
|
-
return plugins;
|
|
228
36
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
37
|
+
export class DevPluginApi extends BasePluginApi {
|
|
38
|
+
watcher = chokidar;
|
|
39
|
+
}
|
|
40
|
+
export class BuildPluginApi extends BasePluginApi {
|
|
41
|
+
buildEndCallbacks = [];
|
|
42
|
+
previewStoryCallbacks = [];
|
|
43
|
+
onBuildEnd(cb) {
|
|
44
|
+
this.buildEndCallbacks.push(cb);
|
|
45
|
+
}
|
|
46
|
+
onPreviewStory(cb) {
|
|
47
|
+
this.previewStoryCallbacks.push(cb);
|
|
237
48
|
}
|
|
238
|
-
plugins.push({
|
|
239
|
-
name: 'histoire-vue-docs-block',
|
|
240
|
-
transform(code, id) {
|
|
241
|
-
if (!id.includes('?vue&type=docs'))
|
|
242
|
-
return;
|
|
243
|
-
if (!id.includes('lang.md'))
|
|
244
|
-
return;
|
|
245
|
-
const html = md.render(code);
|
|
246
|
-
return `export default Comp => {
|
|
247
|
-
Comp.doc = ${JSON.stringify(html)}
|
|
248
|
-
}`;
|
|
249
|
-
},
|
|
250
|
-
});
|
|
251
|
-
return plugins;
|
|
252
49
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ResolvedConfig as ViteConfig } from 'vite';
|
|
2
|
+
import type { Context } from './context.js';
|
|
3
|
+
interface ReturnPayload {
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
close: () => Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
export declare function startPreview(viteConfig: ViteConfig, port: number | null, ctx: Context): Promise<ReturnPayload>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import connect from 'connect';
|
|
3
|
+
import sirv from 'sirv';
|
|
4
|
+
export async function startPreview(viteConfig, port, ctx) {
|
|
5
|
+
const app = connect();
|
|
6
|
+
app.use(viteConfig.base, sirv(ctx.config.outDir, {
|
|
7
|
+
dev: true,
|
|
8
|
+
etag: true,
|
|
9
|
+
single: true,
|
|
10
|
+
}));
|
|
11
|
+
let finalPort = port ?? 3000;
|
|
12
|
+
const httpServer = http.createServer(app);
|
|
13
|
+
return new Promise((resolve, reject) => {
|
|
14
|
+
function onError(e) {
|
|
15
|
+
if (e.code === 'EADDRINUSE') {
|
|
16
|
+
httpServer.listen(++finalPort);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
reject(e);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
httpServer.on('error', onError);
|
|
23
|
+
httpServer.listen(finalPort, () => {
|
|
24
|
+
httpServer.off('error', onError);
|
|
25
|
+
const baseUrl = `http://localhost:${finalPort}${viteConfig.base}`;
|
|
26
|
+
resolve({
|
|
27
|
+
baseUrl,
|
|
28
|
+
close: () => new Promise((resolve, reject) => {
|
|
29
|
+
httpServer.close((err) => {
|
|
30
|
+
if (err) {
|
|
31
|
+
reject(err);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
resolve();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}),
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
import flexsearch from 'flexsearch';
|
|
3
|
+
import path from 'pathe';
|
|
4
|
+
import { loadModule } from './load.js';
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
export async function generateSearchData(ctx) {
|
|
7
|
+
const flexsearchRoot = path.dirname(require.resolve('flexsearch/package.json'));
|
|
8
|
+
const searchIndex = new flexsearch.Document({
|
|
9
|
+
preset: 'match',
|
|
10
|
+
document: {
|
|
11
|
+
id: 'id',
|
|
12
|
+
index: [
|
|
13
|
+
'title',
|
|
14
|
+
'docs',
|
|
15
|
+
],
|
|
16
|
+
store: [
|
|
17
|
+
'kind',
|
|
18
|
+
],
|
|
19
|
+
},
|
|
20
|
+
charset: await loadModule(`${flexsearchRoot}/dist/module/lang/latin/advanced.js`),
|
|
21
|
+
language: await loadModule(`${flexsearchRoot}/dist/module/lang/en.js`),
|
|
22
|
+
tokenize: 'full',
|
|
23
|
+
});
|
|
24
|
+
for (const storyFile of ctx.storyFiles) {
|
|
25
|
+
if (storyFile.story) {
|
|
26
|
+
searchIndex.add({
|
|
27
|
+
id: storyFile.story.id,
|
|
28
|
+
kind: 'story',
|
|
29
|
+
title: storyFile.story.title,
|
|
30
|
+
docs: '', // @TODO
|
|
31
|
+
});
|
|
32
|
+
for (const variant of storyFile.story.variants) {
|
|
33
|
+
searchIndex.add({
|
|
34
|
+
id: `${storyFile.story.id}:${variant.id}`,
|
|
35
|
+
kind: 'variant',
|
|
36
|
+
title: `${storyFile.story.title} ${variant.title}`,
|
|
37
|
+
docs: '', // @TODO
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return exportSearchIndex(searchIndex);
|
|
43
|
+
}
|
|
44
|
+
const exportKeys = new Set([
|
|
45
|
+
'reg',
|
|
46
|
+
'title.cfg',
|
|
47
|
+
'title.map',
|
|
48
|
+
'title.ctx',
|
|
49
|
+
'docs.cfg',
|
|
50
|
+
'docs.map',
|
|
51
|
+
'docs.ctx',
|
|
52
|
+
'tag',
|
|
53
|
+
'store',
|
|
54
|
+
]);
|
|
55
|
+
async function exportSearchIndex(index) {
|
|
56
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
57
|
+
return new Promise(async (resolve, reject) => {
|
|
58
|
+
const exportedData = {};
|
|
59
|
+
const exportedKeys = new Set();
|
|
60
|
+
await index.export((key, data) => {
|
|
61
|
+
exportedData[key] = data;
|
|
62
|
+
exportedKeys.add(key);
|
|
63
|
+
if (exportedKeys.size === exportKeys.size) {
|
|
64
|
+
resolve(exportedData);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
}
|
package/dist/node/server.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createServer as createViteServer } from 'vite';
|
|
2
|
-
import { createVitePlugins, RESOLVED_STORIES_ID } from './
|
|
2
|
+
import { createVitePlugins, RESOLVED_SEARCH_DATA_ID, RESOLVED_STORIES_ID } from './vite.js';
|
|
3
3
|
import { notifyStoryChange, onStoryChange, watchStories } from './stories.js';
|
|
4
4
|
import { useCollectStories } from './collect/index.js';
|
|
5
|
+
import { DevPluginApi } from './plugin.js';
|
|
6
|
+
import { useModuleLoader } from './load.js';
|
|
5
7
|
export async function createServer(ctx, port) {
|
|
6
|
-
await watchStories(ctx);
|
|
7
8
|
const server = await createViteServer({
|
|
8
9
|
plugins: await createVitePlugins(ctx),
|
|
9
10
|
server: {
|
|
@@ -13,6 +14,20 @@ export async function createServer(ctx, port) {
|
|
|
13
14
|
},
|
|
14
15
|
});
|
|
15
16
|
await server.pluginContainer.buildStart({});
|
|
17
|
+
const moduleLoader = useModuleLoader({
|
|
18
|
+
server,
|
|
19
|
+
});
|
|
20
|
+
const pluginOnCleanups = [];
|
|
21
|
+
for (const plugin of ctx.config.plugins) {
|
|
22
|
+
if (plugin.onDev) {
|
|
23
|
+
const api = new DevPluginApi(ctx, plugin, moduleLoader);
|
|
24
|
+
const onCleanup = (cb) => {
|
|
25
|
+
pluginOnCleanups.push(cb);
|
|
26
|
+
};
|
|
27
|
+
await plugin.onDev(api, onCleanup);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
await watchStories(ctx);
|
|
16
31
|
// Wait for pre-bundling (in `listen()`)
|
|
17
32
|
await server.listen(port);
|
|
18
33
|
const { clearCache, executeStoryFile, destroy: destroyCollectStories, } = useCollectStories({
|
|
@@ -56,28 +71,35 @@ export async function createServer(ctx, port) {
|
|
|
56
71
|
}
|
|
57
72
|
queuedFiles = [];
|
|
58
73
|
queueResolve();
|
|
59
|
-
// Invalidate
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
74
|
+
// Invalidate modules
|
|
75
|
+
function invalidateModule(id) {
|
|
76
|
+
const mod = server.moduleGraph.getModuleById(id);
|
|
77
|
+
if (!mod) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
server.moduleGraph.invalidateModule(mod);
|
|
81
|
+
// Send HMR update
|
|
82
|
+
const timestamp = Date.now();
|
|
83
|
+
mod.lastHMRTimestamp = timestamp;
|
|
84
|
+
server.ws.send({
|
|
85
|
+
type: 'update',
|
|
86
|
+
updates: [
|
|
87
|
+
{
|
|
88
|
+
type: 'js-update',
|
|
89
|
+
acceptedPath: mod.url,
|
|
90
|
+
path: mod.url,
|
|
91
|
+
timestamp: timestamp,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
});
|
|
63
95
|
}
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
});
|
|
96
|
+
invalidateModule(RESOLVED_STORIES_ID);
|
|
97
|
+
invalidateModule(RESOLVED_SEARCH_DATA_ID);
|
|
79
98
|
});
|
|
80
99
|
async function close() {
|
|
100
|
+
for (const cb of pluginOnCleanups) {
|
|
101
|
+
await cb();
|
|
102
|
+
}
|
|
81
103
|
await server.close();
|
|
82
104
|
await destroyCollectStories();
|
|
83
105
|
}
|
package/dist/node/stories.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ declare type StoryChangeHandler = (file?: StoryFile) => unknown;
|
|
|
5
5
|
export declare function onStoryChange(handler: StoryChangeHandler): void;
|
|
6
6
|
export declare function watchStories(newContext: Context): Promise<chokidar.FSWatcher>;
|
|
7
7
|
export declare function notifyStoryChange(file?: StoryFile): void;
|
|
8
|
+
export declare function addStory(relativeFilePath: string): StoryFile;
|
|
9
|
+
export declare function removeStory(relativeFilePath: string): void;
|
|
8
10
|
export declare function findAllStories(newContext: Context): Promise<void>;
|
|
9
11
|
export {};
|
package/dist/node/stories.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import chokidar from 'chokidar';
|
|
2
2
|
import { globby } from 'globby';
|
|
3
3
|
import Case from 'case';
|
|
4
|
-
import {
|
|
4
|
+
import { resolve, basename } from 'pathe';
|
|
5
5
|
const handlers = [];
|
|
6
6
|
let context;
|
|
7
7
|
export function onStoryChange(handler) {
|
|
@@ -30,9 +30,9 @@ export function notifyStoryChange(file) {
|
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
function getAbsoluteFilePath(relativeFilePath) {
|
|
33
|
-
return
|
|
33
|
+
return resolve(context.root, relativeFilePath);
|
|
34
34
|
}
|
|
35
|
-
function addStory(relativeFilePath) {
|
|
35
|
+
export function addStory(relativeFilePath) {
|
|
36
36
|
const absoluteFilePath = getAbsoluteFilePath(relativeFilePath);
|
|
37
37
|
const fileId = Case.kebab(relativeFilePath);
|
|
38
38
|
let fileName = basename(relativeFilePath);
|
|
@@ -48,7 +48,7 @@ function addStory(relativeFilePath) {
|
|
|
48
48
|
context.storyFiles.push(file);
|
|
49
49
|
return file;
|
|
50
50
|
}
|
|
51
|
-
function removeStory(relativeFilePath) {
|
|
51
|
+
export function removeStory(relativeFilePath) {
|
|
52
52
|
const fileId = Case.kebab(relativeFilePath);
|
|
53
53
|
const index = context.storyFiles.findIndex((file) => file.id === fileId);
|
|
54
54
|
if (index !== -1)
|
package/dist/node/types.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findUp(cwd: string, fileNames: string[]): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import path from 'pathe';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
export function findUp(cwd = process.cwd(), fileNames) {
|
|
4
|
+
let { root } = path.parse(cwd);
|
|
5
|
+
let dir = cwd;
|
|
6
|
+
// Fix for windows, waiting for pathe to fix this: https://github.com/unjs/pathe/issues/5
|
|
7
|
+
if (root === '' && dir[1] === ':') {
|
|
8
|
+
root = dir.substring(0, 2);
|
|
9
|
+
}
|
|
10
|
+
while (dir !== root) {
|
|
11
|
+
for (const fileName of fileNames) {
|
|
12
|
+
const searchPath = path.join(dir, fileName);
|
|
13
|
+
if (fs.existsSync(searchPath)) {
|
|
14
|
+
return searchPath;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
dir = path.dirname(dir);
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Plugin as VitePlugin } from 'vite';
|
|
2
|
+
import { Context } from './context.js';
|
|
3
|
+
export declare const STORIES_ID = "$histoire-stories";
|
|
4
|
+
export declare const RESOLVED_STORIES_ID: string;
|
|
5
|
+
export declare const SETUP_ID = "$histoire-setup";
|
|
6
|
+
export declare const NOOP_ID = "/$histoire-noop";
|
|
7
|
+
export declare const CONFIG_ID = "$histoire-config";
|
|
8
|
+
export declare const RESOLVED_CONFIG_ID: string;
|
|
9
|
+
export declare const THEME_ID = "$histoire-theme";
|
|
10
|
+
export declare const RESOLVED_THEME_ID: string;
|
|
11
|
+
export declare const SEARCH_DATA_ID = "$histoire-search-data";
|
|
12
|
+
export declare const RESOLVED_SEARCH_DATA_ID: string;
|
|
13
|
+
export declare function createVitePlugins(ctx: Context): Promise<VitePlugin[]>;
|