Skip to content

Commit 8cf396e

Browse files
committed
fix: gen utils with difference name and exportName
1 parent e8e8214 commit 8cf396e

2 files changed

Lines changed: 152 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ 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
15+
const { content, name } = utilsConfig
16+
const { package: packageName, exportName, destructuring } = content
1717

18-
const statement = generateImportStatement({ moduleName: packageName, exportName, alias: subName, destructuring })
18+
const statement = generateImportStatement({ moduleName: packageName, exportName, alias: name, destructuring })
1919
let realExportName = exportName
2020

21-
if (subName) {
22-
realExportName = subName
21+
if (name) {
22+
realExportName = name
2323
}
2424

2525
return {
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 axios as 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 axios as 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)