# 全局安装
npm i pnpm -g
# 查看源
pnpm config get registry
# 切换至淘宝源
pnpm config set registry https://registry.npmmirror.com/
# 使用
pnpm install 包名
# 默认写入dependencies
pnpm add 包名
# -D 写入devDependencies
pnpm add -D
# -g 全局安装
pnpm add -g
mkdir LEA-UI-VITE
cd LEA-UI-VITE
pnpm init
pnpm add -D vite
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no" />
<title>组件库开发</title>
<link rel="shortcut icon" href="./favicon.ico">
</head>
<body>
<div id="app">主页</div>
</body>
</html>
npx vite
src【目录】>main.ts【文件】 在index.html中引入main.ts,通过module
<body>
<div id="app">主页</div>
<script type="module" src="./src/main.ts"></script>
</body>
"scripts":{
"dev":"vite"
}
pnpm i vue
import { defineComponent,h} from 'vue'
export default defineComponent({
name:'LeaButton',
render(){
return h('button',null,'LeaButton')
}
})
import { createApp } from "vue";
import LeaButton from './components/button';
createApp(LeaButton).mount('#app')
npm run dev
# 安装插件[当前版本:3.1.2]
pnpm i @vitejs/plugin-vue -D
# 在vite.config.ts文件填写
import vitejsPluginVue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [
vitejsPluginVue()
],
})
- index.vue代码
<script>
import {defineComponent} from "vue";
export default defineComponent({
name:'LeaIcon',
})
</script>
<template>
<span>icon</span>
</template>
<style>
</style>
import { createApp } from "vue";
//该语句报错,找不到该模块【解决方法3.3.1】
import LeaIcon from './components/icon/index.vue';
createApp(LeaIcon).mount('#app')
# 在[src]目录下,新建shims-vue.d.ts声明文件【声明文件以.d.ts】,代码如下
declare module "*.vue"{
import { DefineComponent } from "vue";
const component:DefineComponent<{},{},any>;
export default component;
}
# 安装[当前版本:2.0.1]
pnpm i @vitejs/plugin-vue-jsx -D
# 配置在vite.config.js文件
import viteJsPluginVueJsx from "@vitejs/plugin-vue-jsx"
export default defineConfig({
plugins:[
viteJsPluginVueJsx()
]
})
# 在components目录下,新建组件 container目录>index.jsx【想要支持ts:则为index.tsx】;代码如下:
import {defineComponent} from "vue";
export default defineComponent({
name:'LeaContainer',
render(){
return <section>container</section>
}
})
# 如果是index.tsx,jsx语法会报错,此时配置tsconfig.json文件即可
{
"compilerOptions": {
"declaration": true, /* 生成相关的 '.d.ts' 文件。 */
"declarationDir": "./dist/types", /* '.d.ts' 文件输出目录 */
"jsx": "preserve",
},
"include": [
"./**/*.*",
"./shims-vue.d.ts",
],
"exclude": [
"node_modules"
],
"esModuleInterop": true,
"allowSyntheticDefaultImports": "true"
}
# 在main.ts上引入组件
import { createApp } from "vue";
import LeaContainer from './components/container/index'
createApp(LeaContainer).mount('#app')