Reader small image

You're reading from  Vue.js 2 Cookbook

Product typeBook
Published inApr 2017
Reading LevelBeginner
PublisherPackt
ISBN-139781786468093
Edition1st Edition
Languages
Right arrow
Author (1)
Andrea Passaglia
Andrea Passaglia
author image
Andrea Passaglia

Andrea Passaglia was born in Genoa, in northern Italy. Interested about technology since his parents gave him a toy computer when he was a boy, he started studying web technologies at an early age. After obtaining his master's degree in computer engineering he worked on the design and implementation of web interfaces for companies of various sizes and in different industries (healthcare, fashion, tourism, and transport). In 2016 he moves in the silicon valley of Europe to tackle new problems in the banking industry at the Edgeverve Dublin Research and Development Labs. A backend technologist by trade, Vue.js is his first tool to bring to life his creations when it comes to the frontend. Andrea is married to a lovely Russian girl from Siberia and they often cook together mixing culinary traditions.
Read more about Andrea Passaglia

Right arrow

Chapter 8. Organize + Automate + Deploy = Webpack

In this chapter, we will talk about the following topics:

  • Extracting logic from your components to keep the code tidy
  • Bundling your component with WebpackPreview
  • Organizing your dependencies with Webpack
  • Using external components in your Webpack project
  • Developing with continuous feedback with hot reloading
  • Using Babel to compile from ES6
  • Running a code linter while developing
  • Using only one command to build both a minified and a development .js file
  • Releasing your components to the public

Introduction


Webpack coupled with npm is a very powerful tool. In essence, it's just a bundler that takes some files along with their dependencies and bundles everything into one or more consumable files. It's now in its second version and represents much more than before, especially for Vue developers.

Webpack will enable you to write components conveniently isolated in single files and shippable on command. It will enable you to use different JavaScript standards, such as ES6, but also other languages altogether, all thanks to loaders, a concept that will recur in the following recipes.

Extracting logic from your components to keep the code tidy


Vue components can become very complex sometimes. In these cases, it's better to split them up and try to hide some complexity with abstraction. The best place to put such complexity is external JavaScript files. This way you have the added benefit that, if necessary, it's easier to share the extracted logic with additional components.

Getting ready

This recipe is of intermediate level. Before coming here, you should have completed the Choosing a development environment recipe in Chapter 1, Getting Started with Vue.js, and should know how to set up a project with npm.

Also, ensure that you have the vue-cli package installed globally with the following command:

npm install -g vue-cli

How to do it...

We will build a calculator for compound interest; you will discover how much money you will have after an initial investment.

Creating a clean Webpack project

Create a new directory and a new Vue project inside it with the following command:

vue...

Bundling your component with Webpack


Webpack lets you package your project in minified JavaScript files. You can then distribute these files or use them yourself. When you use the inbuilt templates that come with vue-cli, Webpack is configured to build an entire working application with it. Sometimes we want to build a library to publish or use in another project. In this recipe, you will tweak the default configuration of the Webpack template to release a component instead.

Getting ready

This recipe will make sense to you only after you have installed npm (refer to the Choosing a development environment recipe in Chapter 1, Getting Started with Vue.js) and got familiar with vue-cli and the Webpack template.

 

How to do it...

For this recipe, you will build a reusable component that shakes whatever you put into it; for this, we will use the excellent CSShake library.

Create a new clean project based on the Webpack template. You can take a look at the previous recipe to see how to do that, or you...

Organizing your dependencies with Webpack


Webpack is a tool for organizing your code and dependencies. Furthermore, it gives you a way to develop and build with JavaScript files that embed all the dependencies and modules that we pass to them. We'll use this in this recipe to build a small Vue application and bundle everything in a single file.

Getting ready

This recipe doesn't require any particular skill except the use of npm and some knowledge of the command line. You can find out more in the Organizing your dependencies with Webpack recipe in this chapter.

How to do it...

Create a new folder for your recipe and create a package.json file with the following content inside it:

{
 "name": "recipe",
 "version": "1.0.0"
}

This defines an npm project in our folder. You can, of course, use npm init or yarn init if you know what you're doing.

We will install Webpack 2 for this recipe. To add it to your project dependencies, run the following command:

npm install --save-dev webpack@2

The --save-dev option...

Using external components in your Webpack project


Using external Vue components in your own project is usually straightforward. Sometimes though, things aren't so simple. In particular, there are some configurations in the official templates with Webpack that (weirdly) actually prevent you from using some external components. In this recipe, we will install a modal dialog component from the Bulma project.

Getting ready

In this recipe, we will tweak the Webpack configuration. It is suggested to have completed the Organizing your dependencies with Webpack recipe before taking up this task.

How to do it...

We will start with a fresh Webpack project. You can create a new one using the vue-cli and the official Webpack template. My suggestion, however, is to begin with my Webpack template, which is a clean slate. To do it, run the following command in a new directory:

vue init gurghet/webpack

We will install vue-bulma-modal, which is a component written in Vue with the Bulma CSS framework:

npm install...

Developing with continuous feedback with hot reloading


Hot reloading is a really useful technology that lets you develop while looking at the results in the browser, without even refreshing the page. It's a very tight loop and can really speed up your development process. In the official Webpack template, hot reloading is installed by default. In this recipe, you will learn how to install it yourself.

Getting ready

Before attempting this recipe, you should have at least a vague idea of how Webpack works; the Organizing your dependencies with Webpack recipe in this chapter will have you covered.

How to do it...

Create a new npm project in a new directory, either with npm init -y or yarn init -y. I personally prefer the second one because the resulting package.json is much more compact.

Note

To install Yarn, you can use the npm install -g yarn command. The main benefit of Yarn is that you will be able to lock your dependencies to a known version. This prevents bugs when working in teams and the...

Using Babel to compile from ES6


ES6 has a lot of useful features, and in this recipe you will learn how you can use it in your projects. It's worth noting that ES6 currently has very good browser support. You won't have compatibility issues with 80% of the browsers in the wild, but you may need to even reach people who're still using Internet Explorer 11, depending on your audience, or you may just want to maximize your audience. Moreover, some tools for development and Node.js still don't fully support ES6, deeming Babel necessary even for development.

Getting ready

In this recipe, we will use npm and the command line. If you completed the Choosing a development environment recipe in Chapter 1, Getting Started with Vue.js, you are probably good to go.

How to do it...

Create a new directory with an empty npm project. You can use the npm init -y command or, if you have Yarn installed, you can use yarn init -y inside the directory. This command will create a new package.json inside the directory...

Running a code linter while developing


Linting your code drastically reduces small bugs and inefficiencies that accumulate during development, it guarantees that the coding style is consistent across a team or organization, and it makes your code more readable. Instead of running the linter once in a while, it's useful to have it constantly running. This recipe teaches you how to do it with Webpack.

Getting ready

In this recipe, we will play with Webpack once again. You will build a tight loop with webpack-dev-server, which is covered in the Developing with continuous feedback with hot reloading recipe.

How to do it...

In a new folder, create a new npm project (you can use npm init -y or yarn init -y).

Inside the folder, create a new directory named src and put a file inside it, called MyComp.vue. Let the file contain the following code:

<template>
  <div>
    Hello {{name}}!
  </div>
</template>
<script>
export default {
  data () {
    return {
      name: 'John...

Using only one command to build both a minified and a development .js file


While working on the release of your components, you may need a reliable process to issue your built files. A common operation is to release two versions of a library/component: one for development purposes and one to be consumed in production code, usually minified. In this recipe, you will tweak the official template to release both a minified and a development JavaScript file at the same time.

Getting ready

This recipe makes sense if you are already building and distributing your own components. If you want to learn more, I suggest you refer to the Bundling your component with Webpack recipe.

How to do it…

We'll start with a project with the official Webpack template. You can use your own, or you can spin up a new project with vue init webpack and install the dependencies with npm isntall.

Go inside the build directory. When you launch the npm run build command, you are effectively launching the build.js file in this...

Releasing your components to the public


At a certain point, there comes a moment when you want to give back to the community. Maybe you built a "fart button" or maybe you built an automates stock options trader; whatever it is that you've built, the JavaScript and Vue community will be happy to welcome you. There is a big chunk of things to be done on the side of marketing and licensing, but in this recipe you will concentrate on the more technical aspects.

Getting ready

This recipe is directed at those who want to share their work in Vue with the rest of the community. In the Bundling your component with Webpack recipe, you will find how to tweak the official Webpack template to bundle your component correctly; this recipe can be thought of as a second part. We will not use the official template though.

How to do it...

The approach I will take for this recipe is to use the excellent vue-share-components template by Guillaume Chau. We'll build a joke button from that starting point.

In your command...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Vue.js 2 Cookbook
Published in: Apr 2017Publisher: PacktISBN-13: 9781786468093
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Andrea Passaglia

Andrea Passaglia was born in Genoa, in northern Italy. Interested about technology since his parents gave him a toy computer when he was a boy, he started studying web technologies at an early age. After obtaining his master's degree in computer engineering he worked on the design and implementation of web interfaces for companies of various sizes and in different industries (healthcare, fashion, tourism, and transport). In 2016 he moves in the silicon valley of Europe to tackle new problems in the banking industry at the Edgeverve Dublin Research and Development Labs. A backend technologist by trade, Vue.js is his first tool to bring to life his creations when it comes to the frontend. Andrea is married to a lovely Russian girl from Siberia and they often cook together mixing culinary traditions.
Read more about Andrea Passaglia