Skip to content

Commit 9de3f7a

Browse files
committed
fix(vue2): parent components importing, close #125
1 parent bb9a62a commit 9de3f7a

File tree

8 files changed

+103
-18
lines changed

8 files changed

+103
-18
lines changed

examples/vue-cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@vue/composition-api": "^1.1.3",
1111
"core-js": "^3.16.3",
12+
"veui": "^2.0.6",
1213
"vue": "^2.6.14"
1314
},
1415
"devDependencies": {

examples/vue-cli/src/App.vue

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
<template>
2-
<div class="block">
3-
<ComponentA msg="a" />
4-
<component-b msg="b" />
5-
<ComponentC msg="c" />
6-
<ComponentD />
2+
<div id="app">
3+
<veui-input-group>
4+
<veui-input />
5+
<veui-button>OK</veui-button>
6+
</veui-input-group>
7+
8+
<veui-select>
9+
<veui-option value="1">
10+
Option 1
11+
</veui-option>
12+
<veui-option value="2">
13+
Option 2
14+
</veui-option>
15+
</veui-select>
16+
17+
<veui-date-picker />
718
</div>
819
</template>
920

10-
<style scoped>
11-
.block {
12-
padding: 0px 20px 10px 20px;
13-
margin: 20px 20px;
14-
border: 1px solid #888;
15-
border-radius: 5px;
21+
<script>
22+
export default {
23+
name: 'App',
24+
}
25+
</script>
26+
27+
<style>
28+
#app {
29+
margin-top: 60px;
1630
}
1731
</style>

examples/vue-cli/vue.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Components = require('unplugin-vue-components/webpack')
99
* @type {import('@vue/cli-service').ProjectOptions}
1010
*/
1111
module.exports = {
12+
transpileDependencies: ['veui'],
1213
configureWebpack: {
1314
plugins: [
1415
ScriptSetup({
@@ -23,6 +24,15 @@ module.exports = {
2324
IconsResolver({
2425
componentPrefix: '',
2526
}),
27+
(name) => {
28+
console.log(name)
29+
if (name.startsWith('Veui')) {
30+
return {
31+
importName: name.slice(4),
32+
path: 'veui',
33+
}
34+
}
35+
},
2636
],
2737
transformer: 'vue2',
2838
}),

pnpm-lock.yaml

Lines changed: 50 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/transforms/vue2.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Debug from 'debug'
22
import MagicString from 'magic-string'
33
import { TransformResult } from 'unplugin'
44
import { Transformer } from '../../types'
5+
import { DISABLE_COMMENT } from '../constants'
56
import { Context } from '../context'
67
import { pascalCase, stringifyComponentImport } from '../utils'
78

@@ -20,7 +21,7 @@ export function Vue2Transformer(ctx: Context): Transformer {
2021

2122
const s = new MagicString(code)
2223

23-
for (const match of code.matchAll(/_c\(['"](.+?)["']([,)])/g)) {
24+
for (const match of code.matchAll(/_c\([\s\n\t]*['"](.+?)["']([,)])/g)) {
2425
const [full, matchedName, append] = match
2526

2627
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
@@ -31,7 +32,7 @@ export function Vue2Transformer(ctx: Context): Transformer {
3132
componentPaths.push(name)
3233
const component = ctx.findComponent(name, [sfcPath], matchedName)
3334
if (component) {
34-
const var_name = `__vite_components_${no}`
35+
const var_name = `__unplugin_components_${no}`
3536
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
3637
no += 1
3738
s.overwrite(start, end, `_c(${var_name}${append}`)
@@ -46,7 +47,7 @@ export function Vue2Transformer(ctx: Context): Transformer {
4647
if (!head.length)
4748
return null
4849

49-
s.prepend(`${head.join(';')};`)
50+
s.prepend(`${DISABLE_COMMENT}${head.join(';')};`)
5051

5152
const result: TransformResult = { code: s.toString() }
5253
if (ctx.sourcemap)

src/core/transforms/vue3.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Debug from 'debug'
22
import MagicString from 'magic-string'
33
import { TransformResult } from 'unplugin'
44
import { Transformer } from '../../types'
5+
import { DISABLE_COMMENT } from '../constants'
56
import { Context } from '../context'
67
import { pascalCase, stringifyComponentImport } from '../utils'
78

@@ -30,7 +31,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
3031
componentPaths.push(name)
3132
const component = ctx.findComponent(name, [sfcPath], matchedName)
3233
if (component) {
33-
const var_name = `__vite_components_${no}`
34+
const var_name = `__unplugin_components_${no}`
3435
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
3536
no += 1
3637
s.overwrite(start, end, var_name)
@@ -45,7 +46,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
4546
if (!head.length)
4647
return null
4748

48-
s.prepend(`${head.join(';')};`)
49+
s.prepend(`${DISABLE_COMMENT}${head.join(';')};`)
4950

5051
const result: TransformResult = { code: s.toString() }
5152
if (ctx.sourcemap)

src/core/unplugin.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createUnplugin } from 'unplugin'
22
import { createFilter } from '@rollup/pluginutils'
33
import { Options } from '../types'
44
import { Context } from './context'
5+
import { shouldTransform } from './utils'
56

67
export default createUnplugin<Options>((options = {}) => {
78
const filter = createFilter(
@@ -19,6 +20,8 @@ export default createUnplugin<Options>((options = {}) => {
1920
},
2021

2122
async transform(code, id) {
23+
if (!shouldTransform(code))
24+
return null
2225
try {
2326
const result = await ctx.transform(code, id)
2427
ctx.generateDeclaration()

src/core/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ResolvedConfig } from 'vite'
44
import { slash, toArray } from '@antfu/utils'
55
import { ComponentInfo, ResolvedOptions, ImportInfo } from '../types'
66
import { Context } from './context'
7+
import { DISABLE_COMMENT } from './constants'
78

89
export interface ResolveComponent {
910
filename: string
@@ -148,3 +149,9 @@ export function getPkgVersion(pkgName: string, defaultVersion: string): string {
148149
return defaultVersion
149150
}
150151
}
152+
153+
export function shouldTransform(code: string) {
154+
if (code.includes(DISABLE_COMMENT))
155+
return false
156+
return true
157+
}

0 commit comments

Comments
 (0)