Intro and Setup
Create a new vue app with the vite starter kit
Open a terminal and change directories to a folder where we can work on several projects during this workshop.
We’ll use vite's template system to scaffold a starting point for us to play around with:
- Run
npm create vite@latest
. It might prompt you to install thevite
template script.
- Name the project
codemashdemo
when prompted.
- When asked for a framework, use the arrow keys to select
Vue
and pressENTER
.
- When asked for a language, select
TypeScript
and pressENTER
.
Congratulations, your new empty project is created! Let’s get it up and running.
Install dependencies
- Change directories into the folder that vite created for you.:
cd codemashdemo
- Install all the libraries using the
npm install
command.
Boot it up
Start up the app using the npm run dev
command.
The terminal should print out a URL, most likely http://localhost:5173 (it might be different on your machine).
You can copy & paste that to your browser, or press o
then Enter
to have the Vite dev server open it in your default browser automatically.
At this point, you’re up and running!
Explore some features
Many frameworks offer “Hot Module Reload.” This feature allows the system to automatically apply new code without restarting the app or reloading the page. The build tool (Vite in this case) watches your code for changes, and when something is saved, it tells the browser to patch the code and redraw only the modified components.
Feedback is nearly instantaneous! Try it out:
Using HMR to update the UI
Open src/components/App.vue
in your editor. Find the line where the HelloWorld
component is rendered in the markup:
<HelloWorld msg="Vite + Vue" />
Edit the msg
attribute to msg="Vite + Vue @ CodeMash"
. Hit save and switch back to your browser. It changed automatically! If you can arrange your windows so you can see both the browser and your code at the same time, you’ll be able to watch your changes instantly sync.
Using HMR to update logic
It’s not just UI code that you can change, but logic too. We’ll see how you can change the code and keep the current state of the application.
Before making some code changes, click the count button in your browser a few times so that the count is more than 0.
Now let’s change the code: open src/components/HelloWorld.vue
in your editor. Find the @click
attribute on the <button>
tag. Change it so that it increments the count
value by 2:
<button type="button" @click="count += 2">count is {{ count }}</button>
Hit save and go back to your browser.
Now when you click the button, the count value goes up by 2. Notice that the counter didn’t reset to 0, as if you had hit the browser’s Reload button. The logic changed, but the counter kept its previous state.
This can help you quickly test logic changes deep in a workflow without having to start over at the beginning.
Building for Production
Back in your terminal press CTRL+C
to stop the npm run dev
command. This stops the server.
Run npm run build
. This command compiles your application to static files and puts them in the dist
folder. You could copy these files to your web server and access them over the internet.
It generated a few files (note that your filenames might be slightly different than mine):
$ tree dist
dist
├── assets
│ ├── index-c0MmWt-x.js
│ └── index-rMl8e96B.css
├── index.html
└── vite.svg
2 directories, 4 files
Even though we had multiple .vue
files written in typescript in the source code, the output is just a single .js
file. This can improve load times by only reading a single file over the internet rather than dozens of smaller files.
Also notice how the filename has some random letters at the end (again, your letters might be different than mine). Make a small change to any of the .vue
files, for example adding your name to the template. Run npm run build
again. Now notice how the gibberish is different.
These letters are a hash of the file contents: if the file changes, it gets a new name. You can configure your server to tell browsers they can cache the files forever: if you make changes, a new filename will be created and you won’t have to worry about browsers running stale code.
Recap
- We learned how to use vite to scaffold a starter vue project we can build on
- We learned how the vite dev server monitors you code for changes and automatically and swiftly applies them to the browser so you can enjoy short feedback cycles
- We learned how to build for production and bundle our code into optimized bundles
Further Reading
Bonus Exercises
- Break the template in a few ways. For example misspell
count
in the@click
attribute or in the text of the button. What happens? Open the browser’s developer tools (F-12). What does it show? Does your text editor give you any feedback about the mistake?
- Change the color of the
.read-the-docs
CSS class and see how hot module reload works for CSS as well
- Add an
import
command at the top of the.vue
file for a file that doesn't exist. What errors do you get?
- Download the Vue Dev Tools for your browser (Chrome, Edge, or Firefox) and explore your app using them