As we work along with the javascript framework (vue / react) we will realize that there are custom components in our application that you might need to re-use in different applications or sometimes we have to share with the community.
Let’s see how to publish our first component into npm and use it in our project.
In my previous post, I explain how to create a vuejs plugin and use it in our project. In this post I am going to use the same project to publish in npm
In the last blog, I explain how to create and use the library/plugin. Lets recap important things to do before publishing into npm
After we build library/plugin files into dist folder, we have to update our package.json
Let us add the main entry point in packge.json file
"main": "./dist/lib.umd.min.js",
Now let’s whitelist what are the files needed when someone is using them inside their node_modules. Here we need to include only dist folder from our project.
"files": [ "dist/*"],
Pushing compiled source code is the right way to publish in NPM. Users usually won’t edit this code. if they want to make changes, they can fork from version control systems (like Github). In case you want to include all files, we add like below
"files": [
"dist/*",
"src/*",
"folder/*",
],
Let’s add additional details like author details and license also into the package.json
version: the version number of our plugin/library. You will have to increment this number every time you publish an update for your package. usually we follow semantic versioning
private : This means your package is public (i.e. everyone is able to see and install it).
license: license for our package
author: author details
main: The primary entry point to our plugin.
files: specifies which files should be published on NPM.
When Vue 3 was released one of the most exciting features are CompositionAPI. An alternative to Options API. In this article, let’s take a look at how we can use CompositionAPIin our project
Now let’s start by installing Vue CLI globally. If you already have the CLI, make sure your running at least (I am using Vue CLI v5.0.4)
npm install -g vue-cli
We’ll use the Vue CLI to build a simple application( I am using vue3-composition-todo as project name here )
vue create vue3-composition-todo
Here I am selecting default Vue 3
After installation move into the folder and as shown above cd vue3-composition-todo and start-server npm run serve . This will start a development server and we can see it on http://localhost:8080/
Install plugins
Let us install Bootstrap for styles in our project
npm install bootstrap --save
lets import in bootstrap CSS in App.vue (We are using only Bootstrap CSS)
import 'bootstrap/dist/css/bootstrap.css'
Final code in main.js looks like below
import { createApp } from 'vue'
import App from './App.vue'
import 'bootstrap/dist/css/bootstrap.css'
createApp(App).mount('#app')
Create UI for TODO
First let’s create a file inside the component TodoList.vue
Inside template, we can do small HTML to show todo list
We have a save function now, after typing on click of saving we call addToDo and push data into our todos object. and the table will re-render and show a new to-do items.
The same way we can add more functionality like removeItem (delete a todo item on click of delete button), toggleCompleted (toggle as completed and in progress) and addTodoForme (adding sample data )
import { reactive } from "vue";
function useTodoList() {
let state = reactive({
newTodo: "",
todos: [
{
id: 1,
title: "Buy groceries for next week",
completed: false,
},
{
id: 2,
title: "Pay credit card bill ",
completed: false,
},
],
});
function addToDo(e) {
e.preventDefault();
state.todos.push({
id: state.todos.length + 1,
title: state.newTodo,
completed: false,
});
state.newTodo = "";
}
function toggleCompleted(item) {
item.completed = !item.completed;
}
function removeItem(item) {
state.todos = state.todos.filter((newItem) => newItem.id !== item.id);
}
function addTodoForme(e) {
e.preventDefault();
const text = `New to do list item with id ${state.todos.length + 1}`;
state.todos.push({
id: state.todos.length + 1,
title: text,
completed: false,
});
}
return {
state,
addToDo,
toggleCompleted,
removeItem,
addTodoForme,
};
}
export default useTodoList;
And final output
Conclusion
Finally, we’ve built our app with Vue 3 Composition API. I hope you learned a few things about Vue 3 and Composition API, Let me know if you have any comments
You can download the full source code from my GitHub repo