-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.ts
167 lines (149 loc) · 5.33 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import '@nuxt/schema'
import {
addTemplate,
createResolver,
defineNuxtModule,
useNuxt
} from '@nuxt/kit'
import { buildDevStandalone } from '@storybook/core-server'
import { isAbsolute, join } from 'pathe'
import { logger } from '@storybook/node-logger'
import { readPackageJSON } from 'pkg-types'
import { resolve } from 'pathe'
import consola from 'consola'
import type { StorybookConfig } from '@storybook/vue3-vite'
import type { Parameters as StorybookParameters } from '@storybook/types'
export interface ModuleOptions {
enabled?: boolean
buildOptions: Parameters<typeof buildDevStandalone>[0]
config: StorybookConfig
parameters?: StorybookParameters
}
declare module '@nuxt/schema' {
interface NuxtHooks {
'storybook:config': (config: StorybookConfig) => void
'storybook:build:before': (
options: ModuleOptions['buildOptions']
) => void
'storybook:build:after': () => void
}
}
export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'storybook',
configKey: 'storybook'
},
defaults: nuxt => ({
enabled: nuxt.options.dev,
config: {
stories: ['components/*.stories.@(js|jsx|ts|tsx|mdx)'],
framework: '@storybook/vue3-vite',
core: {
renderer: resolve(__dirname, 'renderer')
},
addons: []
},
buildOptions: {
configDir: join(nuxt.options.buildDir, '.storybook'),
quiet: true,
packageJson: undefined as any,
port: nuxt.options.devServer.port + 1,
cache: undefined,
loglevel: 'silent'
},
parameters: {
layout: 'centered',
controls: { expanded: true },
actions: { argTypesRegex: '^on[A-Z].*' }
}
}),
async setup(options, nuxt) {
if (options.enabled) {
const { resolve } = createResolver(import.meta.url)
options.config.stories = makeAbsoluteStoriesPaths(
options.config.stories
)
await nuxt.callHook('storybook:config', options.config)
addTemplate({
filename: '.storybook/main.options.json',
write: true,
getContents: () => JSON.stringify(options.config, null, 2)
})
addTemplate({
filename: '.storybook/main.mjs',
write: true,
src: resolve('runtime/main.mjs')
})
addTemplate({
filename: '.storybook/preview.mjs',
write: true,
getContents: () =>
`export const parameters = ${JSON.stringify(
options.parameters,
null,
2
)}`
})
addTemplate({
filename: '.storybook/preview-head.html',
write: true,
getContents: () =>
`<script>window.global = window</script><script>window.__NUXT__={data:{},state:{},_errors:{},serverRendered:false,config:{public:{},app:{baseURL:"\u002F",buildAssetsDir:"\u002F_nuxt\u002F",cdnURL:""}}}</script>`
})
if (!options.buildOptions?.packageJson) {
const packageJson = await readPackageJSON()
options.buildOptions.packageJson = packageJson
}
if (!options.buildOptions?.packageJson?.version) {
options.buildOptions.packageJson.version = '0.0.0'
}
const newLogger = consola.create({
defaults: {
message: '[nuxt:storybook]',
tag: 'nuxt:storybook'
}
})
logger.error = newLogger.error.bind(newLogger)
logger.warn = newLogger.warn.bind(newLogger)
logger.info = newLogger.info.bind(newLogger)
logger.trace = newLogger.trace.bind(newLogger)
// Extend server to close it when nuxt closes.
const { extendServer = () => void 0 } = options.buildOptions
options.buildOptions.extendServer = server => {
extendServer(server)
nuxt.hook('close', () => {
server.close()
})
}
nuxt.hook('app:templatesGenerated', async () => {
await nuxt.callHook(
'storybook:build:before',
options.buildOptions
)
void buildDevStandalone(options.buildOptions).then(() => {
void nuxt.callHook('storybook:build:after')
})
})
}
}
})
/**
* Make stories paths absolute.
*/
function makeAbsoluteStoriesPaths(stories: StorybookConfig['stories']) {
const nuxt = useNuxt()
const list = nuxt.options._layers.flatMap(layer => {
return stories.map(story => {
if (typeof story !== 'string') {
story.directory = isAbsolute(story.directory)
? story.directory
: join(layer.cwd, story.directory)
return story
} else {
story = isAbsolute(story) ? story : join(layer.cwd, story)
}
return story
})
})
return [...new Set(list)]
}