Skip to content

Commit

Permalink
fix: add fille to built components to infer tag name
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 5, 2022
1 parent 9f99909 commit a1ace24
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/histoire/src/client/app/codegen/vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function getTagName (vnode: VNode) {
return asyncComp.name ?? getNameFromFile(asyncComp.__file)
} else if (vnode.type?.name) {
return vnode.type.name
} else if (vnode.type?.__file) {
return getNameFromFile(vnode.type.__file)
}
return 'Anonymous'
}
Expand Down
1 change: 1 addition & 0 deletions packages/histoire/src/node/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function buildCommand () {
}
const ctx: Context = {
config,
mode: 'build',
}
await build(ctx)
}
1 change: 1 addition & 0 deletions packages/histoire/src/node/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function devCommand (options: DevOptions) {
}
const ctx: Context = {
config,
mode: 'dev',
}
const server = await createServer(ctx)
await server.listen(options.port)
Expand Down
1 change: 1 addition & 0 deletions packages/histoire/src/node/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { HistoireConfig } from './config.js'

export interface Context {
config: HistoireConfig
mode: 'dev' | 'build'
}
27 changes: 26 additions & 1 deletion packages/histoire/src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { relative } from 'pathe'
import { defineConfig, mergeConfig, Plugin } from 'vite'
import { APP_PATH, DIST_CLIENT_PATH } from './alias.js'
import { Context } from './context.js'
Expand Down Expand Up @@ -116,8 +117,32 @@ if (import.meta.hot) {
}
},
}
return [

const plugins = [
vitePlugin,
vuePlugin,
]

if (ctx.mode === 'build') {
// Add file name in build mode to have components names instead of <Anonymous>
const include = [/\.vue$/]
const exclude = [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/]
const fileNamePlugin: Plugin = {
name: 'histoire-file-name-plugin',
enforce: 'post',

transform (code, id) {
if (exclude.some(r => r.test(id))) return
if (include.some(r => r.test(id))) {
const file = relative(ctx.config.sourceDir, id)
const index = code.indexOf('export default')
const result = `${code.substring(0, index)}_sfc_main.__file = '${file}'\n${code.substring(index)}`
return result
}
},
}
plugins.push(fileNamePlugin)
}

return plugins
}

0 comments on commit a1ace24

Please sign in to comment.