Skip to content

Commit fd311ba

Browse files
author
Adolph-WSY
authored
fix: only when global.globalApi.length > 0 will use app.component (#59)
1 parent 2994128 commit fd311ba

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

src/packageTransformation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const globalAddConfig: {
2424
'vue-router': '^4.0.8',
2525
'vue-i18n': '^9.1.6'
2626
},
27-
delete: { 'vue-templadte-compiler': '', '@vue/composition-api': '' }
27+
delete: { 'vue-template-compiler': '', '@vue/composition-api': '' }
2828
},
2929
dependencies: {
3030
add: {},

transformations/__tests__/root-prop-to-use.spec.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { defineInlineTest } from 'jscodeshift/src/testUtils'
22
import type { GlobalApi } from '../../src/global'
33
const transform = require('../root-prop-to-use')
44

5+
global.globalApi = []
6+
let api1: GlobalApi = {
7+
name: 'api1',
8+
path: 'src/directive/permission/api1.js'
9+
}
10+
let api2: GlobalApi = { name: 'api2', path: 'src/directive/api2.js' }
11+
global.globalApi.push(api1)
12+
global.globalApi.push(api2)
13+
514
defineInlineTest(
615
transform,
716
{
@@ -31,15 +40,6 @@ defineInlineTest(
3140
`createApp({});`
3241
)
3342

34-
global.globalApi = []
35-
let api1: GlobalApi = {
36-
name: 'api1',
37-
path: 'src/directive/permission/api1.js'
38-
}
39-
let api2: GlobalApi = { name: 'api2', path: 'src/directive/api2.js' }
40-
global.globalApi.push(api1)
41-
global.globalApi.push(api2)
42-
4343
defineInlineTest(
4444
transform,
4545
{ rootPropName: '', isGlobalApi: true },
@@ -49,3 +49,21 @@ import { api2 } from "../src/directive/api2.js";
4949
Vue.createApp().use(api1).use(api2);`,
5050
'Can recognize global api use'
5151
)
52+
53+
defineInlineTest(
54+
transform,
55+
{ rootPropName: '', isGlobalApi: true },
56+
`import Comp1 from "./Comp1.vue"
57+
export default {
58+
install: app => {
59+
app.component("comp1", Comp1);
60+
}
61+
}`,
62+
`import Comp1 from "./Comp1.vue"
63+
export default {
64+
install: app => {
65+
app.component("comp1", Comp1);
66+
}
67+
}`,
68+
'No target Approots, jump out of this transition. '
69+
)

transformations/new-global-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export const transformAST: ASTTransformation = context => {
2626
// TODO:
2727
// should analyze the AST to get the default import of vue-router and vuex,
2828
// rather than hard-coding the names
29-
removeVueUse(context, { removablePlugins: ['VueRouter', 'Vuex','VueCompositionApi','VueI18n'] })
29+
removeVueUse(context, {
30+
removablePlugins: ['VueRouter', 'Vuex', 'VueCompositionApi', 'VueI18n']
31+
})
3032
removeContextualHFromRender(context)
3133

3234
removeExtraneousImport(context, { localBinding: 'Vue' })

transformations/root-prop-to-use.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import wrap from '../src/wrapAstTransformation'
22
import type { ASTTransformation } from '../src/wrapAstTransformation'
33

44
import * as N from 'jscodeshift'
5+
import createDebug from 'debug'
56

7+
const debug = createDebug('vue-codemod:rule')
68
type Params = {
79
rootPropName: string
810
isGlobalApi?: boolean
@@ -38,13 +40,19 @@ export const transformAST: ASTTransformation<Params> = (
3840
}
3941
})
4042

43+
if (appRoots == undefined || appRoots.length == 0) {
44+
debug('No target Approots, jump out of this transition. ')
45+
return
46+
}
47+
4148
// add global api to main.js used by component
42-
if (
43-
isGlobalApi &&
44-
global.globalApi != undefined &&
45-
global.globalApi.length > 0
46-
) {
47-
console.log('add global api in createApp')
49+
if (isGlobalApi) {
50+
debug(filename)
51+
if (global.globalApi == undefined || global.globalApi.length == 0) {
52+
debug('global api is empty')
53+
return
54+
}
55+
debug('add global api in createApp')
4856
const addImport = require('./add-import')
4957
for (let i in global.globalApi) {
5058
let api = global.globalApi[i]

0 commit comments

Comments
 (0)