Skip to content

Commit da31b58

Browse files
committed
feat: implement tree-shaking api
1 parent c237c50 commit da31b58

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

Diff for: generator/codemods/global-api/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = function(fileInfo, api) {
1111
require('./remove-production-tip')(context)
1212
require('./remove-vue-use')(context)
1313
require('./remove-contextual-h')(context)
14+
require('./next-tick')(context)
15+
require('./observable')(context)
16+
require('./version')(context)
1417

1518
// remove extraneous imports
1619
const removeExtraneousImport = require('../utils/remove-extraneous-import')

Diff for: generator/codemods/global-api/next-tick.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
module.exports = function createAppMount(context) {
7+
const { j, root } = context
8+
9+
// Vue.nextTick(() => {})
10+
const nextTickCalls = root.find(j.CallExpression, n => {
11+
return (
12+
n.callee.type === 'MemberExpression' &&
13+
n.callee.property.name === 'nextTick' &&
14+
n.callee.object.name === 'Vue'
15+
)
16+
})
17+
18+
if (!nextTickCalls.length) {
19+
return
20+
}
21+
22+
const addImport = require('../utils/add-import')
23+
addImport(context, { imported: 'nextTick' }, 'vue')
24+
25+
nextTickCalls.replaceWith(({ node }) => {
26+
const el = node.arguments[0]
27+
28+
return j.callExpression(j.identifier('nextTick'), [el])
29+
})
30+
}

Diff for: generator/codemods/global-api/observable.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
module.exports = function createAppMount(context) {
7+
const { j, root } = context
8+
9+
// Vue.observable(state)
10+
const observableCalls = root.find(j.CallExpression, n => {
11+
return (
12+
n.callee.type === 'MemberExpression' &&
13+
n.callee.property.name === 'observable' &&
14+
n.callee.object.name === 'Vue'
15+
)
16+
})
17+
18+
if (!observableCalls.length) {
19+
return
20+
}
21+
22+
const addImport = require('../utils/add-import')
23+
addImport(context, { imported: 'reactive' }, 'vue')
24+
25+
observableCalls.replaceWith(({ node }) => {
26+
const el = node.arguments[0]
27+
28+
return j.callExpression(j.identifier('reactive'), [el])
29+
})
30+
}

Diff for: generator/codemods/global-api/version.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {Object} context
3+
* @param {import('jscodeshift').JSCodeshift} context.j
4+
* @param {ReturnType<import('jscodeshift').Core>} context.root
5+
*/
6+
module.exports = function createAppMount(context) {
7+
const { j, root } = context
8+
9+
// Vue.version
10+
const versionCalls = root.find(j.MemberExpression, n => {
11+
return (
12+
n.property.name === 'version' &&
13+
n.object.name === 'Vue'
14+
)
15+
})
16+
17+
if (!versionCalls.length) {
18+
return
19+
}
20+
21+
const addImport = require('../utils/add-import')
22+
addImport(context, { imported: 'version' }, 'vue')
23+
24+
versionCalls.replaceWith(({ node }) => {
25+
const property = node.property.name
26+
27+
return j.identifier(property)
28+
})
29+
}

0 commit comments

Comments
 (0)