Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat :implement tree-shaking api change #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions transformations/__tests__/tree-shaking.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { defineInlineTest } from 'jscodeshift/src/testUtils'

const nextTick = require('../next-tick')
const observable = require('../observable')
const version = require('../version')
const treeShaking = require('../tree-shaking')

// Vue.nextTick() => nextTick()
defineInlineTest(
nextTick,
{},
`import Vue from 'vue'
Vue.nextTick(() => {
console.log('foo')
})
`,
`import Vue, { nextTick } from 'vue';
nextTick(() => {
console.log('foo')
})
`,
'tree-shaking (Vue.nextTick() to nextTick())'
)

// Vue.observable() => reactive()
defineInlineTest(
observable,
{},
`import Vue from 'vue'
const state = Vue.observable({ count: 0 })`,
`import Vue, { reactive } from 'vue';
const state = reactive({ count: 0 })`,
'tree-shaking (Vue.observable to reactive)'
)

// Vue.version() => version()
defineInlineTest(
version,
{},
`import Vue from 'vue'
var version = Number(Vue.version.split('.')[0])`,
`import Vue, { version } from 'vue';
var version = Number(version.split('.')[0])`,
'tree-shaking (Vue.version to version)'
)

defineInlineTest(
treeShaking,
{},
`import Vue from 'vue'
Vue.nextTick(function() {})
Vue.observable({ count: 0 })
Vue.version`,
`import { nextTick, reactive, version } from 'vue';
nextTick(function() {})
reactive({ count: 0 })
version
`,
'tree-shaking'
)
1 change: 1 addition & 0 deletions transformations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const transformationMap: {
'scoped-slots-to-slots': require('./scoped-slots-to-slots'),
'new-directive-api': require('./new-directive-api'),
'remove-vue-set-and-delete': require('./remove-vue-set-and-delete'),
'tree-shaking': require('./tree-shaking'),

// atomic ones
'remove-contextual-h-from-render': require('./remove-contextual-h-from-render'),
Expand Down
33 changes: 33 additions & 0 deletions transformations/next-tick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'

export const transformAST: ASTTransformation = ({ root, j }) => {
// find the Vue.nextTick(...)
const nextTickCalls = root.find(j.CallExpression, n => {
return (
n.callee.type === 'MemberExpression' &&
n.callee.property.name === 'nextTick' &&
n.callee.object.name === 'Vue'
)
})

if (nextTickCalls.length) {
// add import nextTick
const addImport = require('./add-import')
addImport.transformAST({ root, j }, {
specifier: {
type: 'named',
imported: 'nextTick'
},
source: 'vue'
})

nextTickCalls.replaceWith(({ node }) => {
const el = node.arguments[0]
return j.callExpression(j.identifier('nextTick'), [el])
})
}
}

export default wrap(transformAST)
export const parser = 'babylon'
33 changes: 33 additions & 0 deletions transformations/observable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'

export const transformAST: ASTTransformation = ({ root, j }) => {
// find the Vue.observable(state)
const observableCalls = root.find(j.CallExpression, n => {
return (
n.callee.type === 'MemberExpression' &&
n.callee.property.name === 'observable' &&
n.callee.object.name === 'Vue'
)
})

if (observableCalls.length) {
// add import reactive
const addImport = require('./add-import')
addImport.transformAST({ root, j }, {
specifier: {
type: 'named',
imported: 'reactive'
},
source: 'vue'
})

observableCalls.replaceWith(({ node }) => {
const el = node.arguments[0]
return j.callExpression(j.identifier('reactive'), [el])
})
}
}

export default wrap(transformAST)
export const parser = 'babylon'
18 changes: 18 additions & 0 deletions transformations/tree-shaking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'
import { transformAST as nextTick } from './next-tick'
import { transformAST as observable } from './observable'
import { transformAST as version } from './version'
import { transformAST as removeImport } from './remove-extraneous-import'

export const transformAST: ASTTransformation = (context) => {
nextTick(context)
observable(context)
version(context)

// remove import 'Vue' from 'vue' if not used
removeImport(context, { localBinding: 'Vue' })
}

export default wrap(transformAST)
export const parser = 'babylon'
32 changes: 32 additions & 0 deletions transformations/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import wrap from '../src/wrapAstTransformation'
import type { ASTTransformation } from '../src/wrapAstTransformation'

export const transformAST: ASTTransformation = ({ root, j }) => {
// find Vue.version
const versionCalls = root.find(j.MemberExpression, n => {
return (
n.property.name === 'version' &&
n.object.name === 'Vue'
)
})

if (versionCalls.length) {
const addImport = require('./add-import')
addImport.transformAST({ root, j }, {
specifier: {
type: 'named',
imported: 'version'
},
source: 'vue'
})

versionCalls.replaceWith(({ node }) => {
// @ts-ignore
const property = node.property.name
return j.identifier(property)
})
}
}

export default wrap(transformAST)
export const parser = 'babylon'