TL;DR: In this tutorial, you will be introduced to Vue’s command line interface tool and all the features that the new version ships with.
Vue JS created by Evan You and over 200 open source community lovers which has accumulated more than 121,000 stars on GitHub is a very progressive framework for building user interfaces. It consists of an approachable core library that focuses developers on the view layer of the web applications only, and an ecosystem of supporting libraries that helps you tackle complexity in large single-page applications.
A few months ago, Vue CLI version 3.0 was released by the Vue JS team. In this post, you will be shown all the exciting new features that this new version shipped with and notes on how to get started.
As the Vue JS team is working on the next biggest version of Vue which will be version 3, a lot of momentum is being gathered as we wait. I think the CLI tool being in version 3 is most definitely in line with the Vue calendar for releases to match 3.0 when it finally comes out, this can be at any moment in 2019. Shortly after the release of the CLI version 3.0, Vue 2.6 was released. This was also strategic as it was released on the fifth anniversary of Vue.
As Vue continues to record massive success in the adoption war among frontend JavaScriptframeworks, the team has made it a priority to continue to break down barriers to entry especially for beginners and people who are new to the Vue community. This prompted the decision to totally re-write the CLI tool because of the constant feedback from developers highlighting the struggles and sometimes confusion with lots of configuration while trying to bootstrap Vue projects. So, to bring even more clarity to the setup process, the new CLI tool was built.
This new version of the CLI has two distinct platforms of support that makes it very progressive and ahead of its time as the JavaScript community keeps evolving.
This is what the Vue team calls their new Babel incorporation into the CLI tool to accommodate all the newest JavaScript features in ES2015+ and at the same time still ship bundles with polyfills to cater for older and yet-to-be-updated browsers. When building for production with the following command:
vue-cli-service build --modern
The Vue CLI will outputs two versions of the application: a modern version targeting modern browsers and a kind of legacy bundle supporting older browsers. There are no extra requirements to use the modern mode. The generated HTML file automatically employs the techniques discussed in Phillip Walton’s excellent post:
<script type="module">
, in browsers that support it; they are also preloaded using <link rel="modulepreload">
instead.<script nomodule>
, which is ignored by browsers that support ES modules.<script nomodule>
in Safari 10 is also automatically injected.After testing, a simple beginner app created with the Vue CLI shows that the modern bundle is 16% smaller than the normal bundle.
Vue components can now be built into web components with a line of command:
vue-cli-service build --target wc --name my-element src/MyComponent.vue
A JavaScript bundle is generated registering the internal Vue component as a native custom element on the page, which can then be used as <my-element>
.
Multiple Vue components too can be built into a bundle with multiple chunks with this command:
vue-cli-service build --target wc-async 'src/components/*.vue'
By including a small entry file from the resulting bundle, it registers all components as native custom elements, but only fetches the code for the underlying Vue component when the corresponding custom element is first instantiated on the page.
Sometimes, we do not really want to wait for npm installations or creation of a full fledged project to create a quick component. With the new CLI, you can now create standalone single file Vue components with all the powers of a Vue project from any file location in your machine - this is my favorite feature.
To be able to access this super feature, you have to install the Vue CLI service globally with this command:
npm install -g @vue/cli-service-global
That’s all, you can go ahead and create a file anywhere on your machine. If your single file is called test.vue
``for instance then you can serve it on a development server just as you would with a complete Vue project with this command:
vue serve test.vue
This spins up your Vue app in localhost, exactly as it will with a full project, awesome right?
This new CLI tool was re-written from the ground up to be plugin-based. This means that all the custom and default features like ESLint and Babel are treated as plugins. Plugins can inject dependencies and files during the app’s scaffolding phase, and tweak the app’s webpack config or inject additional commands to the CLI service during development. Plugins affect Vue core component and files and with the Plugin API by Vue, developers can create their own plugin. There is now a kind of plugin marketplace (think VS Code extensions) in Vue through the CLI. Plugins can be easily added using a line of command like this:
vue add pluginName
Where pluginName can be TypeScript, Vuex or any other plugin. The add
command acts exactly like it does in Angular with “ng add”. If you are interested in writing your own plugin, check out the plugin dev guide.
The idea of choosing templates on startup is now a thing of the past, now you select presets: default presets or custom presets where you can choose your own plugins to get started with.
In the bid to totally break entry barriers to getting started with Vue, the Vue team also built a fully functional GUI tool for the CLI with a shiny looking configuration dashboard.
With this GUI tool, you can do almost everything you can with the CLI: create a project, serve it up, perform linting and even build it out for production. You can also install plugins and dependencies with it, also handle basic debugging and a whole lot more. The only thing I found was not yet supported in the GUI tool is accessing the single file Vue components.
To use the GUI tool, you have to simply run the ui
``command in your machine:
vue ui
The dashboard like the one pictured above would run on an available port on your localhost.
At the core, Vue CLI provides a pre-configured build setup built on top of webpack 4. To help reduce the amount of configuration developers have to go through, every Vue CLI 3 project comes with out-of-the-box support for:
.env
filesAlso optionally, you can add these plugins to your preset on startup: TypeScript, PWA, Vue Router & Vuex, ESLint / TSLint / Prettier, Jest or Mocha, Cypress or Nightwatch and many others alike. Vue CLI makes sure all the above features work nicely together so that you do not have to do any config dirty work yourself.
To use the new version 3, uninstall the old CLI tool:
npm uninstall -g vue-cli
Then install the new CLI with this command:
npm install -g @vue/cli
Also to create a new Vue project with the CLI, there is a new syntax:
vue create myapp
Where myapp is the name of the Vue app you intend building.
You have been introduced to all the new features of the Vue CLI 3.0 and the reasoning behind the features. The GUI and the instant prototyping features are my personal favorites, what are yours?