Skip to main content
  1. Posts/

Building custom elements from Vue using the CLI

·498 words·3 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.

If you’re working in Vue, the Vue CLI is an amazing thing. There are a lot of moving parts in modern JavaScript development and the CLI makes managing these very easy.

One of the niceties that the CLI will give you is the ability to compile your Vue components into custom HTML tags. I’ve talked about how to get this set up in [What are custom HTML elements](What are custom HTML elements.md) and in [Building custom elements from Vue CLI](Building custom elements from Vue CLI.md). This article is about how to then compile and export those components so that they are ready to put on any web page.

Transpiling Vue to Custom Components
#

The process of turning Vue into an official, browser supported Web Component is handled via a Vue build tool called @vue/web-component-wrapper. This will take your component and build it into the format needed by the Web Component standard. This transpiling from a Vue component to a custom component is already built into Vue CLI, so if your project was created with Vue CLI, all you have to do to export a component to a custom component is this:

vue-cli-service build --target wc --name my-blink 'src/components/Blink.vue'

This will create three files in your dist folder:

FilenamePurpose
my-blink.jsThe code for your component minus the Vue library
demo.htmlA simple demo HTML file for your component
my-blink.js.mapThe debugging information for your browser
my-blink.min.jsThe minified version of the component
my-blink.min.js.mapThe debugging information for the minified version

The important thing to know here is that you must include the Vue library in your HTML wherever you want to use your component. It’s not included in the build itself. If you look at the demo HTML file that was created during the build process, you’ll see that in action.

<script src="https://unpkg.com/vue"></script>
<script src="./my-blink.js"></script>

<my-blink></my-blink>

You can then use that demo.html file to test out the component.

What if you have more than one component?
#

What if you have multiple components that you want to turn into Web Components? You don’t need to build each one individually, you can instead build them all at once.

vue-cli-service build --target wc-async --name demo 'src/components/*.vue'

This will build your components into multiple files that are actually lazy loaded into the browser. If you don’t use a certain component, its file won’t be loaded in. Nice!

But what about the --name? When you defined that in your single component build, it made the tag that name. But here you have multiple components. What does it do now?

Here, --name is used as a prefix for your tags. So if your Vue component is named blink and the --name is defined as demo, then your tag will export as demo-blink.

And that’s it
#

Now you can build your Vue components, take the js files from dist and drop them onto any web page. Then just include the tag anywhere in your HTML and BOOM Vue components on any page.

Related

Setting up a Vue CLI project for building Custom HTML Elements

·1082 words·6 mins
I talked about what custom HTML elements were in my last post. Today, I’m going to walk through the process of getting a new Vue CLI project off the ground so that you can build your own. I’ll be using the <my-blink> tag example again, but will be focusing on the step by step1 instructions for creating the environment in which to build and deploy the tags you will be creating.

What are custom HTML elements?

·771 words·4 mins
As I’ve been looking at how to structure my JavaScript in my applications using modern JavaScript frameworks, I keep running into the same issue. I want to use component-based design, but I’m not always looking to build a Single Page Application. Sometimes, I just want to add a component onto a page that isn’t tied to the framework at all.

My Current (and Completely Free) Developer Tools Setup

·834 words·4 mins
Having been a web developer for over 20 years, I’ve gone through a lot of tools and IDEs over the years. As 2019 kicks off, I thought I would document my current development environment for anyone who’s interested in what I use. As a note, I mainly work with PHP using the Laravel framework, JavaScript writing Riot and Vue components and deploy to a WebFaction cloud server.