Daisy Rees

Daisy Rees

1565580844

How to Reduce Vuejs Bundle Size in Webpack

I work on the Industry 4.0 team at Stanley Black & Decker. Our team recently created the equivalent of an App Store for Stanley’s manufacturing plants worldwide. Factories can visit the marketplace and select what applications they need based on the products they are producing at that location. This will create a custom build that bundles all of these applications together for the plant to run. Due to the bundling of such a large number of applications our Vue build for production resulted in multiple warnings about excess size.

Size of our build initially

When we do a build we get the following 2 error messages:

Vue recommends that bundles not exceed a size of 244 KiB. We have 14 assets alone where each exceeds this size. In addition, we have four entry points that are also above the recommended size. Here is what I did to reduce the size of our build in half.

What is causing the large build bundles?

First I needed to understand what was causing the large build bundle sizes. To do that I installed webpack-bundle-analyzer. This will provide a visual guide to the size of items in each bundle.

 npm install --save-dev webpack-bundle-analyzer

Next, I configure webpack to use it in the vue.config.js file. Here is what my vue.config.js file looks like:

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
   .BundleAnalyzerPlugin;
   module.exports = {
       configureWebpack: {
           plugins: [new BundleAnalyzerPlugin()]
       }
  };

With the plugin installed when I run build for production again, I can see that my build is 2.48MB. From the image I can see the biggest culprits in size are clearly:

  • vue-echarts
  • vuetify
  • moment
  • lodash

Reducing the size of Lodash

Lodash was taking up 70.74kb of space. Lodash is only used in 2 places in all of the applications in our framework. That is a lot of space for just 2 methods.

Not only were we loading lodash but we were also loading vue-lodash. The first step was to remove vue-lodash from our package.json since it was not needed.

The next step was to import only the two items that we needed from lodash instead of loading the entire library. We were using cloneDeep and sortBy. I replace the initial call that was importing the entire lodash library:

 import _ from 'lodash';

I am replacing it with this call that imports just the 2 items that we need. To do that I change the import from lodash to lodash/core:

 import { cloneDeep, sortBy } from 'lodash/core';

Making this one change has reduced the size of my build bundle from 2.48MB to 2.42MB. Here is the image showing the current size of the build.

Here we can see the size of lodash itself as part of our build bundle.

Reducing the size of moment.js

Moment.js is taking up 234.36KB in size in our bundle. When you look at the image, the overwhelming largest part of that size is the internationalization locales for all the languages that they support. We do not use this part of moment.js at all so this is a lot of dead weight that is being included in our bundle.

Luckily, we can remove it. Instead of importing all of moment.js with this call:

 import moment form 'moment';

We can import just the date manipulation code only with this call:

 import moment from 'moment/src/moment'

There is a catch in making this replacement at least in our codebase. There are 18 places where moment.js is imported in the code. I could have done a global search and replace in the code. But if we add a new application to the framework it is quite possible a developer would use the default call to import moment.js. If they do that then we would be back with importing all the internationalization locales again.

So the tradeoff was to create a shortcut alias in webpack. The shortcut would substitute all calls that import ‘moment’ with ‘moment/src/moment’. We can add that alias in our vue.config.js file using resolve and setting an alias. Here is what my vue.config.js file looks like now.

When I run our build for production now, our bundle has dropped down now to 2.22MB in size.

When you look at moment.js in the image, you will see that the internationalization locales are no longer being loaded at all.

By removing the locales in moment.js, this introduced an error whenever I start my server to run my code that says it cannot find ./locale. After doing some research I discovered that this has been a known issue with moment.js for several years in that moment.js always loads and assumes the locales are present. You cannot tell moment to load just the date manipulation functionality.

To resolve this I use the built-in webpack IgnorePlugin to ignore this message. Here is the plugin code that I added to my vue.config.js file:

new webpack.IgnorePlugin(/^\\.\\/locale$/, /moment$/)

Reducing the size of Vuetify.js

The next thing I want to target is the size of Vuetify.js. Vuetify is taking up 500.78KB in space. That is a huge amount of space for one vendor product.

Vuetify provides a feature that they call a-la-carte. This allows you to import only the Vuetify components that you use. This would reduce the size Vuetify. The challenge is that we have so many applications that going through and trying to determine just the components that we are using was not going to happen.

In the current version of Vuetify (version 1.56 at the time I wrote this article) they are providing a product called vuetify-loader. It will go through your code and determine all the components you are using and then import just them into your build bundle. Note: Eventually vuetify v2 will have this feature built-in. Until that release is available you have to use vuetify-loader to import just the components that you are using. Vuetify documentation states that to obtain all the required styles, we need to import them in stylus.

I realized that we are running an older version of vuetify.js. So I decide to upgrade my version of vuetify to the latest version. I also install the styles and vuetify-loader at the same time with:

npm install vuetify vuetify-loader stylus stylus-loader style-loader css-loader --save

My plugin code to import Vuetify has some customization for the theme to use our company’s color palette. Here is what my current plugin for Vuetify looks like:

I will need to change the import for Vuetify to import from vuetify/lib. I will also import stylus to get all the styles. Here is what my plugin code looks like now:

The last step is to tell webpack to use the vuetify-loader plugin so that it will import only the components that we are using. I will require the plugin and then add it to the plugins array. Here is my vue.config.js file:

Now when I run my build for production my bundle size is 2MB.

Reducing the size of vue-echarts

Vue-echarts is not the largest item I have in my bundle. Vue-echarts runs on top of echarts. Like Vuetify, I am running an older version of both products. to upgrade them both to the latest version I run this command:

 npm install echarts vue-echarts --save

I did some research on vue-echarts GitHub repo looking at all the closed issues to find that the latest version of vue-echarts allows you to load a smaller bundle by changing what you import. Previously I was importing it using this command:

 import ECharts from 'vue-echarts';

I change it to this:

 import ECharts from 'vue-echarts/components/ECharts.vue';

Now when I run a build for production my bundle size is down to 1.28MB.

Conclusion

My goal was to reduce the size of our bundle created for production for our application. The initial size of my build was 2.48MB. By making a few changes I was able to reduce our build size down to 1.2MB. That is an almost 50% reduction in size.

If you are creating production Vue applications you should take the time to evaluate your build size. Use the webpack-bundle-analyzer to determine what items are consuming the most space. Then start to take steps necessary to reduce the size of those items. I was able to reduce the size of the four largest items in my bundle this way.

Hopefully, you will be able to follow these steps to reduce the size of your build for production. If you have any questions or comments, please post them below. Thank you very much for reading.

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

Nuxt.js - Vue.js on Steroids

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

Build a Progressive Web App In VueJs

Build a CMS with Laravel and Vue

Beginner’s Guide to Vue.js

Hands-on Vue.js for Beginners

Top 3 Mistakes That Vue.js Developers Make and Should be Avoided

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You

Vue.js Tutorial: Zero to Sixty


#vue-js #javascript #webpack #web-service

What is GEEK

Buddha Community

How to Reduce Vuejs Bundle Size in Webpack

Hire Dedicated VueJS Developers

Want to Hire VueJS Developer to develop an amazing app?

Hire Dedicated VueJS Developers on the contract (time/project) basis providing regular reporting about your app. We, at HourlyDeveloper.io, implement the right strategic approach to offer a wide spectrum of vue.js development services to suit your requirements at most competitive prices.

Consult with us:- https://bit.ly/2C5M6cz

#hire dedicated vuejs developers #vuejs developer #vuejs development company #vuejs development services #vuejs development #vuejs developer

Daisy Rees

Daisy Rees

1565580844

How to Reduce Vuejs Bundle Size in Webpack

I work on the Industry 4.0 team at Stanley Black & Decker. Our team recently created the equivalent of an App Store for Stanley’s manufacturing plants worldwide. Factories can visit the marketplace and select what applications they need based on the products they are producing at that location. This will create a custom build that bundles all of these applications together for the plant to run. Due to the bundling of such a large number of applications our Vue build for production resulted in multiple warnings about excess size.

Size of our build initially

When we do a build we get the following 2 error messages:

Vue recommends that bundles not exceed a size of 244 KiB. We have 14 assets alone where each exceeds this size. In addition, we have four entry points that are also above the recommended size. Here is what I did to reduce the size of our build in half.

What is causing the large build bundles?

First I needed to understand what was causing the large build bundle sizes. To do that I installed webpack-bundle-analyzer. This will provide a visual guide to the size of items in each bundle.

 npm install --save-dev webpack-bundle-analyzer

Next, I configure webpack to use it in the vue.config.js file. Here is what my vue.config.js file looks like:

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
   .BundleAnalyzerPlugin;
   module.exports = {
       configureWebpack: {
           plugins: [new BundleAnalyzerPlugin()]
       }
  };

With the plugin installed when I run build for production again, I can see that my build is 2.48MB. From the image I can see the biggest culprits in size are clearly:

  • vue-echarts
  • vuetify
  • moment
  • lodash

Reducing the size of Lodash

Lodash was taking up 70.74kb of space. Lodash is only used in 2 places in all of the applications in our framework. That is a lot of space for just 2 methods.

Not only were we loading lodash but we were also loading vue-lodash. The first step was to remove vue-lodash from our package.json since it was not needed.

The next step was to import only the two items that we needed from lodash instead of loading the entire library. We were using cloneDeep and sortBy. I replace the initial call that was importing the entire lodash library:

 import _ from 'lodash';

I am replacing it with this call that imports just the 2 items that we need. To do that I change the import from lodash to lodash/core:

 import { cloneDeep, sortBy } from 'lodash/core';

Making this one change has reduced the size of my build bundle from 2.48MB to 2.42MB. Here is the image showing the current size of the build.

Here we can see the size of lodash itself as part of our build bundle.

Reducing the size of moment.js

Moment.js is taking up 234.36KB in size in our bundle. When you look at the image, the overwhelming largest part of that size is the internationalization locales for all the languages that they support. We do not use this part of moment.js at all so this is a lot of dead weight that is being included in our bundle.

Luckily, we can remove it. Instead of importing all of moment.js with this call:

 import moment form 'moment';

We can import just the date manipulation code only with this call:

 import moment from 'moment/src/moment'

There is a catch in making this replacement at least in our codebase. There are 18 places where moment.js is imported in the code. I could have done a global search and replace in the code. But if we add a new application to the framework it is quite possible a developer would use the default call to import moment.js. If they do that then we would be back with importing all the internationalization locales again.

So the tradeoff was to create a shortcut alias in webpack. The shortcut would substitute all calls that import ‘moment’ with ‘moment/src/moment’. We can add that alias in our vue.config.js file using resolve and setting an alias. Here is what my vue.config.js file looks like now.

When I run our build for production now, our bundle has dropped down now to 2.22MB in size.

When you look at moment.js in the image, you will see that the internationalization locales are no longer being loaded at all.

By removing the locales in moment.js, this introduced an error whenever I start my server to run my code that says it cannot find ./locale. After doing some research I discovered that this has been a known issue with moment.js for several years in that moment.js always loads and assumes the locales are present. You cannot tell moment to load just the date manipulation functionality.

To resolve this I use the built-in webpack IgnorePlugin to ignore this message. Here is the plugin code that I added to my vue.config.js file:

new webpack.IgnorePlugin(/^\\.\\/locale$/, /moment$/)

Reducing the size of Vuetify.js

The next thing I want to target is the size of Vuetify.js. Vuetify is taking up 500.78KB in space. That is a huge amount of space for one vendor product.

Vuetify provides a feature that they call a-la-carte. This allows you to import only the Vuetify components that you use. This would reduce the size Vuetify. The challenge is that we have so many applications that going through and trying to determine just the components that we are using was not going to happen.

In the current version of Vuetify (version 1.56 at the time I wrote this article) they are providing a product called vuetify-loader. It will go through your code and determine all the components you are using and then import just them into your build bundle. Note: Eventually vuetify v2 will have this feature built-in. Until that release is available you have to use vuetify-loader to import just the components that you are using. Vuetify documentation states that to obtain all the required styles, we need to import them in stylus.

I realized that we are running an older version of vuetify.js. So I decide to upgrade my version of vuetify to the latest version. I also install the styles and vuetify-loader at the same time with:

npm install vuetify vuetify-loader stylus stylus-loader style-loader css-loader --save

My plugin code to import Vuetify has some customization for the theme to use our company’s color palette. Here is what my current plugin for Vuetify looks like:

I will need to change the import for Vuetify to import from vuetify/lib. I will also import stylus to get all the styles. Here is what my plugin code looks like now:

The last step is to tell webpack to use the vuetify-loader plugin so that it will import only the components that we are using. I will require the plugin and then add it to the plugins array. Here is my vue.config.js file:

Now when I run my build for production my bundle size is 2MB.

Reducing the size of vue-echarts

Vue-echarts is not the largest item I have in my bundle. Vue-echarts runs on top of echarts. Like Vuetify, I am running an older version of both products. to upgrade them both to the latest version I run this command:

 npm install echarts vue-echarts --save

I did some research on vue-echarts GitHub repo looking at all the closed issues to find that the latest version of vue-echarts allows you to load a smaller bundle by changing what you import. Previously I was importing it using this command:

 import ECharts from 'vue-echarts';

I change it to this:

 import ECharts from 'vue-echarts/components/ECharts.vue';

Now when I run a build for production my bundle size is down to 1.28MB.

Conclusion

My goal was to reduce the size of our bundle created for production for our application. The initial size of my build was 2.48MB. By making a few changes I was able to reduce our build size down to 1.2MB. That is an almost 50% reduction in size.

If you are creating production Vue applications you should take the time to evaluate your build size. Use the webpack-bundle-analyzer to determine what items are consuming the most space. Then start to take steps necessary to reduce the size of those items. I was able to reduce the size of the four largest items in my bundle this way.

Hopefully, you will be able to follow these steps to reduce the size of your build for production. If you have any questions or comments, please post them below. Thank you very much for reading.

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

Nuxt.js - Vue.js on Steroids

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

Build a Progressive Web App In VueJs

Build a CMS with Laravel and Vue

Beginner’s Guide to Vue.js

Hands-on Vue.js for Beginners

Top 3 Mistakes That Vue.js Developers Make and Should be Avoided

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You

Vue.js Tutorial: Zero to Sixty


#vue-js #javascript #webpack #web-service

Dylan North

Dylan North

1557414605

How to Reduce Your Vue.JS Bundle Size With Webpack

I work on the Industry 4.0 team at Stanley Black & Decker. Our team recently created the equivalent of an App Store for Stanley’s manufacturing plants worldwide. Factories can visit the marketplace and select what applications they need based on the products they are producing at that location. This will create a custom build that bundles all of these applications together for the plant to run. Due to the bundling of such a large number of applications our Vue build for production resulted in multiple warnings about excess size.

Size of our build initially

When we do a build we get the following 2 error messages:

Vue recommends that bundles not exceed a size of 244 KiB. We have 14 assets alone where each exceeds this size. In addition, we have four entry points that are also above the recommended size. Here is what I did to reduce the size of our build in half.

What is causing the large build bundles?

First I needed to understand what was causing the large build bundle sizes. To do that I installed webpack-bundle-analyzer. This will provide a visual guide to the size of items in each bundle.

npm install --save-dev webpack-bundle-analyzer

Next, I configure webpack to use it in the vue.config.js file. Here is what my vue.config.js file looks like:

const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
    .BundleAnalyzerPlugin;
module.exports = {
    configureWebpack: {
        plugins: [new BundleAnalyzerPlugin()]
    }
};

With the plugin installed when I run build for production again, I can see that my build is 2.48MB. From the image I can see the biggest culprits in size are clearly:

  • vue-echarts
  • vuetify
  • moment
  • lodash

Reducing the size of Lodash

Lodash was taking up 70.74kb of space. Lodash is only used in 2 places in all of the applications in our framework. That is a lot of space for just 2 methods.

Not only were we loading lodash but we were also loading vue-lodash. The first step was to remove vue-lodash from our package.json since it was not needed.

The next step was to import only the two items that we needed from lodash instead of loading the entire library. We were using cloneDeep and sortBy. I replace the initial call that was importing the entire lodash library:

import _ from 'lodash';

I am replacing it with this call that imports just the 2 items that we need. To do that I change the import from lodash to lodash/core:

import { cloneDeep, sortBy } from 'lodash/core';

Making this one change has reduced the size of my build bundle from 2.48MB to 2.42MB. Here is the image showing the current size of the build.

Here we can see the size of lodash itself as part of our build bundle.

Reducing the size of moment.js

Moment.js is taking up 234.36KB in size in our bundle. When you look at the image, the overwhelming largest part of that size is the internationalization locales for all the languages that they support. We do not use this part of moment.js at all so this is a lot of dead weight that is being included in our bundle.

Luckily, we can remove it. Instead of importing all of moment.js with this call:

import moment form 'moment';

We can import just the date manipulation code only with this call:

import moment from 'moment/src/moment'

There is a catch in making this replacement at least in our codebase. There are 18 places where moment.js is imported in the code. I could have done a global search and replace in the code. But if we add a new application to the framework it is quite possible a developer would use the default call to import moment.js. If they do that then we would be back with importing all the internationalization locales again.

So the tradeoff was to create a shortcut alias in webpack. The shortcut would substitute all calls that import ‘moment’ with ‘moment/src/moment’. We can add that alias in our vue.config.js file using resolve and setting an alias. Here is what my vue.config.js file looks like now.

When I run our build for production now, our bundle has dropped down now to 2.22MB in size.

When you look at moment.js in the image, you will see that the internationalization locales are no longer being loaded at all.

By removing the locales in moment.js, this introduced an error whenever I start my server to run my code that says it cannot find ./locale. After doing some research I discovered that this has been a known issue with moment.js for several years in that moment.js always loads and assumes the locales are present. You cannot tell moment to load just the date manipulation functionality.

To resolve this I use the built-in webpack IgnorePlugin to ignore this message. Here is the plugin code that I added to my vue.config.js file:

new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)

Reducing the size of Vuetify.js

The next thing I want to target is the size of Vuetify.js. Vuetify is taking up 500.78KB in space. That is a huge amount of space for one vendor product.

Vuetify provides a feature that they call a-la-carte. This allows you to import only the Vuetify components that you use. This would reduce the size Vuetify. The challenge is that we have so many applications that going through and trying to determine just the components that we are using was not going to happen.

In the current version of Vuetify (version 1.56 at the time I wrote this article) they are providing a product called vuetify-loader. It will go through your code and determine all the components you are using and then import just them into your build bundle. Note: Eventually vuetify v2 will have this feature built-in. Until that release is available you have to use vuetify-loader to import just the components that you are using. Vuetify documentation states that to obtain all the required styles, we need to import them in stylus.

I realized that we are running an older version of vuetify.js. So I decide to upgrade my version of vuetify to the latest version. I also install the styles and vuetify-loader at the same time with:

npm install vuetify vuetify-loader stylus stylus-loader style-loader css-loader --save

My plugin code to import Vuetify has some customization for the theme to use our company’s color palette. Here is what my current plugin for Vuetify looks like:

I will need to change the import for Vuetify to import from vuetify/lib. I will also import stylus to get all the styles. Here is what my plugin code looks like now:

The last step is to tell webpack to use the vuetify-loader plugin so that it will import only the components that we are using. I will require the plugin and then add it to the plugins array. Here is my vue.config.js file:

Now when I run my build for production my bundle size is 2MB.

Reducing the size of vue-echarts

Vue-echarts is not the largest item I have in my bundle. Vue-echarts runs on top of echarts. Like Vuetify, I am running an older version of both products. to upgrade them both to the latest version I run this command:

npm install echarts vue-echarts --save

I did some research on vue-echarts GitHub repo looking at all the closed issues to find that the latest version of vue-echarts allows you to load a smaller bundle by changing what you import. Previously I was importing it using this command:

import ECharts from 'vue-echarts';

I change it to this:

import ECharts from 'vue-echarts/components/ECharts.vue';

Now when I run a build for production my bundle size is down to 1.28MB.

Conclusion

My goal was to reduce the size of our bundle created for production for our application. The initial size of my build was 2.48MB. By making a few changes I was able to reduce our build size down to 1.2MB. That is an almost 50% reduction in size.

If you are creating production Vue applications you should take the time to evaluate your build size. Use the webpack-bundle-analyzer to determine what items are consuming the most space. Then start to take steps necessary to reduce the size of those items. I was able to reduce the size of the four largest items in my bundle this way.

Hopefully, you will be able to follow these steps to reduce the size of your build for production. If you have any questions or comments, please post them below. Thank you very much for reading.

#javascript #vue-js #webpack

Jam  Dana

Jam Dana

1593332225

Kubernetes & Gitlab + Webpack VueJS — deploy your app completely

Introduction

Kubernetes (k8s) is a portable, extensible, platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It provides exposing container, load balancing, volume storage, auto rollouts and rollbacks, distributes physical resources, self-healing and protects all your configuration. Kubernetes aims to microservices, itself not monolithic, default solutions are optional and pluggable. It also aims to support stateless, stateful, and data-processing workloads.

In this post, we will aim to deploy a Vuejs application very QUICKLY using Gitlab and Kubernetes. For more professional, you can using some tools like Helm (k8s package manager), Vault by HashiCorp (to protect sensitive data) and Prometheus — a monitoring and alerting toolkit. OK, let’s in.

Prerequisite

  1. Yarn as package manager (You may use npm, as well)
  2. Setup a kubernetes cluster on your infrastructure (Read this doc or using cloud service like GKE or EKS). Save the .kubeconfig file, it’s used to authenticate with kubernetes API server.
  3. A gitlab repository
  4. kubectl

Flow

  1. Init base source code
  2. Containerize your app into an image
  3. Save the image on Registry
  4. Apply the image (with config) to k8s
  5. Auto with CI

  1. Let’s start by creating code’s structure:
$ yarn global add vue-cli
$ vue init webpack my-vue-project

The structure will look like this:

2. And then, add Dockerfile:

FROM node:lts-alpine3.11

COPY . /opt/my-vue-project
WORKDIR /opt/my-vue-project
CMD ["yarn", "start"]

NO! Don’t do that. When run locally, webpack reads your config and serves your app at port 8080. In depth, browsers only understand Javascript code so all the Javascript Framework support to generate static files (include HTML, CSS, JS). You just need to serve this static files by Nginx. The container will lightweight and simple like this:

FROM nginx:1.19-alpine

# Run 'yarn build' to see the dist
COPY /dist /usr/share/nginx/html
CMD [ "nginx", "-g", "daemon off;" ]

3. Next step, create some .yaml files. It’s is a parameter send to k8s API server. Server will use resources and setting all up as your config. It pulls image that created above from Gitlab container registry using **imagePullSecrets_. _**I’ll show you how to create secret to authenticate with Gitlab.

#vuejs #gitlab #deployment #webpack #kubernetes

Ferris  Overweg

Ferris Overweg

1574649990

How to Create A Youtube Clone with VueJS, Webpack and Flexbox

In this tutorial we are going to be building our own competitor to YouTube called VueTube. Here’s a screenshot of the finished product!

Prerequisite

Vue is an awesome front-end JavaScript framework for building awesome user interfaces. Webpack is a module bundler for JavaScript which receives assets used in development and bundles them into single static assets. Having a hard time with positioning in CSS? Flexbox simplifies all that.

We shall be using these tools and techniques to build out a Youtube clone.

Getting started

With this tutorial we will be using webpack to build and run single file components. To get started make sure that you have vue-cli installed.

npm i -g vue-cli

Once vue-cli is installed we create a new project using the webpack build template shipped with the Vue CLI. In the command line, run:

vue init webpack vuetube

Answer all the prompted questions. We wont need vue-router for this project.

Create the Video Player component

Now that the project is created we can create our Video Player component. Open the project in the code editor of your choice (I recommend VS Code).

Head to the /src/components/ folder and add a new file called VideoPlayer.vue

Go ahead and edit the component to:

<template>
  <div class="video-player">
    Welcome to VueTube
  </div>
</template>

<script>
export default {
  name: 'VideoPlayer',
  data () {
    return {
    }
  }
}
</script>

<style scoped>
</style>

This is a barebones Vue single-file component. It includes a template for our view, a script section for our components logic and a style section that is scoped and only applies to our component.

We can now tell App.vue to show our VideoPlayer component instead of the out of the box HelloWorld component. To do that we will need to replace the App.vue file in /src with this one.

<template>
  <div id="app">
    <VideoPlayer/>
  </div>
</template>

<script>
import VideoPlayer from './components/VideoPlayer'

export default {
  name: 'App',
  components: {
    VideoPlayer
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
</style>

Now we are importing the correct component and displaying it when the app starts up. Go ahead and start the application and let’s check out our progress so far. Run:

cd vuetube && npm run dev

Creating our data

Now that we have our app set up lets populate some video data. We are going to utilize the vue data object to store our video info. Inside the script tag lets create a variable named videos.

//VideoPlayer.vue
<script>
let videos = [];

export default {
...

We now have an empty array called videos. Let’s add five videos to the array. I am going to add 5 videos from the Tech Reviewer MKBHD. Feel free to use whatever videos you want.

//VideoPlayer.vue
let videos = [
  {
    id: 1,
    title: "18-core iMac Pro Review: Not a Trap!",
    thumbnail:
      "https://i.ytimg.com/vi/jn9mHzXJIV0/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLAvJvk4k_UNB9nst4pFP-txM1TLZA",
    youtubeURL: "https://www.youtube.com/embed/jn9mHzXJIV0",
    creator: "Marques Brownlee",
    likes: 0,
    views: 0
  },
  {
    id: 2,
    title: "Dope Tech: Camera Robots!",
    thumbnail:
      "https://i.ytimg.com/vi/UIwdCN4dV6w/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLDhlan32jHSvicGZezDFPjAOdXGUA",
    youtubeURL: "https://www.youtube.com/embed/UIwdCN4dV6w",
    creator: "Marques Brownlee",
    likes: 0,
    views: 0
  },
  {
    id: 3,
    title: "Let's Talk About Tesla Roadster 2020!",
    thumbnail:
      "https://i.ytimg.com/vi/ctx4YBEdOxo/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLBDnlrC2rVwXamNkicEEbc3Mf4T0w",
    youtubeURL: "https://www.youtube.com/embed/ctx4YBEdOxo",
    creator: "Marques Brownlee",
    likes: 0,
    views: 0
  },
  {
    id: 4,
    title: "Talking Tech with Neil deGrasse Tyson!",
    thumbnail:
      "https://i.ytimg.com/vi/pqQrL1K0Z5g/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLA5hTiwkz4Tr1w1hSMhPlwtmQeyYw",
    youtubeURL: "https://www.youtube.com/embed/pqQrL1K0Z5g",
    creator: "Marques Brownlee",
    likes: 0,
    views: 0
  },
  {
    id: 5,
    title: "The Apple Ecosystem: Explained!",
    thumbnail:
      "https://i.ytimg.com/vi/KB4_WIPE7vo/hqdefault.jpg?sqp=-oaymwEZCNACELwBSFXyq4qpAwsIARUAAIhCGAFwAQ==&rs=AOn4CLCCxXm7aoPShOwON74nhMlGYMUkHw",
    youtubeURL: "https://www.youtube.com/embed/KB4_WIPE7vo",
    creator: "Marques Brownlee",
    likes: 0,
    views: 0
  }
];

So we are storing 6 main attributes. Id, Title, URL, Creator, Likes and Views for each video. Now that we have our data we can start to display it on the screen. The first step is to set our videos variable to the data object.

export default {
  name: 'VideoPlayer',
  data () {
    return {
      videos
    }
  }
}

Looping through our data

Awesome, now we can loop through the data and show a list of the videos that exist on the player. To do that we are going to use v-for.

<div class="video-list">
    <div :key="video.id" v-for="video in videos" class="thumbnail">
        <div class="thumbnail-img">
          <img :src="video.thumbnail" />
        </div>
        <div class="thumbnail-info">
          <h3>{{video.title}}</h3>
          <p>{{video.creator}}</p>
          <p class="thumbnail-views">{{video.views}} Views</p>
        </div>
    </div>
</div>

What we are doing here is looping through the array of videos that we just created. So v-for=”video in videos” is looping through each object or “video” in the videos array.

Since we are looping through the array we can access the fields from each object. In our video list we want to display a thumbnail, creator, views and title. To access those fields within the template we can use the handlebar notation like {{video.title}}

Because the thumbnail needs to be displayed as an image we need to bind the field to the image src. For tags we can’t use the handlebar notation instead we use :src to bind the url.

Refresh your browser and check out what it looks like! You should see something similar to this:

Making the video list pretty

Awesome! Now that we have the video list lets format it. We want the video list to look like the right side of a Youtube video. To get that same look and feel we will be using Flexbox for the layout.

First we want the thumbnail image and the thumbnail info divs to be side by side. To do this we can add just one line to the thumbnail class

<style scoped>
  .thumbnail{
    display:flex;
  }
</style>

Great now we have them side by side let’s add the rest of the styling

.thumbnail img{
    width:168px;
}

.thumbnail-info{
    margin-left:20px;
}

.thumbnail h3{
    font-size:16px;
}

h3,
p{
    margin:0;
    padding:0;
}

.thumbnail-views{
    font-size:14px;
}

Now check out the updates in your browser!

Setting an active video

Our thumbnail list is all set up and now we can display our videos. When the app initially loads we can just display the first video in the array. To do that lets set create a field in the data object called activeVideo this is what we will use to display whatever video the user has picked. To set it to the first video when the page is loaded all we have to do is set activeVideo to videos[0]

export default {
  name: 'VideoPlayer',
  data () {
    return {
      videos,
      activeVideo: videos[0]
    }
  }
}

The active video is now part of the data object meaning that we can access it simply by referencing this.activeVideo inside the template. To show the youtube video we can create an iframe that is bound to the youtubeURL field of our video object.

<div class="video-container">
      <iframe width="640" height="360" :src="this.activeVideo.youtubeURL" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</div>

Let’s add the rest of the video information below the iframe tag.

<iframe ....>
<h3>{{this.activeVideo.title}}</h3>
<div class="row">
    <p>{{this.activeVideo.views}} views</p> 
    <p>{{this.activeVideo.likes}} <button>Like</button></p>
</div>

Awesome! We now have an active video that displays. Let’s make it look good.

.video-player{
    display:flex;
    width:1200px;
    margin:auto;
}

.video-container{
    margin-right:40px;
}

.row{
    display:flex;
    justify-content:space-between;
}

button{
    background:#D0021B;
    color:white;
    border:none;
    padding:10px 20px;
}

Refresh your browser and check it out!

Make it work!

Let’s make it possible for us to choose the video that we want to watch. To do that we will write a method in the script section.

methods:{
  chooseVideo(video){
      //SET VIDEO AS ACTIVE VIDEO
      this.activeVideo = video;
      //INCREASE THE VIDEOS VIEWS BY 1
      video.views += 1;
  }
}

So we have created a method called chooseVideo that takes a video parameter. This method will be used when a user chooses a thumbnail to watch from the video list. When a video is chosen we set the activeVideo to the video that was clicked and we also increase the videos view count by 1.

Now we have to bind the method to a click event on each video in the v-for loop

<div @click="chooseVideo(video)" :key="video.id" v-for="video in videos" class="thumbnail">

Try clicking on a video within the video list! If everything went well then you should be able to watch the video that you clicked on.

Set up the Like Button

Our last step is to set up the like button. We will create another method that adds one to the total likes when the button is clicked.

addLike(){
  this.activeVideo.likes += 1;
}

and then we can bind the addLike method to the like button in the template

<button @click="addLike">Like</button>

Refresh your browser and try to like your videos! You should see the counter going up and it should persist even when you choose a different video.

Add some finishing touches

For our last step we can add our new video players logo. Save the image below as logo.png into the assets folder.

Now we can add the logo to our App.vue file

<div id="app">
    <img style="width:140px; margin:14px;" src="./assets/logo.png"/> 
    <VideoPlayer/>
</div>

Thanks for reading!

Thanks for making it this far. Hope you enjoyed the tutorial. If you have any questions or comments feel free to ask them below. Thank you !

#Vuejs #JavaScript #Development ##Webpack ##Flexbox