Skip to content

Commit 817fd2d

Browse files
committed
feat: use a codemod to transform entryFile, rather than replace it
1 parent 9bba6dc commit 817fd2d

File tree

10 files changed

+4241
-57
lines changed

10 files changed

+4241
-57
lines changed

.prettierrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
semi: false,
3+
singleQuote: true
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import Vue from 'vue';
2+
import App from './App.vue';
3+
4+
Vue.config.productionTip = false;
5+
6+
new Vue({
7+
render: h => h(App),
8+
}).$mount('#app');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createApp } from 'vue';
2+
import App from './App.vue';
3+
4+
createApp().mount(App, '#app');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
jest.autoMockOff()
2+
3+
const { defineTest } = require('jscodeshift/dist/testUtils')
4+
5+
defineTest(__dirname, 'index', null, 'hello')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const removeProductionTip = require('./remove-production-tip')
2+
const transformMount = require('./transformMount')
3+
4+
module.exports = function(fileInfo, api) {
5+
const j = api.jscodeshift
6+
const root = j(fileInfo.source)
7+
8+
// add a `createApp` import
9+
const vueImportExpr = root.find(j.ImportDeclaration, {
10+
source: {
11+
value: 'vue'
12+
}
13+
})
14+
vueImportExpr.forEach(({ node }) => {
15+
node.specifiers.push(j.importSpecifier(j.identifier('createApp')))
16+
})
17+
18+
removeProductionTip(j, root)
19+
transformMount(j, root)
20+
21+
// remove extraneous Vue import
22+
const localVueUsages = root.find(j.Identifier, { name: 'Vue' })
23+
if (localVueUsages.length === 1) {
24+
localVueUsages.closest(j.ImportDefaultSpecifier).remove()
25+
}
26+
27+
return root.toSource({ lineTerminator: '\n' })
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function removeProductionTip(j, root) {
2+
const productionTipAssignment = root.find(
3+
j.AssignmentExpression,
4+
n =>
5+
n.left.type === 'MemberExpression' &&
6+
n.left.property.name === 'productionTip' &&
7+
n.left.object.property.name === 'config' &&
8+
n.left.object.object.name === 'Vue'
9+
)
10+
productionTipAssignment.remove()
11+
12+
return root
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module.exports = function transformMount(j, root) {
2+
// new Vue(...).$mount()
3+
const mountCalls = root.find(j.CallExpression, n => {
4+
return (
5+
n.callee.type === 'MemberExpression' &&
6+
n.callee.property.name === '$mount' &&
7+
n.callee.object.type === 'NewExpression' &&
8+
n.callee.object.callee.name === 'Vue'
9+
)
10+
})
11+
12+
mountCalls.replaceWith(({ node }) => {
13+
let options = node.callee.object.arguments[0]
14+
const el = node.arguments[0]
15+
16+
// if it's a simple option, like `{ render: h => h(App) }`,
17+
// then just use the App variable
18+
if (
19+
options.properties.length === 1 &&
20+
options.properties[0].key.name === 'render' &&
21+
options.properties[0].value.type === 'ArrowFunctionExpression' &&
22+
options.properties[0].value.body.type === 'CallExpression'
23+
) {
24+
options = options.properties[0].value.body.arguments[0]
25+
}
26+
27+
return j.callExpression(
28+
j.memberExpression(
29+
j.callExpression(j.identifier('createApp'), []),
30+
j.identifier('mount')
31+
),
32+
[options, el]
33+
)
34+
})
35+
36+
return root
37+
}

generator/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ module.exports = (api) => {
88
}
99
})
1010

11-
// TODO: transformScript main.js
12-
api.render('./template')
11+
api.transformScript(api.entryFile, require('./codemods/rfc09-global-api'))
1312

1413
// TODO: uninstall vue-template-compiler (need to fix @vue/cli-service first)
1514
}

package.json

+8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@
99
},
1010
"author": "Haoqun Jiang",
1111
"license": "MIT",
12+
"scripts": {
13+
"test": "jest"
14+
},
1215
"dependencies": {
1316
"vue-loader": "^16.0.0-alpha.1"
1417
},
1518
"peerDependencies": {
1619
"@vue/compiler-sfc": "^3.0.0-alpha.1",
1720
"vue": "^3.0.0-alpha.1"
21+
},
22+
"devDependencies": {
23+
"jest": "^24.9.0",
24+
"jscodeshift": "^0.7.0",
25+
"prettier": "^1.19.1"
1826
}
1927
}

0 commit comments

Comments
 (0)