histoire 0.12.4 → 0.13.1
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/node/build.js +17 -11
- package/dist/node/builtin-plugins/pinceau-tokens.d.ts +6 -0
- package/dist/node/builtin-plugins/pinceau-tokens.js +505 -0
- package/dist/node/collect/index.js +1 -0
- package/dist/node/index.d.ts +7 -0
- package/dist/node/server.js +7 -6
- package/dist/node/util/vendors.d.ts +1 -0
- package/dist/node/util/vendors.js +7 -2
- package/dist/node/virtual/index.d.ts +34 -0
- package/dist/node/virtual/index.js +46 -0
- package/dist/node/virtual/markdown.d.ts +2 -0
- package/dist/node/virtual/markdown.js +19 -0
- package/dist/node/virtual/noop.d.ts +1 -0
- package/dist/node/virtual/noop.js +1 -0
- package/dist/node/virtual/resolved-config.d.ts +2 -0
- package/dist/node/virtual/resolved-config.js +10 -0
- package/dist/node/virtual/resolved-generated-global-setup.d.ts +2 -0
- package/dist/node/virtual/resolved-generated-global-setup.js +23 -0
- package/dist/node/virtual/resolved-generated-setup-code.d.ts +2 -0
- package/dist/node/virtual/resolved-generated-setup-code.js +5 -0
- package/dist/node/virtual/resolved-markdown-files.d.ts +2 -0
- package/dist/node/virtual/resolved-markdown-files.js +17 -0
- package/dist/node/virtual/resolved-stories.d.ts +2 -0
- package/dist/node/virtual/resolved-stories.js +41 -0
- package/dist/node/virtual/resolved-support-plugins-client.d.ts +2 -0
- package/dist/node/virtual/resolved-support-plugins-client.js +11 -0
- package/dist/node/virtual/resolved-support-plugins-collect.d.ts +2 -0
- package/dist/node/virtual/resolved-support-plugins-collect.js +11 -0
- package/dist/node/virtual/resolved-theme.d.ts +2 -0
- package/dist/node/virtual/resolved-theme.js +12 -0
- package/dist/node/virtual/story-source.d.ts +2 -0
- package/dist/node/virtual/story-source.js +16 -0
- package/dist/node/virtual/story.d.ts +2 -0
- package/dist/node/virtual/story.js +7 -0
- package/dist/node/virtual/util.d.ts +2 -0
- package/dist/node/virtual/util.js +4 -0
- package/dist/node/virtual/vite-plugin.d.ts +3 -0
- package/dist/node/virtual/vite-plugin.js +118 -0
- package/dist/node/vite.d.ts +0 -22
- package/dist/node/vite.js +27 -281
- package/package.json +9 -9
- package/src/node/build.ts +19 -11
- package/src/node/builtin-plugins/pinceau-tokens.ts +521 -0
- package/src/node/collect/index.ts +1 -0
- package/src/node/index.ts +8 -0
- package/src/node/server.ts +7 -6
- package/src/node/util/vendors.ts +9 -2
- package/src/node/virtual/index.ts +47 -0
- package/src/node/virtual/markdown.ts +21 -0
- package/src/node/virtual/noop.ts +1 -0
- package/src/node/virtual/resolved-config.ts +12 -0
- package/src/node/virtual/resolved-generated-global-setup.ts +24 -0
- package/src/node/virtual/resolved-generated-setup-code.ts +7 -0
- package/src/node/virtual/resolved-markdown-files.ts +19 -0
- package/src/node/virtual/resolved-stories.ts +43 -0
- package/src/node/virtual/resolved-support-plugins-client.ts +14 -0
- package/src/node/virtual/resolved-support-plugins-collect.ts +14 -0
- package/src/node/virtual/resolved-theme.ts +14 -0
- package/src/node/virtual/story-source.ts +17 -0
- package/src/node/virtual/story.ts +9 -0
- package/src/node/virtual/util.ts +5 -0
- package/src/node/virtual/vite-plugin.ts +135 -0
- package/src/node/vite.ts +28 -294
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Context } from '../context.js'
|
|
2
|
+
import { GENERATED_SETUP_CODE } from './index.js'
|
|
3
|
+
import { ID_SEPARATOR } from './util.js'
|
|
4
|
+
|
|
5
|
+
export const resolvedGeneratedGlobalSetup = (ctx: Context) => {
|
|
6
|
+
if (ctx.config.setupCode) {
|
|
7
|
+
return [
|
|
8
|
+
// Import
|
|
9
|
+
`${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
|
|
10
|
+
// List
|
|
11
|
+
`const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
|
|
12
|
+
// Setups
|
|
13
|
+
...ctx.supportPlugins.map(p => p.setupFn).map(fnName => `export async function ${fnName} (payload) {
|
|
14
|
+
for (const setup of setupList) {
|
|
15
|
+
if (setup?.${fnName}) {
|
|
16
|
+
await setup.${fnName}(payload)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}`),
|
|
20
|
+
].join('\n')
|
|
21
|
+
} else {
|
|
22
|
+
return ''
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Context } from '../context.js'
|
|
2
|
+
import { getInjectedImport } from '../util/vendors.js'
|
|
3
|
+
|
|
4
|
+
export const resolvedMarkdownFiles = (ctx: Context) => {
|
|
5
|
+
const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`virtual:md:${f.id}`)})`).join(',')
|
|
6
|
+
return `import { reactive } from ${process.env.HISTOIRE_DEV ? `'vue'` : getInjectedImport('@histoire/vendors/vue')}
|
|
7
|
+
export const markdownFiles = reactive({${filesJs}})
|
|
8
|
+
if (import.meta.hot) {
|
|
9
|
+
if (!window.__hst_md_hmr) {
|
|
10
|
+
window.__hst_md_hmr = (newModule) => {
|
|
11
|
+
markdownFiles[newModule.relativePath] = () => newModule
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
import.meta.hot.accept(newModule => {
|
|
16
|
+
Object.assign(markdownFiles, newModule.markdownFiles)
|
|
17
|
+
})
|
|
18
|
+
}`
|
|
19
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Context } from '../context.js'
|
|
2
|
+
import { makeTree } from '../tree.js'
|
|
3
|
+
|
|
4
|
+
export const resolvedStories = (ctx: Context) => {
|
|
5
|
+
const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
|
|
6
|
+
const files = resolvedStories.map((file, index) => {
|
|
7
|
+
return {
|
|
8
|
+
id: file.id,
|
|
9
|
+
path: file.treePath,
|
|
10
|
+
filePath: file.relativePath,
|
|
11
|
+
story: {
|
|
12
|
+
...file.story,
|
|
13
|
+
docsText: undefined,
|
|
14
|
+
},
|
|
15
|
+
supportPluginId: file.supportPluginId,
|
|
16
|
+
index,
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
return `${ctx.supportPlugins.map(p => p.importStoriesPrepend).filter(Boolean).join('\n')}
|
|
20
|
+
${resolvedStories.map((file, index) => {
|
|
21
|
+
const supportPlugin = ctx.supportPlugins.find(p => p.id === file.supportPluginId)
|
|
22
|
+
if (!supportPlugin) {
|
|
23
|
+
throw new Error(`Could not find support plugin for story ${file.path}: ${file.supportPluginId}`)
|
|
24
|
+
}
|
|
25
|
+
return supportPlugin.importStoryComponent(file, index)
|
|
26
|
+
}).filter(Boolean).join('\n')}
|
|
27
|
+
export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}, source: () => import('virtual:story-source:${file.story.id}')}`).join(',\n')}]
|
|
28
|
+
export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
|
|
29
|
+
const handlers = []
|
|
30
|
+
export function onUpdate (cb) {
|
|
31
|
+
handlers.push(cb)
|
|
32
|
+
}
|
|
33
|
+
if (import.meta.hot) {
|
|
34
|
+
import.meta.hot.accept(newModule => {
|
|
35
|
+
files = newModule.files
|
|
36
|
+
tree = newModule.tree
|
|
37
|
+
handlers.forEach(h => {
|
|
38
|
+
h(newModule.files, newModule.tree)
|
|
39
|
+
newModule.onUpdate(h)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
}`
|
|
43
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import type { Context } from '../context.js'
|
|
3
|
+
import { PLUGINS_HAVE_DEV } from './util.js'
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url)
|
|
6
|
+
|
|
7
|
+
export const resolvedSupportPluginsClient = (ctx: Context) => {
|
|
8
|
+
const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/client${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
|
|
9
|
+
paths: [ctx.root, import.meta.url],
|
|
10
|
+
}))})`)
|
|
11
|
+
return `export const clientSupportPlugins = {
|
|
12
|
+
${plugins.join(',\n ')}
|
|
13
|
+
}`
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import type { Context } from '../context.js'
|
|
3
|
+
import { PLUGINS_HAVE_DEV } from './util.js'
|
|
4
|
+
|
|
5
|
+
const require = createRequire(import.meta.url)
|
|
6
|
+
|
|
7
|
+
export const resolvedSupportPluginsCollect = (ctx: Context) => {
|
|
8
|
+
const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/collect${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
|
|
9
|
+
paths: [ctx.root, import.meta.url],
|
|
10
|
+
}))})`)
|
|
11
|
+
return `export const collectSupportPlugins = {
|
|
12
|
+
${plugins.join(',\n ')}
|
|
13
|
+
}`
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { parseColor } from '../colors.js'
|
|
2
|
+
import type { Context } from '../context.js'
|
|
3
|
+
|
|
4
|
+
export const resolvedTheme = (ctx: Context) => {
|
|
5
|
+
let css = '*, ::before, ::after {'
|
|
6
|
+
// Colors
|
|
7
|
+
for (const color in ctx.config.theme?.colors ?? {}) {
|
|
8
|
+
for (const key in ctx.config.theme.colors[color]) {
|
|
9
|
+
css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
css += '}'
|
|
13
|
+
return css
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
import { resolve } from 'pathe'
|
|
3
|
+
import type { Context } from '../context.js'
|
|
4
|
+
|
|
5
|
+
export const storySource = async (ctx: Context, id: string) => {
|
|
6
|
+
const storyId = id.slice('/__resolved__virtual:story-source:'.length)
|
|
7
|
+
const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId)
|
|
8
|
+
if (storyFile) {
|
|
9
|
+
let source: string
|
|
10
|
+
if (storyFile.virtual) {
|
|
11
|
+
source = storyFile.moduleCode
|
|
12
|
+
} else {
|
|
13
|
+
source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8')
|
|
14
|
+
}
|
|
15
|
+
return `export default ${JSON.stringify(source)}`
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context } from '../context.js'
|
|
2
|
+
|
|
3
|
+
export const story = (ctx: Context, id: string) => {
|
|
4
|
+
const moduleId = id.replace('\0', '')
|
|
5
|
+
const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual)
|
|
6
|
+
if (storyFile) {
|
|
7
|
+
return storyFile.moduleCode
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import type { Plugin as VitePlugin } from 'vite'
|
|
2
|
+
import { resolve } from 'pathe'
|
|
3
|
+
import type { Context } from '../context.js'
|
|
4
|
+
import * as VirtualFiles from './index.js'
|
|
5
|
+
import { ID_SEPARATOR } from './util.js'
|
|
6
|
+
import { generateDocSearchData, generateTitleSearchData, getSearchDataJS } from '../search.js'
|
|
7
|
+
|
|
8
|
+
export const createVirtualFilesPlugin = (ctx: Context, isServer: boolean) => ({
|
|
9
|
+
name: 'histoire-virtual-files',
|
|
10
|
+
|
|
11
|
+
async resolveId (id, importer) {
|
|
12
|
+
if (id.startsWith(VirtualFiles.STORIES_ID)) {
|
|
13
|
+
return VirtualFiles.RESOLVED_STORIES_ID
|
|
14
|
+
}
|
|
15
|
+
if (id.startsWith(VirtualFiles.SETUP_ID)) {
|
|
16
|
+
const setupFileConfig = ctx.config.setupFile
|
|
17
|
+
if (setupFileConfig) {
|
|
18
|
+
let file: string
|
|
19
|
+
if (typeof setupFileConfig === 'string') {
|
|
20
|
+
file = setupFileConfig
|
|
21
|
+
} else if (isServer && 'server' in setupFileConfig) {
|
|
22
|
+
file = setupFileConfig.server
|
|
23
|
+
} else if (!isServer && 'browser' in setupFileConfig) {
|
|
24
|
+
file = setupFileConfig.browser
|
|
25
|
+
}
|
|
26
|
+
if (file) {
|
|
27
|
+
return this.resolve(resolve(ctx.root, file), importer, {
|
|
28
|
+
skipSelf: true,
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return VirtualFiles.NOOP_ID
|
|
33
|
+
}
|
|
34
|
+
if (id.startsWith(VirtualFiles.CONFIG_ID)) {
|
|
35
|
+
return VirtualFiles.RESOLVED_CONFIG_ID
|
|
36
|
+
}
|
|
37
|
+
if (id.startsWith(VirtualFiles.THEME_ID)) {
|
|
38
|
+
return VirtualFiles.RESOLVED_THEME_ID
|
|
39
|
+
}
|
|
40
|
+
if (id.startsWith(VirtualFiles.SEARCH_TITLE_DATA_ID)) {
|
|
41
|
+
return VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID
|
|
42
|
+
}
|
|
43
|
+
if (id.startsWith(VirtualFiles.SEARCH_DOCS_DATA_ID)) {
|
|
44
|
+
return VirtualFiles.RESOLVED_SEARCH_DOCS_DATA_ID
|
|
45
|
+
}
|
|
46
|
+
if (id.startsWith(VirtualFiles.GENERATED_GLOBAL_SETUP)) {
|
|
47
|
+
return VirtualFiles.RESOLVED_GENERATED_GLOBAL_SETUP
|
|
48
|
+
}
|
|
49
|
+
if (id.startsWith(VirtualFiles.GENERATED_SETUP_CODE)) {
|
|
50
|
+
const [, index] = id.split(ID_SEPARATOR)
|
|
51
|
+
return `${VirtualFiles.RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`
|
|
52
|
+
}
|
|
53
|
+
if (id.startsWith(VirtualFiles.SUPPORT_PLUGINS_CLIENT)) {
|
|
54
|
+
return VirtualFiles.RESOLVED_SUPPORT_PLUGINS_CLIENT
|
|
55
|
+
}
|
|
56
|
+
if (id.startsWith(VirtualFiles.SUPPORT_PLUGINS_COLLECT)) {
|
|
57
|
+
return VirtualFiles.RESOLVED_SUPPORT_PLUGINS_COLLECT
|
|
58
|
+
}
|
|
59
|
+
if (id.startsWith(VirtualFiles.MARKDOWN_FILES)) {
|
|
60
|
+
return VirtualFiles.RESOLVED_MARKDOWN_FILES
|
|
61
|
+
}
|
|
62
|
+
if (id.startsWith('virtual:story:')) {
|
|
63
|
+
return `\0${id}`
|
|
64
|
+
}
|
|
65
|
+
if (id.startsWith('virtual:story-source:')) {
|
|
66
|
+
return `/__resolved__${id}`
|
|
67
|
+
// @TODO
|
|
68
|
+
// return `\0${id}`
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (id.startsWith('virtual:md:')) {
|
|
72
|
+
return `/__resolved__${id}`
|
|
73
|
+
// @TODO
|
|
74
|
+
// return `\0${id}`
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
async load (id) {
|
|
79
|
+
if (id === VirtualFiles.RESOLVED_STORIES_ID) {
|
|
80
|
+
return VirtualFiles.resolvedStories(ctx)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (id === VirtualFiles.NOOP_ID) {
|
|
84
|
+
return VirtualFiles.noop()
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (id === VirtualFiles.RESOLVED_CONFIG_ID) {
|
|
88
|
+
return VirtualFiles.resolvedConfig(ctx)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (id === VirtualFiles.RESOLVED_THEME_ID) {
|
|
92
|
+
return VirtualFiles.resolvedTheme(ctx)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (id === VirtualFiles.RESOLVED_SEARCH_TITLE_DATA_ID) {
|
|
96
|
+
return getSearchDataJS(await generateTitleSearchData(ctx))
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (id === VirtualFiles.RESOLVED_SEARCH_DOCS_DATA_ID) {
|
|
100
|
+
return getSearchDataJS(await generateDocSearchData(ctx))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (id === VirtualFiles.RESOLVED_GENERATED_GLOBAL_SETUP) {
|
|
104
|
+
return VirtualFiles.resolvedGeneratedGlobalSetup(ctx)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (id.startsWith(VirtualFiles.RESOLVED_GENERATED_SETUP_CODE)) {
|
|
108
|
+
return VirtualFiles.resolvedGeneratedSetupCode(ctx, id)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (id === VirtualFiles.RESOLVED_SUPPORT_PLUGINS_CLIENT) {
|
|
112
|
+
return VirtualFiles.resolvedSupportPluginsClient(ctx)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (id === VirtualFiles.RESOLVED_SUPPORT_PLUGINS_COLLECT) {
|
|
116
|
+
return VirtualFiles.resolvedSupportPluginsCollect(ctx)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (id === VirtualFiles.RESOLVED_MARKDOWN_FILES) {
|
|
120
|
+
return VirtualFiles.resolvedMarkdownFiles(ctx)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (id.startsWith('\0virtual:story:')) {
|
|
124
|
+
return VirtualFiles.story(ctx, id)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (id.startsWith('/__resolved__virtual:story-source:')) {
|
|
128
|
+
return VirtualFiles.storySource(ctx, id)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (id.startsWith('/__resolved__virtual:md:')) {
|
|
132
|
+
return VirtualFiles.markdown(ctx, id)
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
} as VitePlugin)
|
package/src/node/vite.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRequire } from 'node:module'
|
|
2
|
-
import { relative,
|
|
2
|
+
import { relative, join, dirname } from 'pathe'
|
|
3
3
|
import {
|
|
4
4
|
Plugin as VitePlugin,
|
|
5
5
|
UserConfig as ViteConfig,
|
|
@@ -8,59 +8,14 @@ import {
|
|
|
8
8
|
loadConfigFromFile as loadViteConfigFromFile,
|
|
9
9
|
} from 'vite'
|
|
10
10
|
import { lookup as lookupMime } from 'mrmime'
|
|
11
|
-
import fs from 'fs-extra'
|
|
12
11
|
import { APP_PATH, TEMP_PATH } from './alias.js'
|
|
13
12
|
import { Context } from './context.js'
|
|
14
13
|
import { notifyStoryChange } from './stories.js'
|
|
15
|
-
import { makeTree } from './tree.js'
|
|
16
|
-
import { parseColor } from './colors.js'
|
|
17
14
|
import { createMarkdownPlugins } from './markdown.js'
|
|
18
|
-
import {
|
|
19
|
-
import { getInjectedImport } from './util/vendors.js'
|
|
15
|
+
import { createVirtualFilesPlugin } from './virtual/vite-plugin.js'
|
|
20
16
|
|
|
21
17
|
const require = createRequire(import.meta.url)
|
|
22
18
|
|
|
23
|
-
// @TODO wait for virtual modules HMR to work in vite to use `virtual:` and `\0` prefix
|
|
24
|
-
// https://github.com/vitejs/vite/pull/10313
|
|
25
|
-
export const STORIES_ID = 'virtual:$histoire-stories'
|
|
26
|
-
export const RESOLVED_STORIES_ID = `/__resolved__${STORIES_ID}`
|
|
27
|
-
// export const RESOLVED_STORIES_ID = `\0${STORIES_ID}`
|
|
28
|
-
export const SETUP_ID = 'virtual:$histoire-setup'
|
|
29
|
-
export const NOOP_ID = 'virtual:$histoire-noop'
|
|
30
|
-
export const CONFIG_ID = 'virtual:$histoire-config'
|
|
31
|
-
export const RESOLVED_CONFIG_ID = `/__resolved__${CONFIG_ID}`
|
|
32
|
-
// export const RESOLVED_CONFIG_ID = `\0${CONFIG_ID}`
|
|
33
|
-
export const THEME_ID = 'virtual:$histoire-theme'
|
|
34
|
-
export const RESOLVED_THEME_ID = `/__resolved__${THEME_ID}.css`
|
|
35
|
-
// export const RESOLVED_THEME_ID = `\0${THEME_ID}.css`
|
|
36
|
-
export const SEARCH_TITLE_DATA_ID = 'virtual:$histoire-search-title-data'
|
|
37
|
-
export const RESOLVED_SEARCH_TITLE_DATA_ID = `/__resolved__${SEARCH_TITLE_DATA_ID}`
|
|
38
|
-
// export const RESOLVED_SEARCH_TITLE_DATA_ID = `\0${SEARCH_TITLE_DATA_ID}`
|
|
39
|
-
export const SEARCH_DOCS_DATA_ID = 'virtual:$histoire-search-docs-data'
|
|
40
|
-
export const RESOLVED_SEARCH_DOCS_DATA_ID = `/__resolved__${SEARCH_DOCS_DATA_ID}`
|
|
41
|
-
// export const RESOLVED_SEARCH_DOCS_DATA_ID = `\0${SEARCH_DOCS_DATA_ID}`
|
|
42
|
-
export const GENERATED_GLOBAL_SETUP = 'virtual:$histoire-generated-global-setup'
|
|
43
|
-
export const RESOLVED_GENERATED_GLOBAL_SETUP = `/__resolved__${GENERATED_GLOBAL_SETUP}`
|
|
44
|
-
// export const RESOLVED_GENERATED_GLOBAL_SETUP = `\0${GENERATED_GLOBAL_SETUP}`
|
|
45
|
-
export const GENERATED_SETUP_CODE = 'virtual:$histoire-generated-setup-code'
|
|
46
|
-
export const RESOLVED_GENERATED_SETUP_CODE = `/__resolved__${GENERATED_SETUP_CODE}`
|
|
47
|
-
// export const RESOLVED_GENERATED_SETUP_CODE = `\0${GENERATED_SETUP_CODE}`
|
|
48
|
-
export const SUPPORT_PLUGINS_CLIENT = 'virtual:$histoire-support-plugins-client'
|
|
49
|
-
export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `/__resolved__${SUPPORT_PLUGINS_CLIENT}`
|
|
50
|
-
// export const RESOLVED_SUPPORT_PLUGINS_CLIENT = `\0${SUPPORT_PLUGINS_CLIENT}`
|
|
51
|
-
export const SUPPORT_PLUGINS_COLLECT = 'virtual:$histoire-support-plugins-collect'
|
|
52
|
-
export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `/__resolved__${SUPPORT_PLUGINS_COLLECT}`
|
|
53
|
-
// export const RESOLVED_SUPPORT_PLUGINS_COLLECT = `\0${SUPPORT_PLUGINS_COLLECT}`
|
|
54
|
-
export const MARKDOWN_FILES = 'virtual:$histoire-markdown-files'
|
|
55
|
-
export const RESOLVED_MARKDOWN_FILES = `/__resolved__${MARKDOWN_FILES}`
|
|
56
|
-
// export const RESOLVED_MARKDOWN_FILES = `\0${MARKDOWN_FILES}`
|
|
57
|
-
|
|
58
|
-
const ID_SEPARATOR = '__-__'
|
|
59
|
-
|
|
60
|
-
const PLUGINS_HAVE_DEV = [
|
|
61
|
-
'@histoire/plugin-vue',
|
|
62
|
-
]
|
|
63
|
-
|
|
64
19
|
export async function mergeHistoireViteConfig (viteConfig: InlineConfig, ctx: Context) {
|
|
65
20
|
if (ctx.config.vite) {
|
|
66
21
|
const command = ctx.mode === 'dev' ? 'serve' : 'build'
|
|
@@ -134,6 +89,16 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
134
89
|
alias: {
|
|
135
90
|
'histoire-style': join(APP_PATH, process.env.HISTOIRE_DEV ? 'app/style/main.pcss' : 'style.css'),
|
|
136
91
|
},
|
|
92
|
+
|
|
93
|
+
...(isServer
|
|
94
|
+
? {
|
|
95
|
+
// Force resolving deps like Node.JS resolution algorithm (in case some modules are not loaded with ssr: true e.g. .vue files)
|
|
96
|
+
conditions: ['node'],
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
|
|
98
|
+
// @ts-ignore we support Vite ^3.0, but browserField is available in Vite ^3.2
|
|
99
|
+
browserField: false,
|
|
100
|
+
}
|
|
101
|
+
: {}),
|
|
137
102
|
},
|
|
138
103
|
optimizeDeps: {
|
|
139
104
|
entries: [
|
|
@@ -169,264 +134,31 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
|
|
|
169
134
|
watch: {
|
|
170
135
|
ignored: [`!**/node_modules/.histoire/**`, '**/vite.config.*'],
|
|
171
136
|
},
|
|
137
|
+
...isServer
|
|
138
|
+
? {
|
|
139
|
+
hmr: false,
|
|
140
|
+
}
|
|
141
|
+
: {},
|
|
172
142
|
},
|
|
173
143
|
define: {
|
|
174
144
|
// We need to force this to be able to use `devtoolsRawSetupState`
|
|
175
145
|
__VUE_PROD_DEVTOOLS__: 'true',
|
|
176
146
|
// Disable warnings
|
|
177
147
|
'process.env.NODE_ENV': JSON.stringify(isServer ? 'production' : process.env.NODE_ENV ?? 'development'),
|
|
178
|
-
|
|
179
|
-
|
|
148
|
+
...!isServer
|
|
149
|
+
? {
|
|
150
|
+
// Collect flag
|
|
151
|
+
'process.env.HST_COLLECT': 'false',
|
|
152
|
+
}
|
|
153
|
+
: {},
|
|
180
154
|
},
|
|
181
155
|
cacheDir: isServer ? 'node_modules/.hst-vite-server' : 'node_modules/.hst-vite',
|
|
182
156
|
}
|
|
183
157
|
},
|
|
184
158
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
if (id.startsWith(SETUP_ID)) {
|
|
190
|
-
const setupFileConfig = ctx.config.setupFile
|
|
191
|
-
if (setupFileConfig) {
|
|
192
|
-
let file: string
|
|
193
|
-
if (typeof setupFileConfig === 'string') {
|
|
194
|
-
file = setupFileConfig
|
|
195
|
-
} else if (isServer && 'server' in setupFileConfig) {
|
|
196
|
-
file = setupFileConfig.server
|
|
197
|
-
} else if (!isServer && 'browser' in setupFileConfig) {
|
|
198
|
-
file = setupFileConfig.browser
|
|
199
|
-
}
|
|
200
|
-
if (file) {
|
|
201
|
-
return this.resolve(resolve(ctx.root, file), importer, {
|
|
202
|
-
skipSelf: true,
|
|
203
|
-
})
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return NOOP_ID
|
|
207
|
-
}
|
|
208
|
-
if (id.startsWith(CONFIG_ID)) {
|
|
209
|
-
return RESOLVED_CONFIG_ID
|
|
210
|
-
}
|
|
211
|
-
if (id.startsWith(THEME_ID)) {
|
|
212
|
-
return RESOLVED_THEME_ID
|
|
213
|
-
}
|
|
214
|
-
if (id.startsWith(SEARCH_TITLE_DATA_ID)) {
|
|
215
|
-
return RESOLVED_SEARCH_TITLE_DATA_ID
|
|
216
|
-
}
|
|
217
|
-
if (id.startsWith(SEARCH_DOCS_DATA_ID)) {
|
|
218
|
-
return RESOLVED_SEARCH_DOCS_DATA_ID
|
|
219
|
-
}
|
|
220
|
-
if (id.startsWith(GENERATED_GLOBAL_SETUP)) {
|
|
221
|
-
return RESOLVED_GENERATED_GLOBAL_SETUP
|
|
222
|
-
}
|
|
223
|
-
if (id.startsWith(GENERATED_SETUP_CODE)) {
|
|
224
|
-
const [, index] = id.split(ID_SEPARATOR)
|
|
225
|
-
return `${RESOLVED_GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}`
|
|
226
|
-
}
|
|
227
|
-
if (id.startsWith(SUPPORT_PLUGINS_CLIENT)) {
|
|
228
|
-
return RESOLVED_SUPPORT_PLUGINS_CLIENT
|
|
229
|
-
}
|
|
230
|
-
if (id.startsWith(SUPPORT_PLUGINS_COLLECT)) {
|
|
231
|
-
return RESOLVED_SUPPORT_PLUGINS_COLLECT
|
|
232
|
-
}
|
|
233
|
-
if (id.startsWith(MARKDOWN_FILES)) {
|
|
234
|
-
return RESOLVED_MARKDOWN_FILES
|
|
235
|
-
}
|
|
236
|
-
if (id.startsWith('virtual:story:')) {
|
|
237
|
-
return `\0${id}`
|
|
238
|
-
}
|
|
239
|
-
if (id.startsWith('virtual:story-source:')) {
|
|
240
|
-
return `/__resolved__${id}`
|
|
241
|
-
// @TODO
|
|
242
|
-
// return `\0${id}`
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
if (id.startsWith('virtual:md:')) {
|
|
246
|
-
return `/__resolved__${id}`
|
|
247
|
-
// @TODO
|
|
248
|
-
// return `\0${id}`
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
|
|
252
|
-
async load (id) {
|
|
253
|
-
if (id === RESOLVED_STORIES_ID) {
|
|
254
|
-
const resolvedStories = ctx.storyFiles.filter(s => !!s.story)
|
|
255
|
-
const files = resolvedStories.map((file, index) => {
|
|
256
|
-
return {
|
|
257
|
-
id: file.id,
|
|
258
|
-
path: file.treePath,
|
|
259
|
-
filePath: file.relativePath,
|
|
260
|
-
story: {
|
|
261
|
-
...file.story,
|
|
262
|
-
docsText: undefined,
|
|
263
|
-
},
|
|
264
|
-
supportPluginId: file.supportPluginId,
|
|
265
|
-
index,
|
|
266
|
-
}
|
|
267
|
-
})
|
|
268
|
-
return `${ctx.supportPlugins.map(p => p.importStoriesPrepend).filter(Boolean).join('\n')}
|
|
269
|
-
${resolvedStories.map((file, index) => {
|
|
270
|
-
const supportPlugin = ctx.supportPlugins.find(p => p.id === file.supportPluginId)
|
|
271
|
-
if (!supportPlugin) {
|
|
272
|
-
throw new Error(`Could not find support plugin for story ${file.path}: ${file.supportPluginId}`)
|
|
273
|
-
}
|
|
274
|
-
return supportPlugin.importStoryComponent(file, index)
|
|
275
|
-
}).filter(Boolean).join('\n')}
|
|
276
|
-
export let files = [${files.map((file) => `{${JSON.stringify(file).slice(1, -1)}, component: Comp${file.index}, source: () => import('virtual:story-source:${file.story.id}')}`).join(',\n')}]
|
|
277
|
-
export let tree = ${JSON.stringify(makeTree(ctx.config, resolvedStories))}
|
|
278
|
-
const handlers = []
|
|
279
|
-
export function onUpdate (cb) {
|
|
280
|
-
handlers.push(cb)
|
|
281
|
-
}
|
|
282
|
-
if (import.meta.hot) {
|
|
283
|
-
import.meta.hot.accept(newModule => {
|
|
284
|
-
files = newModule.files
|
|
285
|
-
tree = newModule.tree
|
|
286
|
-
handlers.forEach(h => {
|
|
287
|
-
h(newModule.files, newModule.tree)
|
|
288
|
-
newModule.onUpdate(h)
|
|
289
|
-
})
|
|
290
|
-
})
|
|
291
|
-
}`
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
if (id === NOOP_ID) {
|
|
295
|
-
return `export default () => {}`
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
if (id === RESOLVED_CONFIG_ID) {
|
|
299
|
-
let js = `export const config = ${JSON.stringify(ctx.config)}\n`
|
|
300
|
-
if (ctx.config.theme?.logo) {
|
|
301
|
-
for (const key in ctx.config.theme.logo) {
|
|
302
|
-
js += `import Logo_${key} from '${ctx.config.theme.logo[key]}'\n`
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
js += `export const logos = {${Object.keys(ctx.config.theme?.logo ?? {}).map(key => `${key}: Logo_${key}`).join(', ')}}\n`
|
|
306
|
-
return js
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
if (id === RESOLVED_THEME_ID) {
|
|
310
|
-
let css = '*, ::before, ::after {'
|
|
311
|
-
// Colors
|
|
312
|
-
for (const color in ctx.config.theme?.colors ?? {}) {
|
|
313
|
-
for (const key in ctx.config.theme.colors[color]) {
|
|
314
|
-
css += `--_histoire-color-${color}-${key}: ${parseColor(ctx.config.theme.colors[color][key]).color.join(' ')};`
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
css += '}'
|
|
318
|
-
return css
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (id === RESOLVED_SEARCH_TITLE_DATA_ID) {
|
|
322
|
-
return getSearchDataJS(await generateTitleSearchData(ctx))
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
if (id === RESOLVED_SEARCH_DOCS_DATA_ID) {
|
|
326
|
-
return getSearchDataJS(await generateDocSearchData(ctx))
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
if (id === RESOLVED_GENERATED_GLOBAL_SETUP) {
|
|
330
|
-
if (ctx.config.setupCode) {
|
|
331
|
-
return [
|
|
332
|
-
// Import
|
|
333
|
-
`${ctx.config.setupCode.map((c, index) => `import * as setup_${index} from '${GENERATED_SETUP_CODE}${ID_SEPARATOR}${index}'`).join('\n')}`,
|
|
334
|
-
// List
|
|
335
|
-
`const setupList = [${ctx.config.setupCode.map((c, index) => `setup_${index}`).join(', ')}]`,
|
|
336
|
-
// Setups
|
|
337
|
-
...ctx.supportPlugins.map(p => p.setupFn).map(fnName => `export async function ${fnName} (payload) {
|
|
338
|
-
for (const setup of setupList) {
|
|
339
|
-
if (setup?.${fnName}) {
|
|
340
|
-
await setup.${fnName}(payload)
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
}`),
|
|
344
|
-
].join('\n')
|
|
345
|
-
} else {
|
|
346
|
-
return ''
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
if (id.startsWith(RESOLVED_GENERATED_SETUP_CODE)) {
|
|
351
|
-
const [, index] = id.split(ID_SEPARATOR)
|
|
352
|
-
return ctx.config.setupCode?.[index] ?? ''
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if (id === RESOLVED_SUPPORT_PLUGINS_CLIENT) {
|
|
356
|
-
const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/client${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
|
|
357
|
-
paths: [ctx.root, import.meta.url],
|
|
358
|
-
}))})`)
|
|
359
|
-
return `export const clientSupportPlugins = {
|
|
360
|
-
${plugins.join(',\n ')}
|
|
361
|
-
}`
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
if (id === RESOLVED_SUPPORT_PLUGINS_COLLECT) {
|
|
365
|
-
const plugins = ctx.supportPlugins.map(p => `'${p.id}': () => import(${JSON.stringify(require.resolve(`${p.moduleName}/collect${process.env.HISTOIRE_DEV && PLUGINS_HAVE_DEV.includes(p.moduleName) ? '-dev' : ''}`, {
|
|
366
|
-
paths: [ctx.root, import.meta.url],
|
|
367
|
-
}))})`)
|
|
368
|
-
return `export const collectSupportPlugins = {
|
|
369
|
-
${plugins.join(',\n ')}
|
|
370
|
-
}`
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (id === RESOLVED_MARKDOWN_FILES) {
|
|
374
|
-
const filesJs = ctx.markdownFiles.map(f => `${JSON.stringify(f.relativePath)}: () => import(${JSON.stringify(`virtual:md:${f.id}`)})`).join(',')
|
|
375
|
-
return `import { reactive } from ${process.env.HISTOIRE_DEV ? `'vue'` : getInjectedImport('@histoire/vendors/vue')}
|
|
376
|
-
export const markdownFiles = reactive({${filesJs}})
|
|
377
|
-
if (import.meta.hot) {
|
|
378
|
-
if (!window.__hst_md_hmr) {
|
|
379
|
-
window.__hst_md_hmr = (newModule) => {
|
|
380
|
-
markdownFiles[newModule.relativePath] = () => newModule
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
import.meta.hot.accept(newModule => {
|
|
385
|
-
Object.assign(markdownFiles, newModule.markdownFiles)
|
|
386
|
-
})
|
|
387
|
-
}`
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
if (id.startsWith('\0virtual:story:')) {
|
|
391
|
-
const moduleId = id.replace('\0', '')
|
|
392
|
-
const storyFile = ctx.storyFiles.find(f => f.moduleId === moduleId && f.virtual)
|
|
393
|
-
if (storyFile) {
|
|
394
|
-
return storyFile.moduleCode
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (id.startsWith('/__resolved__virtual:story-source:')) {
|
|
399
|
-
const storyId = id.slice('/__resolved__virtual:story-source:'.length)
|
|
400
|
-
const storyFile = ctx.storyFiles.find(f => f.story?.id === storyId)
|
|
401
|
-
if (storyFile) {
|
|
402
|
-
let source: string
|
|
403
|
-
if (storyFile.virtual) {
|
|
404
|
-
source = storyFile.moduleCode
|
|
405
|
-
} else {
|
|
406
|
-
source = await fs.readFile(resolve(ctx.root, storyFile.relativePath), 'utf-8')
|
|
407
|
-
}
|
|
408
|
-
return `export default ${JSON.stringify(source)}`
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (id.startsWith('/__resolved__virtual:md:')) {
|
|
413
|
-
const fileId = id.slice('/__resolved__virtual:md:'.length)
|
|
414
|
-
const file = ctx.markdownFiles.find(f => f.id === fileId)
|
|
415
|
-
if (!file) {
|
|
416
|
-
throw new Error(`Markdown file not found: ${fileId}`)
|
|
417
|
-
}
|
|
418
|
-
const { html, frontmatter, relativePath } = file
|
|
419
|
-
return `export const html = ${JSON.stringify(html)}
|
|
420
|
-
export const frontmatter = ${JSON.stringify(frontmatter)}
|
|
421
|
-
export const relativePath = ${JSON.stringify(relativePath)}
|
|
422
|
-
|
|
423
|
-
if (import.meta.hot) {
|
|
424
|
-
import.meta.hot.accept(newModule => {
|
|
425
|
-
if (newModule) {
|
|
426
|
-
window.__hst_md_hmr(newModule)
|
|
427
|
-
}
|
|
428
|
-
})
|
|
429
|
-
}`
|
|
159
|
+
options () {
|
|
160
|
+
this.meta.histoire = {
|
|
161
|
+
isCollecting: isServer,
|
|
430
162
|
}
|
|
431
163
|
},
|
|
432
164
|
|
|
@@ -517,6 +249,8 @@ if (import.meta.hot) {
|
|
|
517
249
|
},
|
|
518
250
|
})
|
|
519
251
|
|
|
252
|
+
plugins.push(createVirtualFilesPlugin(ctx, isServer))
|
|
253
|
+
|
|
520
254
|
// Markdown
|
|
521
255
|
plugins.push(...await createMarkdownPlugins(ctx))
|
|
522
256
|
|