-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.js
123 lines (108 loc) · 3.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const fs = require('fs')
module.exports = (api) => {
api.extendPackage({
dependencies: {
vue: '^3.0.0-alpha.9'
},
devDependencies: {
'@vue/compiler-sfc': '^3.0.0-alpha.9',
// remove the vue-template-compiler
'vue-template-compiler': null
}
},
{
prune: true
})
if (api.hasPlugin('eslint')) {
api.extendPackage({
devDependencies: {
'eslint-plugin-vue': '^7.0.0-alpha.0'
}
})
// `plugin:vue/essential` -> `plugin:vue/vue3-essential`, etc.
const updateConfig = cfg =>
cfg.replace(
/plugin:vue\/(essential|recommended|strongly-recommended)/gi,
'plugin:vue/vue3-$1'
)
// if the config is placed in `package.json`
const eslintConfigInPkg = api.generator.pkg.eslintConfig
if (eslintConfigInPkg && eslintConfigInPkg.extends) {
eslintConfigInPkg.extends = eslintConfigInPkg.extends.map(cfg => updateConfig(cfg))
}
// if the config has been extracted to a standalone file
api.render((files) => {
for (const filename of [
'.eslintrc.js',
'.eslintrc.cjs',
'.eslintrc.yaml',
'.eslintrc.yml',
'.eslinrc.json',
'.eslintrc'
]) {
if (files[filename]) {
files[filename] = updateConfig(files[filename])
}
}
})
}
if (api.hasPlugin('vuex') || api.generator.pkg.dependencies['vuex']) {
api.extendPackage({
dependencies: {
vuex: '^4.0.0-alpha.1'
}
})
api.exitLog('Installed vuex 4.0.')
api.exitLog('Codemod not yet implemented, please follow the documentation at https://github.com/vuejs/vuex/tree/4.0')
// Codemod TODOs:
// * Remove `Vue.use(Vuex)`
// * `new Vue({ store })` -> `app.use(store)`
// * optional: `new Vuex.Store({})` -> `createStore({})`
}
if (api.hasPlugin('router') || api.generator.pkg.dependencies['router']) {
api.extendPackage({
dependencies: {
'vue-router': '^4.0.0-alpha.3'
}
})
api.exitLog('Installed vue-router 4.0.')
api.exitLog('Codemod not yet implemented, please follow the documentation at https://github.com/vuejs/vue-router-next')
// Codemod TODOs:
// * Remove `Vue.use(VueRouter)`
// * `new VueRouter({})` -> `createRouter({})`
// * `mode`:
// * `mode: 'history'` -> `history: createWebHistory()`
// * `mode: 'hash'` -> `history: createWebHashHistory()`
// * `mode: 'abstract'` -> `history: createMemoryHistory()`
// * `new Vue({ router })` -> `app.use(router)`
// * Async component syntax migration as described in RFC0007
// * Create the corresponding imports
// * Remove unused imports (use ESLint for this task)
// Others:
// * Catch all routes (`/*`) must now be defined using a parameter with a custom regex: `/:catchAll(.*)`
// * `keep-alive` is not yet supported
// * Partial support of per-component navigation guards. No `beforeRouteEnter`
}
const globalApiTransform = require('./codemods/global-api')
api.transformScript(api.entryFile, globalApiTransform)
const resolveFile = (file) => {
let filePath = api.resolve(file)
if (fs.existsSync(filePath)) return filePath
filePath = api.resolve(file.replace('.js', '.ts'))
if (fs.existsSync(filePath)) return filePath
filePath = api.resolve(path.join(file.replace('.js', ''), 'index.js'))
if (fs.existsSync(filePath)) return filePath
filePath = api.resolve(path.join(file.replace('.js', ''), 'index.ts'))
if (fs.existsSync(filePath)) return filePath
}
const routerPath = resolveFile('router')
if (routerPath) {
api.transformScript(routerPath, globalApiTransform)
api.transformScript(routerPath, require('./codemods/router'))
}
const storePath = resolveFile('store')
if (storePath) {
api.transformScript(storePath, globalApiTransform)
api.transformScript(storePath, require('./codemods/vuex'))
}
}