Skip to content

Commit c60db40

Browse files
committed
[fix] #515: infer SFC names with a vite plugin
1 parent 6659284 commit c60db40

File tree

4 files changed

+109
-48
lines changed

4 files changed

+109
-48
lines changed

packages/ui/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"unplugin-auto-import": "^0.15.1",
8888
"unplugin-icons": "^0.15.3",
8989
"vite": "^4.1.4",
90+
"vite-plugin-inspect": "^0.7.18",
9091
"vite-plugin-windicss": "^1.8.10",
9192
"vue-tsc": "^1.2.0",
9293
"windicss": "^3.5.4"

packages/ui/src/components/all-components.spec.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { COMPONENTS } from './all-components'
33
import { getComponentName } from '../util'
44

55
for (const [nameExpected, component] of Object.entries(COMPONENTS)) {
6-
// FIXME it seems that automatic name inference does not work anymore
7-
// https://vuejs.org/api/sfc-spec.html#automatic-name-inference
8-
// Should we really set it? Reasons:
9-
// - DevTools inspection and dev warnings (should work without setting `.name`, need to check)
10-
// - Global components registration
11-
test.skip(`Component "${nameExpected}" should have the same actual "name"`, () => {
6+
test(`Component "${nameExpected}" should have the same actual "name"`, () => {
127
const nameActual = getComponentName(component)
138

149
expect(nameActual).toEqual(nameExpected)

packages/ui/vite.config.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from 'vitest/config'
22
import windiPlugin from 'vite-plugin-windicss'
33
import vuePlugin from '@vitejs/plugin-vue'
4+
import inspectPlugin from 'vite-plugin-inspect'
45
import type { RootNode, TemplateChildNode } from '@vue/compiler-core'
56
import iconsPlugin from 'unplugin-icons/vite'
67
import svgPlugin from '@soramitsu-ui/vite-plugin-svg'
@@ -82,6 +83,22 @@ function checkAutoImportedTypesPlugin(checkDts = resolve('auto-imports.d.ts')):
8283
}
8384
}
8485

86+
function sfcInferNamePlugin(): Plugin {
87+
return {
88+
name: 'soramitsu-ui:sfc-infer-name',
89+
transform(src, id) {
90+
if (/\.vue$/.test(id)) {
91+
const componentName = path.basename(id, '.vue')
92+
return {
93+
code: src + `\n;_sfc_main.name = "${componentName}";`,
94+
sourcemap: null,
95+
}
96+
}
97+
return undefined
98+
},
99+
}
100+
}
101+
85102
export default defineConfig({
86103
test: {
87104
include: ['src/**/*.spec.ts'],
@@ -113,6 +130,7 @@ export default defineConfig({
113130
},
114131
},
115132
}),
133+
sfcInferNamePlugin(),
116134
iconsPlugin(),
117135
svgPlugin({
118136
svgo: {
@@ -150,6 +168,9 @@ export default defineConfig({
150168
},
151169
}),
152170
checkAutoImportedTypesPlugin(),
171+
172+
// save for future debugging needs
173+
// inspectPlugin({ build: true, outputDir: '.vite-inspect' }),
153174
],
154175
build: {
155176
sourcemap: true,

pnpm-lock.yaml

+86-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)