1 Star 0 Fork 0

dafengzhen/vite-plugin-mp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

使用 Vite 编译微信小程序

当你想使用原生微信小程序开发,不想使用其他框架进行开发,只想简单的使用 Vite 进行编译打包,那么这就是该插件用途

注意使用版本,该插件测试的为微信小程序基于 Skyline 渲染引擎的 Ts + Scss 模板,以及 Vite 5

安装依赖

npm install -D glob rollup-plugin-copy @dafengzhen/vite-plugin-mp

使用插件

  • 编辑 vite.config.ts 文件
import path from "path";
import VitePluginMp from '@dafengzhen/vite-plugin-mp';
import {defineConfig} from 'vite';

export default defineConfig({
    plugins: [
        VitePluginMp({
            buildDir: path.resolve(__dirname, 'miniprogram'),
            outputDir: path.resolve(__dirname, 'dist')
        })
    ]
})
  • 构建编译小程序
npm run build
  • 输入输出示例

这里省略多余的文件,只展示关键信息

输入的代码结构:

├── ...
├── miniprogram
│   ├── apis
│   │   └── test
│   │       └── index.ts
│   ├── app.json
│   ├── app.scss
│   ├── app.ts
│   ├── assets
│   │   ├── images
│   │   │   ├── test.png
│   ├── components
│   │   └── navigation-bar
│   │       ├── navigation-bar.json
│   │       ├── navigation-bar.scss
│   │       ├── navigation-bar.ts
│   │       └── navigation-bar.wxml
│   ├── constants
│   │   └── index.ts
│   ├── interfaces
│   │   └── index.ts
│   ├── pages
│   │   ├── index
│   │   │   ├── index.json
│   │   │   ├── index.scss
│   │   │   ├── index.ts
│   │   │   └── index.wxml
│   ├── sitemap.json
│   └── tools
│       ├── index.ts
│       ├── request.ts
│       └── upload-request.ts
├── project.config.json
├── project.private.config.json
└── ...

输出的代码结构:

├── ...
├── dist
│   ├── miniprogram
│   │   ├── apis
│   │   │   └── test
│   │   │       └── index.js
│   │   ├── app.js
│   │   ├── app.json
│   │   ├── app.wxss
│   │   ├── assets
│   │   │   ├── images
│   │   │   │   ├── test.png
│   │   ├── components
│   │   │   └── navigation-bar
│   │   │       ├── navigation-bar.js
│   │   │       ├── navigation-bar.json
│   │   │       ├── navigation-bar.wxml
│   │   │       └── navigation-bar.wxss
│   │   ├── constants
│   │   │   └── index.js
│   │   ├── pages
│   │   │   ├── index
│   │   │   │   ├── index.js
│   │   │   │   ├── index.json
│   │   │   │   ├── index.wxml
│   │   │   │   └── index.wxss
│   │   ├── sitemap.json
│   │   └── tools
│   │       ├── index.js
│   │       ├── request.js
│   │       └── upload-request.js
│   ├── project.config.json
│   └── project.private.config.json
└── ...

插件选项

  • buildDir 必填。编译小程序的代码目录
  • outputDir 必填。输出编译小程序的代码目录
  • processedDir 可选。要处理的代码文件夹名称,默认为 ['apis', 'components', 'constants', 'pages', 'tools']
  • processedAssetDir 可选。要处理的资源文件夹名称,默认为 ['assets']

其他相关

  • 当不要想使用该插件,如何将原来代码转换为使用 Uni-App / Taro 等框架开发

该插件使用的对象为微信原生小程序,意味着如果需要转化为其他框架,可以从 "微信原生小程序如何转换 XXX 框架" 为出发点,这是一种方式

另一种方式是使用框架本身提供的转换工具,尝试转换微信小程序,例如 Uni-App 转换工具指南

  • 当使用该插件遇到无法编译,或者发生未知错误,想改为原来的微信小程序需要如何做

首先要做的是移除该插件,因为该插件本质上只是使用 Vite 来对文件进行编译而已

其次,即使不使用 Vite,微信开发者工具也能进行编译,除此之外没有太大区别

移除该插件后,剩下的就是熟悉的微信小程序代码

License

MIT

MIT License Copyright (c) 2023 dafengzhen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

使用 Vite 编译微信小程序 展开 收起
TypeScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/dafengzhen/vite-plugin-mp.git
git@gitee.com:dafengzhen/vite-plugin-mp.git
dafengzhen
vite-plugin-mp
vite-plugin-mp
main

搜索帮助

Cb406eda 1850385 E526c682 1850385