prettier-plugin-mp
Version:
Prettier plugin for WeChat Mini Program WXML files
153 lines (116 loc) • 4.19 kB
Markdown
for formatting WeChat Mini Program WXML and WXS files with professional code quality.
✅ **WXML 格式化**: 为 WXML 文件提供正确的缩进和结构
✅ **WXS JavaScript 格式化**: 为嵌入的 WXS 模块提供完整的 JavaScript 格式化
✅ **可配置选项**: 为 WXML 和 WXS 提供独立的格式化选项
✅ **微信语法支持**: 完整支持 `wx:for`、`wx:if` 和其他微信指令
✅ **专业品质**: 基于官方 Prettier XML 插件架构
```bash
pnpm install -D prettier-plugin-mp
npm install --save-dev prettier-plugin-mp
```
```bash
prettier --plugin=prettier-plugin-mp --write "**/*.wxml"
prettier --plugin=prettier-plugin-mp --write src/pages/index.wxml
```
Create a `.prettierrc` file:
```json
{
"plugins": ["prettier-plugin-mp"],
"overrides": [
{
"files": "*.wxml",
"options": {
"parser": "wxml"
}
}
]
}
```
你可以通过在 `.prettierrc` 文件中添加这些选项来自定义格式化行为:
You can customize the formatting behavior by adding these options to your `.prettierrc` file:
| 选项 Option | 类型 Type | 默认值 Default | 描述 Description |
|--------|------|---------|-------------|
| `wxmlTabWidth` | `int` | `2` | WXML 缩进空格数 Number of spaces per indentation level for WXML |
| `wxmlPrintWidth` | `int` | `80` | WXML 换行长度 Line length where Prettier will try to wrap for WXML |
| `wxmlSingleQuote` | `boolean` | `false` | WXML 属性使用单引号 Use single quotes in WXML attributes |
| 选项 Option | 类型 Type | 默认值 Default | 描述 Description |
|--------|------|---------|-------------|
| `wxsTabWidth` | `int` | `2` | WXS 代码缩进空格数 Number of spaces per indentation level for WXS code |
| `wxsPrintWidth` | `int` | `80` | WXS 代码换行长度 Line length where Prettier will try to wrap for WXS code |
| `wxsSingleQuote` | `boolean` | `true` | WXS 代码使用单引号 Use single quotes in WXS code |
| `wxsSemi` | `boolean` | `true` | WXS 语句末尾添加分号 Print semicolons at the ends of statements in WXS code |
```json
{
"plugins": ["prettier-plugin-mp"],
"overrides": [
{
"files": "*.wxml",
"options": {
"parser": "wxml",
"wxmlTabWidth": 2,
"wxmlPrintWidth": 100,
"wxmlSingleQuote": false,
"wxsTabWidth": 2,
"wxsPrintWidth": 80,
"wxsSingleQuote": true,
"wxsSemi": true
}
}
]
}
```
```xml
<view><text>Hello</text><wxs module="test">var a=1;function test(){return a;}</wxs></view>
```
```xml
<view>
<text>Hello</text>
<wxs module="test">
var a = 1;
function test() {
return a;
}
</wxs>
</view>
```
- **WXML 元素**: 所有标准 WXML 标签和属性 All standard WXML tags and attributes
- **微信指令**: `wx:for`, `wx:if`, `wx:elif`, `wx:else`, `wx:key` 等 WeChat Directives
- **数据绑定**: `{{ }}` 表达式 Data Binding expressions
- **WXS 模块**: `<wxs>` 标签内的完整 JavaScript 语法 Complete JavaScript syntax within `<wxs>` tags
- **事件处理**: `bind:tap`, `catch:tap` 等 Event Handlers
- **自闭合标签**: `<image />`, `<input />` 等的正确格式化 Proper formatting for self-closing tags
```bash
git clone https://github.com/your-username/prettier-plugin-mp.git
cd prettier-plugin-mp
pnpm install
npm install
npm test
npm run format:wxml
```
MIT
微信小程序 WXML 和 WXS 文件的 Prettier 格式化插件。
A Prettier plugin