In this tutorial, we are going to learn how to create a library and a web component of the Vuejs application, so that we can reuse them as a library in a different application and a custom HTML element in different webpages.
Using VueCli 3.x it's very easy to build our application. It gives some command to create them.
1. Create a sample Vuejs application:
Open the command prompt or terminal and use the following command:
vue create vue-build-target-app
You can either select the Default option so that you don't manually setup. This will default use the yarn package manager so, make sure to have package manager installed. Also, you can manually select features option.
Now, go to the project directory and run the application using the command:
cd vue-build-target-app
yarn serve
If you open the project, you can see the default component called HelloWorld.vue. We are using the same component to test.2. Build as a Library:
Note: In lib mode, Vue is externalized. This means the bundle will not bundle Vue even if your code imports Vue
While building as a library even if we are importing the Vue in the component it will not use that in the bundle.
Building a single component as a library:
vue-cli-service build --target lib --name myLib [entry]
This is the command that we use where myLib is the name of the output bundle file and [entry] is the entity that we use.For e.g:
vue-cli-service build --target lib --name HelloWorld src/components/HelloWorld.vue
This will build the single component HelloWorld.vue as the library. So, what you need to do is place the above command inside the package.json file under scripts.
"build-lib": "vue-cli-service build --target lib --name HelloWorld src/components/HelloWorld.vue",
Now, run the script from the command prompt:
yarn build-lib
This command will build the target files inside dis folder.
<meta charset="utf-8"></meta>
<title>HelloWorld demo</title>
<script src="https://unpkg.com/vue"></script>
<script src="./HelloWorld.umd.js"></script>
<link href="./HelloWorld.css" rel="stylesheet"> </link>
<div id="app">
<demo> </demo>
</div>
<script>
new Vue({
components: {
demo: HelloWorld
}
}).$mount('#app')
</script>
If you run this HTML file then you can see the desire output for that page.dist\HelloWorld.umd.min.js is the minified version of the UMD bundle
dist\HelloWorld.common.js is used in a module-based application
dist\HelloWorld.css is the CSS file that is associated with that component.
You can use those desired files on your web pages.
3. Build as a Web Component:
You can build a single entry as:
vue-cli-service build --target wc --name my-element [entry]
Here wc will build as a web component.
For e.g:
vue-cli-service build --target wc -name HelloWorld src/components/HelloWorld.vue
Use this inside script and run the command:
"build-wc":vue-cli-service build --target wc -name HelloWorld src/components/HelloWorld.vue
Here you can give any name instead of "build-wc" but you need to run the build command with this name
yarn build-wc
Which will create the following files under the dist folder:dist\hello-world.js
dist\hello-world.min.js
This is the web pack bundle that can be used in web pages. Sample example is created under the same folder i.e demo.html.
This doesn't require component registration, as the component is already registered while creating it. So it looks like a normal plain HTML element.
If you want to build the multiple components file then use the following command instead:
//package.json
"build-wc": "vue-cli-service build --target wc --name demo src/components/*.vue",
0 comments:
New comments are not allowed.