🔐 reusable/
┣ 📂 vue3/
┃ ┣ 💼 node_modules/
┃ ┣ 📦 vue-sfc-vite/
┃ ┣ 📦 vue3-base-input/
┃ ┣ 📦 vue3-base-x/
┃ ┣ …
┃ ┣ 📜 .gitignore
┃ ┣ 📜 lerna.json
┃ ┣ 📜 LICENSE.md
┃ ┣ 📜 package-lock.json
┃ ┗ 📜 package.json
┣ 📂 vue2/
┃ ┣ ...
┃ ...
- Let’s take
afzalsabbir
as our GitHub username - Create a private GitHub repository (i.e.:
reusable
) - Clone the repository to your local machine
- Install
lerna
globally (npm install -g lerna
) - Follow #setup-package-for-the-first-time.2.0 to init lerna in the
reusable/<packages_directory>
directory
[Note: Later on we've usedvue3/
as packages_directory] - Follow #setup-package-for-the-first-time.2.i to config
lerna.json
- Install
sfc
globally (npm install -g vue-sfc-rollup
) - Follow #create-package.2 to create a single file components in the
reusable/<packages_directory>
directory
[Note: Later on we've usedvue3/
as packages_directory]
- Create a folder inside the repository root (reusable) (i.e.
vue3
as we're going to create some packages for VueJS v3.x) - Init lerna latest by running:
cd vue3 && npx lerna@latest init
- Edit
lerna.json
to add the following:{ "packages": ["./*"], "version": "independent" }
- Create LICENSE.md
- Follow #create-package to create a new package
- Edit
- Run
sfc-init
to create a new single file component with options:- Which version of Vue are you writing for?
»Vue 3
- Is this a single component or a library?
»Single component
- What is the npm name of your component?
»base-button
- What is the kebab-case tag name for your component?
»base-button
- Will this component be written in JavaScript or TypeScript?
»TypeScript
- Enter a location to save the component files:
»./vue-sfc-vite
[Note: Remember to remove if/vue-sfc-vite
exists in the directory:reusable/vue3/
before pressing Enter]
- Which version of Vue are you writing for?
- Run
cd vue-sfc-vite && npm i
- Follow #config-package to config and test the package
- Follow #publish to publish the package
- Follow #basic to clone the repository to your local machine
- Run
cd vue-sfc-vite
- Follow #config-package.3 to make any changes
- Update the README.md file as shown in README.md
- Run
npm run serve
to check if your package is working correctly or not in the browser - Follow #publish to publish a new version of the component after changes
- If facing any jsx issue then edit the
tsconfig.json
and add a property jsx in compilerOptions and set value preserve.[Note: Restarting the editor should resolve any underline errors]{ "compilerOptions": { //... "jsx": "preserve" //... } }
- Set in package.json:
{ "name": "@afzalsabbir/vue-sfc-vite", "version": "0.0.0", "description": "Some description", "author": "arbs23 <[email protected]>", "homepage": "https://github.com/afzalsabbir/reusable", "license": "ISC", "repository": { "type": "git", "url": "git+https://github.com/afzalsabbir/reusable.git" }, "bugs": { "url": "https://github.com/afzalsabbir/reusable/issues" }, "dependencies": { "bootstrap": "5.1.3" }, "peerDependencies": { //... "bootstrap": "5.1.3" }, //... }
[Note: Add all dependencies in the package.json’s dependencies as well as in peerDependencies. Don’t forget to removepackage-lock.json
and to runnpm install
if any new dependency added or any changes inpackage.json
while testing] - Write/Paste your code here:
- Let's
vue-sfc-vite
is the package you
have created in the previous step
or
want to update in the next step - Remove
package-lock.json
&node_modules
and then runnpm install
- In
reusable\vue3\vue-sfc-vite\src\vue-sfc-vite.vue
put your single file component code (package code):- Code in package template
<template>...</template>
:<template> <button :class="'btn ' + buttonClass" :disabled="isDisabled" @click="$emit('click')" > <!-- @slot default button slot --> <slot>{{ buttonText }}</slot> </button> </template>
- Code in package script
<script lang="ts">...</script>
:import { defineComponent } from "vue"; export default defineComponent({ name: "BaseButton", props: { /** * @property {string} buttonClass - CSS class name for button. * @default btn-primary */ buttonClass: { type: String, default: "btn-primary", }, /** * @property {string} buttonText - Button text. * @default null */ buttonText: { type: String, default: "", }, /** * @property {boolean} isDisabled - Button enabled/disabled. * @default false */ isDisabled: { type: Boolean, default: false }, }, emits: ["click"], });
- Code in package style
<style scoped>...</style>
:/*Write your style here...*/ .btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
- Code in package template
- In
reusable\vue3\vue-sfc-vite\dev\
put your test code- Add your stylesheets or any other imports in the
serve.ts
fileimport { createApp } from 'vue'; import Dev from './serve.vue'; //... import "bootstrap/dist/css/bootstrap.min.css"; //... const app = createApp(Dev); app.mount('#app');
- Add your component code in the
serve.vue
file- Changes in component template
<template>...</template>
:<div id="app"> <BaseButton button-class="btn-primary" button-text="Wow button!" @click="handleClick" /> <BaseButton button-class="btn-warning w-25" button-text="Wow button!" @click="handleClick" > <template v-slot:default> <div class="d-flex justify-content-center align-items-center"> <img class="img-fluid w-25" src="./assets/sample.svg"/> <p class="m-0 ms-2">Sample</p> </div> </template> </BaseButton> </div>
- Changes in component script
<script lang="ts">...</script>
:import { defineComponent } from 'vue'; import BaseButton from '@/base-button.vue'; export default defineComponent({ name: 'ServeDev', components: { BaseButton }, setup() { const handleClick = () => { alert('Button clicked!'); } return { handleClick } } });
- Changes in component style
<style scoped>...</style>
:/*Write your style here...*/ .btn-primary { color: #fff; background-color: #007bff; border-color: #007bff; }
- Any devDependencies specially for
serve.vue
must include invue3/package.json
devDependencies
i.e.:Here<template> <!-- ... --> <base-tree-select v-model="selectedNodes" style="width: 250px" selection-mode="checkbox" display="chip" placeholder="Select Department/Garage.." :options="companyDepartmentSetting" > <template v-slot:footer> <div class="d-flex justify-content-end mx-2"> <base-button button-class="btn-outline-primary btn-sm my-2" @click="selectedNodes = {}" > Clear All </base-button> </div> </template> </base-tree-select> <!-- ... --> </template>
<base-button/>
is a component used here for@afzalsabbir/vue3-base-tree-select
from@afzalsabbir/vue-sfc-vite
. As it'll be in test example only so it's a devDependencies in@afzalsabbir/vue3-base-tree-select/package.json
and we have to put it invue3/package.json
devDependencies as well
- Changes in component template
- Add your stylesheets or any other imports in the
- Let's
- Create/Update the README.md file as shown in README.md
- Run
npm run serve
to check if your package is working correctly or not in the browser
- Run
lerna changed
to see the changes available to publish. Before run this command, make sure that thegit status
is clean - We can run
lerna clean -y
to remove any existingnode_modules
from all packages - Run
lerna bootstrap --hoist
will grab dependencies from all packages and install them all inreusable/vue3/node_modules
- Go to every package (i.e.:
/vue-sfc-vite
) directory and runnpm run build
before publishing any package - Run
npm login --registry=https://npm.pkg.github.com
to login before publishing
[Note: Make sure you have created a git access token with scopes:write:packages,repo,delete:packages
or click here to create a new token] - Run
npm whoami
if you are not there then runnpm adduser --registry=https://registry.npmjs.org/:_authToken=<token-from-npm>
to add yourself as a publisher - Run
lerna publish --registry=https://npm.pkg.github.com
to publish
[Note: push any local change to remotegit
]- Select version to publish
- Type
y
to confirm
# package_name (vue-sfc-vite)
Package description
## Installation
Installation command:
```bash
npm install @afzalsabbir/vue-sfc-vite
```
## Dependencies
List of dependencies:
| Dependencies | Description |
| :---------------------- | :--------------- |
| `"bootstrap": "^5.1.3"` | Some description |
| ... | ... |
## Properties
Following properties can be passed to this component:
| Name | Type | Description | Required |
| :------------ | :------- | :--------------------- | :------- |
| `buttonClass` | `string` | Style class for button | No |
| ... | ... | ... | ... |
| ... | ... | ... | ... |
## Events
Following events can be used to this component:
| Name | Behaviour |
| :------ | :---------------------------------------- |
| `click` | Triggered if a user clicks on the button. |
| ... | ... |
| ... | ... |
## Usage/Examples
Write where to use and how to use...
[Note: Paste serve.vue file from reusable/vue3/vue-sfc-vite/dev/ directory]
## Screenshots
If there is any screenshots available, paste them here...
- Open terminal where you want to install the package in
- Make sure
.npmrc
has the following content://npm.pkg.github.com/:_authToken=<your-token> @afzalsabbir:registry=https://npm.pkg.github.com
- Log in to npm with
npm login --registry=https://npm.pkg.github.com
- To install a package you need to run the following command:
[Note: Make sure you have created a git access token with scopes:
npm install @afzalsabbir/vue-sfc-vite
write:packages,repo,delete:packages
. You can make change/update.npmrc
file by yourself, where npm login details are stored in]
- Import the package in your project
import BaseButton from '@afzalsabbir/vue-sfc-vite';
- Register the component globally in
main.ts
file//... const app = createApp(App); app.component("BaseButton", BaseButton); //...
- Use the package in your project
[Note: Make sure you have imported the package in your project]
<template> <!-- ... --> <BaseButton button-class="btn-primary" button-text="Wow button!" @click="handleClick" /> <!-- ... --> </template>