Skip to content

Commit

Permalink
refactor(cli): improve create command template code (#1788)
Browse files Browse the repository at this point in the history
* refactor(cli): improve create command template code

* style(cli): format code
  • Loading branch information
chouchouji authored Oct 1, 2024
1 parent f4aaa55 commit 97b726e
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 28 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
packages/varlet-cli/lib/**
packages/varlet-cli/template/**

packages/varlet-vite-plugins/lib/**

Expand Down
6 changes: 5 additions & 1 deletion packages/varlet-cli/template/create/[ComponentName].tsx.ejs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { defineComponent } from 'vue'
import { props } from './props'
import { createNamespace } from '../utils/components'

import './<%- camelizeName %>.less'

const { name, n, classes } = createNamespace('<%- kebabCaseName %>')

export default defineComponent({
name: '<%- bigCamelizeNamespace + bigCamelizeName %>',
name,
props,
setup(props, { slots }) {
return () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/varlet-cli/template/create/[ComponentName].vue.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
<script lang="ts">
import { defineComponent } from 'vue'
import { props } from './props'
import { createNamespace } from '../utils/components'
const { name, n, classes } = createNamespace('<%- kebabCaseName %>')
export default defineComponent({
name: '<%- bigCamelizeNamespace + bigCamelizeName %>',
name,
props
})
</script>

<style lang="less">
@import './<%- camelizeName %>.less';
@import './<%- camelizeName %>';
</style>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import <%- bigCamelizeName %> from '..'
import <%- bigCamelizeNamespace + bigCamelizeName %> from '../<%- bigCamelizeName %>'
import { createApp } from 'vue'
import { mount } from '@vue/test-utils'
import { expect, test } from 'vitest'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Locale.add('en-US', Locale.enUS)
add('zh-CN', zhCN)
add('en-US', enUS)

export { add, t, merge, use }
export { add, t, merge, use }
6 changes: 2 additions & 4 deletions packages/varlet-cli/template/generators/base/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@
"types/index.d.ts",
".varlet/**"
],
"extends": [
"@varlet"
]
}
"extends": ["@varlet"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
darkMode: null,
i18n: null,
playground: null,
versions: null
versions: null,
},
menu: [
{
Expand Down Expand Up @@ -44,5 +44,5 @@ export default defineConfig({
i18n: null,
darkMode: null,
},
}
},
})
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<template>
<button class="va-button" :style="{ background: color }" @click="handleClick">
<button :class="n()" :style="{ background: color }" @click="handleClick">
<slot />
</button>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { createNamespace } from '../utils/components'
const { name, n, classes } = createNamespace('button')
export default defineComponent({
name: 'VaButton',
name,
props: {
color: {
type: String,
Expand All @@ -21,8 +24,9 @@ export default defineComponent({
const handleClick = (e: Event) => {
props.onClick?.(e)
}
return {
n,
handleClick,
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Component, Plugin, App } from 'vue'
import { createNamespaceFn } from '@varlet/shared'

export type ComponentWithInstall<T> = T & Plugin

Expand All @@ -14,4 +15,6 @@ export function withInstall<T = Component>(component: Component, target?: T): Co
}

return componentWithInstall as ComponentWithInstall<T>
}
}

export const createNamespace = createNamespaceFn('va')
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { defineComponent, PropType } from 'vue'
import { createNamespace } from '../utils/components'

import './button.less'

const { name, n, classes } = createNamespace('button')

export default defineComponent({
name: 'VaButton',
name,
props: {
color: {
type: String,
Expand All @@ -18,7 +22,7 @@ export default defineComponent({
const { color } = props

return (
<button class="va-button" style={{ background: color }} onClick={handleClick}>
<button class={n()} style={{ background: color }} onClick={handleClick}>
{slots.default?.()}
</button>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ withInstall(Button)

export const _ButtonComponent = Button

export default Button
export default Button
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Component, Plugin, App } from 'vue'
import { createNamespaceFn } from '@varlet/shared'

export type ComponentWithInstall<T> = T & Plugin

Expand All @@ -14,4 +15,6 @@ export function withInstall<T = Component>(component: Component, target?: T): Co
}

return componentWithInstall as ComponentWithInstall<T>
}
}

export const createNamespace = createNamespaceFn('va')
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
header: {
darkMode: null,
playground: null,
versions: null
versions: null,
},
menu: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<template>
<button class="va-button" :style="{ background: color }" @click="handleClick">
<button :class="n()" :style="{ background: color }" @click="handleClick">
{{ t('button') }}
<slot />
</button>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue'
import { createNamespace } from '../utils/components'
// i18n for component's internal
import { t } from '../locale'
const { name, n, classes } = createNamespace('button')
export default defineComponent({
name: 'VaButton',
name,
props: {
color: {
type: String,
Expand All @@ -28,6 +31,7 @@ export default defineComponent({
return {
t,
n,
handleClick,
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function useLocale<T = Message>() {
add,
use,
merge,
t
t,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Component, Plugin, App } from 'vue'
import { createNamespaceFn } from '@varlet/shared'

export type ComponentWithInstall<T> = T & Plugin

Expand All @@ -14,4 +15,6 @@ export function withInstall<T = Component>(component: Component, target?: T): Co
}

return componentWithInstall as ComponentWithInstall<T>
}
}

export const createNamespace = createNamespaceFn('va')
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { defineComponent, PropType } from 'vue'
import { createNamespace } from '../utils/components'

import './button.less'

// i18n for component's internal
import { t } from '../locale'

const { name, n, classes } = createNamespace('button')

export default defineComponent({
name: 'VaButton',
name,
props: {
color: {
type: String,
Expand All @@ -23,7 +27,7 @@ export default defineComponent({
const { color } = props

return (
<button class="va-button" style={{ background: color }} onClick={handleClick}>
<button class={n()} style={{ background: color }} onClick={handleClick}>
{t('button')}
{slots.default?.()}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function useLocale<T = Message>() {
add,
use,
merge,
t
t,
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Component, Plugin, App } from 'vue'
import { createNamespaceFn } from '@varlet/shared'

export type ComponentWithInstall<T> = T & Plugin

Expand All @@ -14,4 +15,6 @@ export function withInstall<T = Component>(component: Component, target?: T): Co
}

return componentWithInstall as ComponentWithInstall<T>
}
}

export const createNamespace = createNamespaceFn('va')

0 comments on commit 97b726e

Please sign in to comment.