vue-component-toolkit
Version:
A Vue3 component toolkit library
194 lines (146 loc) • 7.07 kB
Plain Text
https://blog.ayitinya.me/articles/how-to-create-and-publish-vue-component-to-npm
https://www.telerik.com/blogs/vuejs-how-to-build-your-first-package-publish-it-on-npm
build lib
https://folio3.com/blog/how-to-publish-a-private-vue-3-components-library-to-npm-packages/
Here's an improved and completed version of your tutorial, fixing errors and adding more context and missing steps:
Step-by-Step Guide to Publish a Private Vue 3 Components Library to NPM Packages
Step 1: Create a Vue 3 Project Using Vite
Start by creating a Vue 3 project using Vite. Run the following command in your terminal:
bash
Copy code
npm create vite@latest
When prompted, select Vue for the framework and TypeScript for the language. After the project is created, navigate into the project folder and install the dependencies:
bash
Copy code
cd <your-project-name>
npm install
Install the necessary package for building your application with TypeScript support and configuring Rollup for bundling the components:
bash
Copy code
npm install rollup-plugin-typescript2
Now, run the development server to ensure everything is working:
bash
Copy code
npm run dev
This will start the server at http://localhost:5173/, where you can see your Vue 3 project in action.
Step 2: Create a Simple Component
Next, let's create a simple Vue component that we want to include in the library. In your src/components directory, create a file called MyButton.vue with the following content:
vue
Copy code
<template>
<button class="my-button">My Awesome Button</button>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'MyButton',
});
</script>
<style scoped>
.my-button {
border: 2px solid red;
border-radius: 2px;
padding: 10px;
font-size: 16px;
}
</style>
This defines a simple button component that you can later export in your library.
Step 3: Set Up an Entry Point for Your Library
Create an entry file for your library that will export the components and allow them to be used globally in Vue projects.
In the src directory, create a file called index.ts with the following content:
typescript
Copy code
import { App } from 'vue';
import MyButton from './components/MyButton.vue';
export default {
install: (app: App) => {
app.component('MyButton', MyButton);
},
};
export { MyButton };
This file serves two purposes:
Plugin installation: It exports an object with an install function that registers the MyButton component globally,
making it available in any Vue 3 app that uses this library.
Direct component export: It also exports MyButton so you can import it directly when needed in a project.
Step 4: Set Up Rollup for Bundling Your Library
To package your components for distribution, you'll need to configure Rollup. Create a rollup.config.js file in the root directory of your project
with the following configuration:
javascript
Copy code
import typescript from 'rollup-plugin-typescript2';
import vue from 'rollup-plugin-vue';
import path from 'path';
export default {
input: path.resolve(__dirname, 'src/index.ts'),
output: [
{
file: 'dist/my-library.cjs.js',
format: 'cjs',
exports: 'named',
},
{
file: 'dist/my-library.esm.js',
format: 'esm',
},
{
file: 'dist/my-library.umd.js',
format: 'umd',
name: 'MyLibrary',
},
],
plugins: [
vue(),
typescript(),
],
external: ['vue'], // Ensure Vue is not bundled inside the library
};
Explanation of the configuration:
Input: The entry file src/index.ts.
Outputs: Three different formats are generated for compatibility with various bundlers and projects:
CommonJS (cjs) for Node.js environments.
ES Module (esm) for modern JavaScript bundlers like Webpack and Rollup.
UMD format (umd) for both browser and Node.js environments.
Plugins: The configuration uses the rollup-plugin-vue to process .vue files and rollup-plugin-typescript2 to handle TypeScript.
External: Vue is marked as an external dependency because it should not be bundled with the library.
Step 5: Build Your Library
Now that your Rollup configuration is set up, run the following command to build your library:
bash
Copy code
npm run build
This will generate the dist folder containing your library in the specified formats (.cjs.js, .esm.js, and .umd.js).
Step 6: Publishing Your Library to NPM (Private)
To publish your library to a private NPM registry, follow these steps:
Login to NPM: If you haven't logged in yet, use the following command to log in to your NPM account:
bash
Copy code
npm login
If you want to publish to a private registry, set up the registry URL in your .npmrc file or use the --registry flag when publishing.
Publish to NPM: Publish your library by running the following command:
bash
Copy code
npm publish --access=restricted
The --access=restricted flag ensures that the package is published as private, meaning only users or teams with access to the package can install it.
Step 7: Using Your Library in Other Projects
Once the package is published, you can use it in other Vue projects. In your main.ts file, you can import and use the library like this:
typescript
Copy code
import { createApp } from 'vue';
import App from './App.vue';
import MyLibrary from 'my-library'; // Replace with your actual package name
const app = createApp(App);
app.use(MyLibrary);
app.mount('#app');
This will globally register MyButton, and you can use it in any template without needing to import it manually.
Alternatively, you can import only the component you need directly:
typescript
Copy code
import { createApp } from 'vue';
import App from './App.vue';
import { MyButton } from 'my-library'; // Direct import
const app = createApp(App);
app.component('MyButton', MyButton);
app.mount('#app');
Final Thoughts
This tutorial covers the basic steps to create, build, and publish a private Vue 3 components library to NPM.
You'll want to extend it by adding more components, handling versioning, and ensuring proper testing.
You can also explore advanced Rollup configurations to optimize the build for performance and size.