├── .gitignore ├── .prettierrc.js ├── LICENSE ├── README.md ├── generator ├── codemods │ ├── global-api │ │ ├── __testfixtures__ │ │ │ ├── basic.input.js │ │ │ ├── basic.output.js │ │ │ ├── custom-root-prop.input.js │ │ │ ├── custom-root-prop.output.js │ │ │ ├── el.input.js │ │ │ ├── el.output.js │ │ │ ├── vue-router.input.js │ │ │ ├── vue-router.output.js │ │ │ ├── vue-use.input.js │ │ │ ├── vue-use.output.js │ │ │ ├── vuex.input.js │ │ │ └── vuex.output.js │ │ ├── __tests__ │ │ │ └── global-api-test.js │ │ ├── create-app-mount.js │ │ ├── index.js │ │ ├── remove-contextual-h.js │ │ ├── remove-production-tip.js │ │ ├── remove-trivial-root.js │ │ ├── remove-vue-use.js │ │ └── root-prop-to-use.js │ ├── router │ │ ├── __testfixtures__ │ │ │ ├── create-history.input.js │ │ │ ├── create-history.output.js │ │ │ ├── create-router.input.js │ │ │ └── create-router.output.js │ │ ├── __tests__ │ │ │ └── router-test.js │ │ └── index.js │ ├── utils │ │ ├── add-import.js │ │ └── remove-extraneous-import.js │ └── vuex │ │ ├── __testfixtures__ │ │ ├── import-alias.input.js │ │ ├── import-alias.output.js │ │ ├── store.input.js │ │ ├── store.output.js │ │ ├── vuex-dot-store.input.js │ │ └── vuex-dot-store.output.js │ │ ├── __tests__ │ │ └── vuex-test.js │ │ └── index.js └── index.js ├── index.js ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | semi: false, 3 | singleQuote: true 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Haoqun Jiang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vue-cli-plugin-vue-next 2 | 3 | A Vue CLI plugin for trying out the Vue 3 beta. 4 | 5 | This is for preview purposes only. There might be bugs and undocumented behavior differences from v2, which are expected. 6 | 7 | Also note that if you are using VSCode, Vetur isn't updated to take advantage of Vue 3's typing yet so intellisense in Vue files may not be fully functional (especially in templates). 8 | 9 | ## Usage 10 | 11 | ```sh 12 | # in an existing Vue CLI project 13 | vue add vue-next 14 | ``` 15 | 16 | ## What's implemented? 17 | 18 | - [x] Add Vue 3 beta and `@vue/compiler-sfc` to the project dependencies. 19 | - [x] Configure webpack to compile `.vue` files with the new Vue 3 compiler. 20 | - [x] [Codemods](./generator/codemods/global-api) that automatically migrate some global API changes mentioned in [RFC-0009](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0009-global-api-change.md). 21 | - [x] Install Vuex 4.0 & Vue Router 4.0 in the project, if older versions of them are detected. 22 | - [x] Codemods for the API changes in Vuex and Vue Router. 23 | 24 | ## TODOs 25 | 26 | - [ ] More comprehensive codemods for breaking changes in Vue 3. 27 | - [ ] TypeScript support 28 | - [ ] Test utils support 29 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/basic.input.js: -------------------------------------------------------------------------------- 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'); 9 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/basic.output.js: -------------------------------------------------------------------------------- 1 | import { createApp } from 'vue'; 2 | import App from './App.vue'; 3 | 4 | createApp(App).mount('#app'); 5 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/custom-root-prop.input.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import App from './App.vue'; 3 | 4 | new Vue({ 5 | myOption: 'hello!', 6 | render: h => h(App), 7 | }).$mount('#app'); 8 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/custom-root-prop.output.js: -------------------------------------------------------------------------------- 1 | import { createApp, h } from 'vue'; 2 | import App from './App.vue'; 3 | 4 | createApp({ 5 | myOption: 'hello!', 6 | render: () => h(App), 7 | }).mount('#app'); 8 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/el.input.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vuejs/vue-cli-plugin-vue-next/121c571accf5b486d4e8e360ce7848d8c4d6c67d/generator/codemods/global-api/__testfixtures__/el.input.js -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/el.output.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vuejs/vue-cli-plugin-vue-next/121c571accf5b486d4e8e360ce7848d8c4d6c67d/generator/codemods/global-api/__testfixtures__/el.output.js -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vue-router.input.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import App from './App.vue'; 3 | import router from './router'; 4 | 5 | new Vue({ 6 | router, 7 | render: (h) => h(App), 8 | }).$mount('#app'); 9 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vue-router.output.js: -------------------------------------------------------------------------------- 1 | import { createApp } from 'vue'; 2 | import App from './App.vue'; 3 | import router from './router'; 4 | 5 | createApp(App).use(router).mount('#app'); 6 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vue-use.input.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue' 2 | import VueRouter from 'vue-router' 3 | import Vuex from 'vuex' 4 | 5 | Vue.use(VueRouter) 6 | Vue.use(Vuex) 7 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vue-use.output.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vuejs/vue-cli-plugin-vue-next/121c571accf5b486d4e8e360ce7848d8c4d6c67d/generator/codemods/global-api/__testfixtures__/vue-use.output.js -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vuex.input.js: -------------------------------------------------------------------------------- 1 | import Vue from 'vue'; 2 | import App from './App.vue'; 3 | import store from './store'; 4 | import anotherStore from './another-store'; 5 | 6 | new Vue({ 7 | store, 8 | render: h => h(App), 9 | }).$mount('#app'); 10 | 11 | new Vue({ 12 | store: anotherStore, 13 | render: h => h(App), 14 | }).$mount('#app'); 15 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__testfixtures__/vuex.output.js: -------------------------------------------------------------------------------- 1 | import { createApp } from 'vue'; 2 | import App from './App.vue'; 3 | import store from './store'; 4 | import anotherStore from './another-store'; 5 | 6 | createApp(App).use(store).mount('#app'); 7 | 8 | createApp(App).use(anotherStore).mount('#app'); 9 | -------------------------------------------------------------------------------- /generator/codemods/global-api/__tests__/global-api-test.js: -------------------------------------------------------------------------------- 1 | jest.autoMockOff() 2 | 3 | const { defineTest } = require('jscodeshift/dist/testUtils') 4 | 5 | defineTest(__dirname, 'index', null, 'basic') 6 | defineTest(__dirname, 'index', null, 'custom-root-prop') 7 | // defineTest(__dirname, 'index', null, 'el') 8 | defineTest(__dirname, 'index', null, 'vue-router') 9 | defineTest(__dirname, 'index', null, 'vuex') 10 | defineTest(__dirname, 'index', null, 'vue-use') 11 | -------------------------------------------------------------------------------- /generator/codemods/global-api/create-app-mount.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Object} context 3 | * @param {import('jscodeshift').JSCodeshift} context.j 4 | * @param {ReturnType} context.root 5 | */ 6 | module.exports = function createAppMount(context) { 7 | const { j, root } = context 8 | 9 | // new Vue(...).$mount() 10 | const mountCalls = root.find(j.CallExpression, n => { 11 | return ( 12 | n.callee.type === 'MemberExpression' && 13 | n.callee.property.name === '$mount' && 14 | n.callee.object.type === 'NewExpression' && 15 | n.callee.object.callee.name === 'Vue' 16 | ) 17 | }) 18 | 19 | if (!mountCalls.length) { 20 | return 21 | } 22 | 23 | const addImport = require('../utils/add-import') 24 | addImport(context, { imported: 'createApp' }, 'vue') 25 | 26 | mountCalls.replaceWith(({ node }) => { 27 | let rootProps = node.callee.object.arguments[0] 28 | const el = node.arguments[0] 29 | 30 | return j.callExpression( 31 | j.memberExpression( 32 | j.callExpression(j.identifier('createApp'), [rootProps]), 33 | j.identifier('mount') 34 | ), 35 | [el] 36 | ) 37 | }) 38 | } 39 | -------------------------------------------------------------------------------- /generator/codemods/global-api/index.js: -------------------------------------------------------------------------------- 1 | /** @type {import('jscodeshift').Transform} */ 2 | module.exports = function(fileInfo, api) { 3 | const j = api.jscodeshift 4 | const root = j(fileInfo.source) 5 | const context = { j, root } 6 | 7 | require('./create-app-mount')(context) 8 | require('./root-prop-to-use')(context, 'store') 9 | require('./root-prop-to-use')(context, 'router') 10 | require('./remove-trivial-root')(context) 11 | require('./remove-production-tip')(context) 12 | require('./remove-vue-use')(context) 13 | require('./remove-contextual-h')(context) 14 | 15 | // remove extraneous imports 16 | const removeExtraneousImport = require('../utils/remove-extraneous-import') 17 | removeExtraneousImport(context, 'Vue') 18 | removeExtraneousImport(context, 'Vuex') 19 | removeExtraneousImport(context, 'VueRouter') 20 | 21 | return root.toSource({ lineTerminator: '\n' }) 22 | } 23 | 24 | module.exports.parser = 'babylon' 25 | -------------------------------------------------------------------------------- /generator/codemods/global-api/remove-contextual-h.js: -------------------------------------------------------------------------------- 1 | const addImport = require('../utils/add-import') 2 | 3 | /** 4 | * replace `render: h => h(App)` with `render: () => h(App) 5 | * @param {Object} context 6 | * @param {import('jscodeshift').JSCodeshift} context.j 7 | * @param {ReturnType} context.root 8 | */ 9 | module.exports = function removeContextualH(context) { 10 | const { j, root } = context 11 | 12 | const renderFns = root.find(j.ObjectProperty, { 13 | key: { 14 | name: 'render' 15 | }, 16 | value: { 17 | type: 'ArrowFunctionExpression' 18 | } 19 | }) 20 | if (renderFns.length) { 21 | addImport(context, { imported: 'h' }, 'vue') 22 | renderFns.forEach(({ node }) => { 23 | node.value.params.shift() 24 | }) 25 | } 26 | 27 | // TODO: render methods 28 | } 29 | -------------------------------------------------------------------------------- /generator/codemods/global-api/remove-production-tip.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Object} context 3 | * @param {import('jscodeshift').JSCodeshift} context.j 4 | * @param {ReturnType} context.root 5 | */ 6 | module.exports = function removeProductionTip({ j, root }) { 7 | const productionTipAssignment = root.find( 8 | j.AssignmentExpression, 9 | n => 10 | j.MemberExpression.check(n.left) && 11 | n.left.property.name === 'productionTip' && 12 | n.left.object.property.name === 'config' && 13 | n.left.object.object.name === 'Vue' 14 | ) 15 | productionTipAssignment.remove() 16 | } 17 | -------------------------------------------------------------------------------- /generator/codemods/global-api/remove-trivial-root.js: -------------------------------------------------------------------------------- 1 | /** 2 | * It is expected to be run after the `createApp` transformataion 3 | * if a root component is trivial, that is, it contains only one simple prop, 4 | * like `{ render: h => h(App) }`, then just use the `App` variable 5 | * 6 | * TODO: implement `remove-trivial-render`, 7 | * move all other rootProps to the second argument of `createApp` 8 | * @param {Object} context 9 | * @param {import('jscodeshift').JSCodeshift} context.j 10 | * @param {ReturnType} context.root 11 | */ 12 | module.exports = function removeTrivialRoot({ j, root }) { 13 | const appRoots = root.find(j.CallExpression, { 14 | callee: { name: 'createApp' }, 15 | arguments: args => args.length === 1 && args[0].type === 'ObjectExpression' 16 | }) 17 | appRoots.forEach(({ node: createAppCall }) => { 18 | const { properties } = createAppCall.arguments[0] 19 | if ( 20 | properties.length === 1 && 21 | properties[0].key.name === 'render' && 22 | j.ArrowFunctionExpression.check(properties[0].value) 23 | ) { 24 | const renderFnBody = properties[0].value.body 25 | const isTrivial = 26 | j.CallExpression.check(renderFnBody) && 27 | renderFnBody.arguments.length === 1 28 | if (isTrivial) { 29 | createAppCall.arguments[0] = renderFnBody.arguments[0] 30 | } 31 | } 32 | }) 33 | } 34 | -------------------------------------------------------------------------------- /generator/codemods/global-api/remove-vue-use.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Remove `Vue.use()` calls 3 | * Per current design, `Vue.use` is replaced by `app.use`. 4 | * But in library implementations like `vue-router` and `vuex`, 5 | * the new `app.use` does not reuse the same argument passed to `Vue.use()`, 6 | * but expects instantiated instances that are used to pass to the root components instead. 7 | * So we now expect the migration to be done in the `root-prop-to-use` transformation, 8 | * and the `Vue.use` statements can be just abandoned. 9 | * @param {Object} context 10 | * @param {import('jscodeshift').JSCodeshift} context.j 11 | * @param {ReturnType} context.root 12 | */ 13 | module.exports = function removeVueUse({ j, root }) { 14 | const vueUseCalls = root.find(j.CallExpression, { 15 | callee: { 16 | type: 'MemberExpression', 17 | object: { 18 | name: 'Vue' 19 | }, 20 | property: { 21 | name: 'use' 22 | } 23 | } 24 | }) 25 | 26 | const removablePlugins = ['VueRouter', 'Vuex'] 27 | const removableUseCalls = vueUseCalls.filter(({ node }) => { 28 | if (j.Identifier.check(node.arguments[0])) { 29 | const plugin = node.arguments[0].name 30 | if (removablePlugins.includes(plugin)) { 31 | return true 32 | } 33 | } 34 | 35 | return false 36 | }) 37 | 38 | removableUseCalls.remove() 39 | } 40 | -------------------------------------------------------------------------------- /generator/codemods/global-api/root-prop-to-use.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Expected to be run after the `createApp` transformation. 3 | * Transforms expressions like `createApp({ router })` to `createApp().use(router)` 4 | * @param {Object} context 5 | * @param {import('jscodeshift').JSCodeshift} context.j 6 | * @param {ReturnType} context.root 7 | */ 8 | module.exports = function rootPropToUse(context, rootPropName) { 9 | const { j, root } = context 10 | 11 | const appRoots = root.find(j.CallExpression, { 12 | callee: { name: 'createApp' }, 13 | arguments: args => 14 | args.length === 1 && 15 | args[0].type === 'ObjectExpression' && 16 | args[0].properties.find(p => p.key.name === rootPropName) 17 | }) 18 | 19 | appRoots.replaceWith(({ node: createAppCall }) => { 20 | const rootProps = createAppCall.arguments[0] 21 | const propertyIndex = rootProps.properties.findIndex( 22 | p => p.key.name === rootPropName 23 | ) 24 | const [{ value: pluginInstance }] = rootProps.properties.splice( 25 | propertyIndex, 26 | 1 27 | ) 28 | 29 | return j.callExpression( 30 | j.memberExpression( 31 | j.callExpression(j.identifier('createApp'), [rootProps]), 32 | j.identifier('use') 33 | ), 34 | [pluginInstance] 35 | ) 36 | }) 37 | } 38 | -------------------------------------------------------------------------------- /generator/codemods/router/__testfixtures__/create-history.input.js: -------------------------------------------------------------------------------- 1 | import VueRouter from 'vue-router'; 2 | import Home from '../views/Home.vue'; 3 | 4 | const routes = [ 5 | { 6 | path: '/', 7 | name: 'home', 8 | component: Home, 9 | }, 10 | { 11 | path: '/about', 12 | name: 'about', 13 | component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') 14 | }, 15 | ]; 16 | 17 | const router = new VueRouter({ 18 | mode: 'history', 19 | base: process.env.BASE_URL, 20 | routes 21 | }); 22 | 23 | new VueRouter({ 24 | mode: 'history', 25 | routes 26 | }); 27 | 28 | export default router; 29 | -------------------------------------------------------------------------------- /generator/codemods/router/__testfixtures__/create-history.output.js: -------------------------------------------------------------------------------- 1 | import { createRouter, createWebHistory } from 'vue-router'; 2 | import Home from '../views/Home.vue'; 3 | 4 | const routes = [ 5 | { 6 | path: '/', 7 | name: 'home', 8 | component: Home, 9 | }, 10 | { 11 | path: '/about', 12 | name: 'about', 13 | component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') 14 | }, 15 | ]; 16 | 17 | const router = createRouter({ 18 | history: createWebHistory(process.env.BASE_URL), 19 | routes 20 | }); 21 | 22 | createRouter({ 23 | history: createWebHistory(), 24 | routes 25 | }); 26 | 27 | export default router; 28 | -------------------------------------------------------------------------------- /generator/codemods/router/__testfixtures__/create-router.input.js: -------------------------------------------------------------------------------- 1 | import VueRouter from 'vue-router'; 2 | import Home from '../views/Home.vue'; 3 | 4 | const routes = [ 5 | { 6 | path: '/', 7 | name: 'home', 8 | component: Home, 9 | }, 10 | { 11 | path: '/about', 12 | name: 'about', 13 | component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') 14 | }, 15 | ]; 16 | 17 | const router = new VueRouter({ 18 | base: process.env.BASE_URL, 19 | routes 20 | }); 21 | 22 | new VueRouter({ 23 | routes 24 | }); 25 | 26 | export default router; 27 | -------------------------------------------------------------------------------- /generator/codemods/router/__testfixtures__/create-router.output.js: -------------------------------------------------------------------------------- 1 | import { createRouter, createWebHashHistory } from 'vue-router'; 2 | import Home from '../views/Home.vue'; 3 | 4 | const routes = [ 5 | { 6 | path: '/', 7 | name: 'home', 8 | component: Home, 9 | }, 10 | { 11 | path: '/about', 12 | name: 'about', 13 | component: () => import(/* webpackChunkName: "about" */ '../views/About.vue') 14 | }, 15 | ]; 16 | 17 | const router = createRouter({ 18 | history: createWebHashHistory(process.env.BASE_URL), 19 | routes 20 | }); 21 | 22 | createRouter({ 23 | history: createWebHashHistory(), 24 | routes 25 | }); 26 | 27 | export default router; 28 | -------------------------------------------------------------------------------- /generator/codemods/router/__tests__/router-test.js: -------------------------------------------------------------------------------- 1 | jest.autoMockOff() 2 | 3 | const { defineTest } = require('jscodeshift/dist/testUtils') 4 | 5 | defineTest(__dirname, 'index', null, 'create-router') 6 | defineTest(__dirname, 'index', null, 'create-history') 7 | -------------------------------------------------------------------------------- /generator/codemods/router/index.js: -------------------------------------------------------------------------------- 1 | const addImport = require('../utils/add-import') 2 | const removeExtraneousImport = require('../utils/remove-extraneous-import') 3 | 4 | /** @type {import('jscodeshift').Transform} */ 5 | module.exports = function(fileInfo, api) { 6 | const j = api.jscodeshift 7 | const root = j(fileInfo.source) 8 | const context = { j, root } 9 | 10 | // new Router() -> createRouter() 11 | const routerImportDecls = root.find(j.ImportDeclaration, { 12 | source: { 13 | value: 'vue-router' 14 | } 15 | }) 16 | 17 | const importedVueRouter = routerImportDecls.find(j.ImportDefaultSpecifier) 18 | if (importedVueRouter.length) { 19 | const localVueRouter = importedVueRouter.get(0).node.local.name 20 | 21 | const newVueRouter = root.find(j.NewExpression, { 22 | callee: { 23 | type: 'Identifier', 24 | name: localVueRouter 25 | } 26 | }) 27 | 28 | addImport(context, { imported: 'createRouter' }, 'vue-router') 29 | newVueRouter.replaceWith(({ node }) => { 30 | // mode: 'history' -> history: createWebHistory(), etc 31 | let historyMode = 'createWebHashHistory' 32 | let baseValue 33 | node.arguments[0].properties = node.arguments[0].properties.map(p => { 34 | if (p.key.name === 'mode') { 35 | const mode = p.value.value 36 | if (mode === 'hash') { 37 | historyMode = 'createWebHashHistory' 38 | } else if (mode === 'history') { 39 | historyMode = 'createWebHistory' 40 | } else if (mode === 'abstract') { 41 | historyMode = 'createMemoryHistory' 42 | } else { 43 | console.error( 44 | `mode must be one of 'hash', 'history', or 'abstract'` 45 | ) 46 | return p 47 | } 48 | return 49 | } else if (p.key.name === 'base') { 50 | baseValue = p.value 51 | return 52 | } 53 | 54 | return p 55 | }) 56 | 57 | // add the default mode with a hash history 58 | addImport(context, { imported: historyMode }, 'vue-router') 59 | node.arguments[0].properties = node.arguments[0].properties.filter( 60 | p => !!p 61 | ) 62 | node.arguments[0].properties.unshift( 63 | j.objectProperty( 64 | j.identifier('history'), 65 | j.callExpression( 66 | j.identifier(historyMode), 67 | baseValue ? [baseValue] : [] 68 | ) 69 | ) 70 | ) 71 | 72 | return j.callExpression(j.identifier('createRouter'), node.arguments) 73 | }) 74 | removeExtraneousImport(context, localVueRouter) 75 | } 76 | 77 | return root.toSource({ lineTerminator: '\n' }) 78 | } 79 | 80 | module.exports.parser = 'babylon' 81 | -------------------------------------------------------------------------------- /generator/codemods/utils/add-import.js: -------------------------------------------------------------------------------- 1 | // specifier should be in the form of `Vue` or `{ imported: 'h' }` or `{ imported: 'h', local: 'createElement' }` 2 | module.exports = function addImport(context, specifier, source) { 3 | const { j, root } = context 4 | 5 | const isDefaultImport = typeof specifier === 'string' 6 | const localName = isDefaultImport 7 | ? specifier 8 | : specifier.local || specifier.imported 9 | 10 | const duplicate = root.find(j.ImportDeclaration, { 11 | specifiers: arr => arr.some(s => s.local.name === localName), 12 | source: { 13 | value: source 14 | } 15 | }) 16 | if (duplicate.length) { 17 | return 18 | } 19 | 20 | let newImportSpecifier 21 | if (isDefaultImport) { 22 | newImportSpecifier = j.importDefaultSpecifier(j.identifier(specifier)) 23 | } else { 24 | newImportSpecifier = j.importSpecifier( 25 | j.identifier(specifier.imported), 26 | j.identifier(specifier.local || specifier.imported) 27 | ) 28 | } 29 | 30 | const matchedDecl = root.find(j.ImportDeclaration, { 31 | source: { 32 | value: source 33 | } 34 | }) 35 | if (matchedDecl.length) { 36 | // add new specifier to the existing import declaration 37 | matchedDecl.get(0).node.specifiers.push(newImportSpecifier) 38 | } else { 39 | const newImportDecl = j.importDeclaration( 40 | [newImportSpecifier], 41 | j.stringLiteral(source) 42 | ) 43 | 44 | const lastImportDecl = root.find(j.ImportDeclaration).at(-1) 45 | if (lastImportDecl.length) { 46 | // add the new import declaration after all other import declarations 47 | lastImportDecl.insertAfter(newImportDecl) 48 | } else { 49 | // add new import declaration at the beginning of the file 50 | root.get().node.program.body.unshift(newImportDecl) 51 | } 52 | } 53 | 54 | return root 55 | } 56 | -------------------------------------------------------------------------------- /generator/codemods/utils/remove-extraneous-import.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Note: 3 | * here we don't completely remove the import declaration statement 4 | * if all import specifiers are removed. 5 | * For example, `import foo from 'bar'`, 6 | * if `foo` is unused, the statement would become `import 'bar'`. 7 | * It is because we are not sure if the module contains any side effects. 8 | * @param {Object} context 9 | * @param {import('jscodeshift').JSCodeshift} context.j 10 | * @param {ReturnType} context.root 11 | */ 12 | module.exports = function removeExtraneousImport({ root, j }, name) { 13 | const usages = root.find(j.Identifier, { name }).filter(identifierPath => { 14 | const parent = identifierPath.parent.node 15 | 16 | // Ignore the import specifier 17 | if ( 18 | j.ImportDefaultSpecifier.check(parent) || 19 | j.ImportSpecifier.check(parent) 20 | ) { 21 | return false 22 | } 23 | 24 | // Ignore properties in MemberExpressions 25 | if ( 26 | j.MemberExpression.check(parent) && 27 | parent.property === identifierPath.node 28 | ) { 29 | return false 30 | } 31 | 32 | // Ignore keys in ObjectProperties 33 | if ( 34 | j.ObjectProperty.check(parent) && 35 | parent.key === identifierPath.node && 36 | parent.value !== identifierPath.node 37 | ) { 38 | return false 39 | } 40 | 41 | return true 42 | }) 43 | 44 | if (!usages.length) { 45 | let specifier = root.find(j.ImportSpecifier, { 46 | local: { 47 | name 48 | } 49 | }) 50 | if (!specifier.length) { 51 | specifier = root.find(j.ImportDefaultSpecifier, { 52 | local: { 53 | name 54 | } 55 | }) 56 | } 57 | 58 | if (!specifier.length) { 59 | return 60 | } 61 | 62 | const decl = specifier.closest(j.ImportDeclaration) 63 | const declNode = decl.get(0).node 64 | const peerSpecifiers = declNode.specifiers 65 | const source = declNode.source.value 66 | 67 | // these modules are known to have no side effects 68 | const safelyRemovableModules = ['vue', 'vue-router', 'vuex'] 69 | if ( 70 | peerSpecifiers.length === 1 && 71 | safelyRemovableModules.includes(source) 72 | ) { 73 | decl.remove() 74 | } else { 75 | // otherwise, only remove the specifier 76 | specifier.remove() 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/import-alias.input.js: -------------------------------------------------------------------------------- 1 | import { Store as TheStore } from 'vuex'; 2 | 3 | const store = new TheStore({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/import-alias.output.js: -------------------------------------------------------------------------------- 1 | import { createStore } from 'vuex'; 2 | 3 | const store = createStore({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/store.input.js: -------------------------------------------------------------------------------- 1 | import { Store } from 'vuex'; 2 | 3 | const store = new Store({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/store.output.js: -------------------------------------------------------------------------------- 1 | import { createStore } from 'vuex'; 2 | 3 | const store = createStore({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/vuex-dot-store.input.js: -------------------------------------------------------------------------------- 1 | import Vuex from 'vuex'; 2 | 3 | const store = new Vuex.Store({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__testfixtures__/vuex-dot-store.output.js: -------------------------------------------------------------------------------- 1 | import Vuex from 'vuex'; 2 | 3 | const store = Vuex.createStore({ 4 | state () { 5 | return { 6 | count: 1 7 | } 8 | } 9 | }); 10 | 11 | export default store; 12 | -------------------------------------------------------------------------------- /generator/codemods/vuex/__tests__/vuex-test.js: -------------------------------------------------------------------------------- 1 | jest.autoMockOff() 2 | 3 | const { defineTest } = require('jscodeshift/dist/testUtils') 4 | 5 | defineTest(__dirname, 'index', null, 'store') 6 | defineTest(__dirname, 'index', null, 'vuex-dot-store') 7 | defineTest(__dirname, 'index', null, 'import-alias') 8 | -------------------------------------------------------------------------------- /generator/codemods/vuex/index.js: -------------------------------------------------------------------------------- 1 | const addImport = require('../utils/add-import') 2 | const removeExtraneousImport = require('../utils/remove-extraneous-import') 3 | 4 | // new Store() -> createStore() 5 | /** @type {import('jscodeshift').Transform} */ 6 | module.exports = function(fileInfo, api) { 7 | const j = api.jscodeshift 8 | const root = j(fileInfo.source) 9 | const context = { j, root } 10 | 11 | const vuexImportDecls = root.find(j.ImportDeclaration, { 12 | source: { 13 | value: 'vuex' 14 | } 15 | }) 16 | 17 | const importedVuex = vuexImportDecls.find(j.ImportDefaultSpecifier) 18 | const importedStore = vuexImportDecls.find(j.ImportSpecifier, { 19 | imported: { 20 | name: 'Store' 21 | } 22 | }) 23 | 24 | if (importedVuex.length) { 25 | const localVuex = importedVuex.get(0).node.local.name 26 | const newVuexDotStore = root.find(j.NewExpression, { 27 | callee: { 28 | type: 'MemberExpression', 29 | object: { 30 | type: 'Identifier', 31 | name: localVuex 32 | }, 33 | property: { 34 | name: 'Store' 35 | } 36 | } 37 | }) 38 | 39 | newVuexDotStore.replaceWith(({ node }) => { 40 | return j.callExpression( 41 | j.memberExpression( 42 | j.identifier(localVuex), 43 | j.identifier('createStore') 44 | ), 45 | node.arguments 46 | ) 47 | }) 48 | } 49 | 50 | if (importedStore.length) { 51 | const localStore = importedStore.get(0).node.local.name 52 | const newStore = root.find(j.NewExpression, { 53 | callee: { 54 | type: 'Identifier', 55 | name: localStore 56 | } 57 | }) 58 | 59 | addImport(context, { imported: 'createStore' }, 'vuex') 60 | newStore.replaceWith(({ node }) => { 61 | return j.callExpression(j.identifier('createStore'), node.arguments) 62 | }) 63 | removeExtraneousImport(context, localStore) 64 | } 65 | 66 | return root.toSource({ lineTerminator: '\n' }) 67 | } 68 | 69 | module.exports.parser = 'babylon' 70 | -------------------------------------------------------------------------------- /generator/index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs') 2 | const path = require('path') 3 | 4 | module.exports = (api) => { 5 | // need the CLI to support the `prune` option of `extendPackage` 6 | api.assertCliVersion('>= 4.2.3') 7 | 8 | api.extendPackage({ 9 | dependencies: { 10 | vue: '^3.0.0-beta.1' 11 | }, 12 | devDependencies: { 13 | '@vue/compiler-sfc': '^3.0.0-beta.1', 14 | // remove the vue-template-compiler 15 | 'vue-template-compiler': null 16 | } 17 | }, 18 | { 19 | prune: true 20 | }) 21 | 22 | const globalAPITransform = require('./codemods/global-api') 23 | api.transformScript(api.entryFile, globalAPITransform) 24 | 25 | if (api.hasPlugin('eslint')) { 26 | api.extendPackage({ 27 | devDependencies: { 28 | 'eslint-plugin-vue': '^7.0.0-alpha.0' 29 | } 30 | }) 31 | 32 | // `plugin:vue/essential` -> `plugin:vue/vue3-essential`, etc. 33 | const updateConfig = cfg => 34 | cfg.replace( 35 | /plugin:vue\/(essential|recommended|strongly-recommended)/gi, 36 | 'plugin:vue/vue3-$1' 37 | ) 38 | 39 | // if the config is placed in `package.json` 40 | const eslintConfigInPkg = api.generator.pkg.eslintConfig 41 | if (eslintConfigInPkg && eslintConfigInPkg.extends) { 42 | eslintConfigInPkg.extends = eslintConfigInPkg.extends.map(cfg => updateConfig(cfg)) 43 | } 44 | // if the config has been extracted to a standalone file 45 | api.render((files) => { 46 | for (const filename of [ 47 | '.eslintrc.js', 48 | '.eslintrc.cjs', 49 | '.eslintrc.yaml', 50 | '.eslintrc.yml', 51 | '.eslinrc.json', 52 | '.eslintrc' 53 | ]) { 54 | if (files[filename]) { 55 | files[filename] = updateConfig(files[filename]) 56 | } 57 | } 58 | }) 59 | } 60 | 61 | const resolveFile = (file) => { 62 | if (!/\.(j|t)s$/ig.test(file)) { 63 | file += '.js' 64 | } 65 | let resolvedPath = api.resolve(file) 66 | const possiblePaths = [ 67 | resolvedPath, 68 | resolvedPath.replace(/\.js$/ig, '.ts'), 69 | path.join(resolvedPath.replace(/\.js$/ig, ''), 'index.js'), 70 | path.join(resolvedPath.replace(/\.js$/ig, ''), 'index.ts') 71 | ] 72 | for (const p of possiblePaths) { 73 | if (fs.existsSync(p)) { 74 | return path.relative(api.resolve('.'), p).replace(/\\/g, '/') 75 | } 76 | } 77 | } 78 | 79 | if (api.hasPlugin('vuex') || api.generator.pkg.dependencies['vuex']) { 80 | api.extendPackage({ 81 | dependencies: { 82 | vuex: '^4.0.0-alpha.1' 83 | } 84 | }) 85 | 86 | api.exitLog('Installed vuex 4.0.') 87 | api.exitLog('Documentation available at https://github.com/vuejs/vuex/tree/4.0') 88 | 89 | const storePath = resolveFile('src/store') 90 | if (storePath) { 91 | api.transformScript(storePath, globalAPITransform) 92 | api.transformScript(storePath, require('./codemods/vuex')) 93 | } 94 | } 95 | 96 | if (api.hasPlugin('router') || api.generator.pkg.dependencies['router']) { 97 | api.extendPackage({ 98 | dependencies: { 99 | 'vue-router': '^4.0.0-alpha.6' 100 | } 101 | }) 102 | 103 | api.exitLog('Installed vue-router 4.0.') 104 | api.exitLog('Documentation available at https://github.com/vuejs/vue-router-next') 105 | 106 | const routerPath = resolveFile('src/router') 107 | if (routerPath) { 108 | api.transformScript(routerPath, globalAPITransform) 109 | api.transformScript(routerPath, require('./codemods/router')) 110 | } 111 | 112 | // Notes: 113 | // * Catch all routes (`/*`) must now be defined using a parameter with a custom regex: `/:catchAll(.*)` 114 | // * `keep-alive` is not yet supported 115 | // * Partial support of per-component navigation guards. No `beforeRouteEnter` 116 | } 117 | 118 | if (api.hasPlugin('unit-jest') || api.hasPlugin('unit-mocha')) { 119 | api.extendPackage({ 120 | devDependencies: { 121 | '@vue/test-utils': '^2.0.0-alpha.1' 122 | } 123 | }) 124 | 125 | api.exitLog('Installed @vue/test-utils 2.0.') 126 | api.exitLog('Documentation available at https://github.com/vuejs/vue-test-utils-next') 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk') 2 | 3 | module.exports = (api, options) => { 4 | try { 5 | api.assertVersion('< 4.5.0') 6 | } catch (e) { 7 | console.warn(chalk.red(`Vue CLI now supports Vue 3 by default.`)) 8 | console.warn(chalk.red(`vue-cli-plugin-vue-next is no longer needed, please remove it from the dependencies.`)) 9 | return 10 | } 11 | 12 | const vueLoaderCacheConfig = api.genCacheConfig('vue-loader', { 13 | 'vue-loader': require('vue-loader/package.json').version, 14 | '@vue/compiler-sfc': require('@vue/compiler-sfc/package.json').version 15 | }) 16 | 17 | api.chainWebpack(config => { 18 | config.resolve.alias 19 | .set( 20 | 'vue$', 21 | options.runtimeCompiler 22 | ? 'vue/dist/vue.esm-bundler.js' 23 | : 'vue/dist/vue.runtime.esm-bundler.js' 24 | ) 25 | 26 | config.module 27 | .rule('vue') 28 | .test(/\.vue$/) 29 | .use('vue-loader') 30 | .loader(require.resolve('vue-loader')) 31 | .options(vueLoaderCacheConfig) 32 | 33 | config 34 | .plugin('vue-loader') 35 | .use(require('vue-loader').VueLoaderPlugin) 36 | }) 37 | } 38 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vue-cli-plugin-vue-next", 3 | "version": "0.1.4", 4 | "description": "Vue CLI plugin for trying out vue-next (experimental)", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/vuejs/vue-cli-plugin-vue-next.git" 9 | }, 10 | "author": "Haoqun Jiang", 11 | "license": "MIT", 12 | "scripts": { 13 | "test": "jest" 14 | }, 15 | "dependencies": { 16 | "chalk": "^4.1.0", 17 | "vue-loader": "^16.0.0-alpha.3" 18 | }, 19 | "peerDependencies": { 20 | "@vue/compiler-sfc": "^3.0.0-alpha.4", 21 | "vue": "^3.0.0-alpha.4" 22 | }, 23 | "devDependencies": { 24 | "@types/jscodeshift": "^0.7.0", 25 | "jest": "^25.1.0", 26 | "jscodeshift": "^0.7.0", 27 | "prettier": "^1.19.1" 28 | } 29 | } 30 | --------------------------------------------------------------------------------