Skip to content

Commit 510194d

Browse files
authored
fix: gen utils with difference name and exportName (#1609)
1 parent 4249e13 commit 510194d

File tree

4 files changed

+167
-17
lines changed

4 files changed

+167
-17
lines changed

packages/vue-generator/src/plugins/genUtilsPlugin.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ function genUtilsPlugin(options = {}) {
1212
const { path, fileName } = realOptions
1313

1414
const handleNpmUtils = (utilsConfig) => {
15-
const { content } = utilsConfig
16-
const { package: packageName, exportName, destructuring, subName } = content
17-
18-
const statement = generateImportStatement({ moduleName: packageName, exportName, alias: subName, destructuring })
15+
const { content, name } = utilsConfig
16+
const { package: packageName, exportName, destructuring } = content
17+
18+
// 如果 destructuring 为 false,则使用 name 作为导出名称
19+
const statement = generateImportStatement({
20+
moduleName: packageName,
21+
exportName: destructuring ? exportName : name,
22+
alias: name,
23+
destructuring
24+
})
1925
let realExportName = exportName
2026

21-
if (subName) {
22-
realExportName = subName
27+
if (name) {
28+
realExportName = name
2329
}
2430

2531
return {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import axios from 'axios'
22
import { Button } from '@opentiny/vue'
3-
import { NavMenu } from '@opentiny/vue'
3+
import { NavMenu as Menu } from '@opentiny/vue'
44
import { Modal } from '@opentiny/vue'
55
import { Pager } from '@opentiny/vue'
66
const npm = ''
@@ -10,4 +10,4 @@ const test = function test() {
1010
const util = function util() {
1111
console.log(321)
1212
}
13-
export { axios, Button, NavMenu, Modal, npm, Pager, test, util }
13+
export { axios, Button, Menu, Modal, npm, Pager, test, util }

packages/vue-generator/test/testcases/sfc/case08/expected/iconTest.vue

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ import { IconEdit, IconMore, IconCustom } from '@opentiny/vue-icon'
2222
import * as vue from 'vue'
2323
import { defineProps, defineEmits } from 'vue'
2424
import { I18nInjectionKey } from 'vue-i18n'
25+
2526
const TinyIconEdit = IconEdit()
2627
const TinyIconMore = IconMore()
2728
const TinyIconCustom = IconCustom()
2829
const props = defineProps({})
30+
2931
const emit = defineEmits([])
3032
const { t, lowcodeWrap, stores } = vue.inject(I18nInjectionKey).lowcode()
31-
const wrap = lowcodeWrap(props, {
32-
emit
33-
})
34-
wrap({
35-
stores
36-
})
33+
const wrap = lowcodeWrap(props, { emit })
34+
wrap({ stores })
35+
3736
const state = vue.reactive({})
38-
wrap({
39-
state
40-
})
37+
wrap({ state })
4138
</script>
4239
<style scoped>
4340
.page-base-style {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { describe, expect, test } from 'vitest'
2+
import genUtilsPlugin from '@/plugins/genUtilsPlugin'
3+
4+
describe('genUtilsPlugin', () => {
5+
test('should do nothing when utils is not array', () => {
6+
const plugin = genUtilsPlugin()
7+
expect(plugin.run({})).toBeUndefined()
8+
expect(plugin.run({ utils: null })).toBeUndefined()
9+
})
10+
11+
test('should generate import for npm util (destructuring true)', () => {
12+
const plugin = genUtilsPlugin()
13+
const schema = {
14+
utils: [
15+
{
16+
type: 'npm',
17+
name: 'request',
18+
content: {
19+
package: 'axios',
20+
exportName: 'axios',
21+
destructuring: false
22+
}
23+
}
24+
]
25+
}
26+
27+
const result = plugin.run(schema)
28+
expect(result?.fileType).toBe('js')
29+
expect(result?.fileName).toBe('utils.js')
30+
expect(result?.path).toBe('./src')
31+
expect(result?.fileContent).toContain("import request from 'axios'")
32+
expect(result?.fileContent).toContain('export { request }')
33+
})
34+
35+
test('should generate const for function util', () => {
36+
const plugin = genUtilsPlugin()
37+
const schema = {
38+
utils: [
39+
{
40+
type: 'function',
41+
name: 'sum',
42+
content: {
43+
value: '(a, b) => a + b'
44+
}
45+
}
46+
]
47+
}
48+
49+
const result = plugin.run(schema)
50+
expect(result?.fileContent).toContain('const sum = (a, b) => a + b')
51+
expect(result?.fileContent).toContain('export { sum }')
52+
})
53+
54+
test('should support aliasing npm util via name', () => {
55+
const plugin = genUtilsPlugin()
56+
const schema = {
57+
utils: [
58+
{
59+
type: 'npm',
60+
name: 'dayjs',
61+
content: {
62+
package: 'dayjs',
63+
exportName: 'dayjs',
64+
destructuring: false
65+
}
66+
}
67+
]
68+
}
69+
70+
const result = plugin.run(schema)
71+
expect(result?.fileContent).toContain("import dayjs from 'dayjs'")
72+
expect(result?.fileContent).toContain('export { dayjs }')
73+
})
74+
75+
test('should support mixed utils and custom options', () => {
76+
const plugin = genUtilsPlugin({ path: './custom', fileName: 'my-utils.js' })
77+
const schema = {
78+
utils: [
79+
{
80+
type: 'npm',
81+
name: 'request',
82+
content: {
83+
package: 'axios',
84+
exportName: 'axios',
85+
destructuring: false
86+
}
87+
},
88+
{
89+
type: 'function',
90+
name: 'sum',
91+
content: {
92+
value: '(a, b) => a + b'
93+
}
94+
}
95+
]
96+
}
97+
98+
const result = plugin.run(schema)
99+
expect(result?.fileName).toBe('my-utils.js')
100+
expect(result?.path).toBe('./custom')
101+
expect(result?.fileContent).toContain("import request from 'axios'")
102+
expect(result?.fileContent).toContain('const sum = (a, b) => a + b')
103+
expect(result?.fileContent).toContain('export { request,sum }')
104+
})
105+
106+
test('should generate destructuring import without alias for npm util', () => {
107+
const plugin = genUtilsPlugin()
108+
const schema = {
109+
utils: [
110+
{
111+
type: 'npm',
112+
name: 'TinyButton',
113+
content: {
114+
package: '@opentiny/vue',
115+
exportName: 'TinyButton',
116+
destructuring: true
117+
}
118+
}
119+
]
120+
}
121+
122+
const result = plugin.run(schema)
123+
expect(result?.fileContent).toContain("import { TinyButton } from '@opentiny/vue'")
124+
expect(result?.fileContent).toContain('export { TinyButton }')
125+
})
126+
127+
test('should generate destructuring import with alias for npm util', () => {
128+
const plugin = genUtilsPlugin()
129+
const schema = {
130+
utils: [
131+
{
132+
type: 'npm',
133+
name: 'TinyButton',
134+
content: {
135+
package: '@opentiny/vue',
136+
exportName: 'Button',
137+
destructuring: true
138+
}
139+
}
140+
]
141+
}
142+
143+
const result = plugin.run(schema)
144+
expect(result?.fileContent).toContain("import { Button as TinyButton } from '@opentiny/vue'")
145+
expect(result?.fileContent).toContain('export { TinyButton }')
146+
})
147+
})

0 commit comments

Comments
 (0)