Improve Frontend Code Quality with Tools — WeChat Mini Program

How to configure linting and formatting to improve code quality in WeChat Mini Program projects, with practical setup details.

Nov 21, 2021 · 2 min read · 231 Words · -Views -Comments · Programming
Code Quality 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

I spent the last two weeks developing WeChat Mini Programs. Their code style needs matching tooling, and the setup differs slightly from general web projects. Notes below.

Where Config Files Live

A typical Mini Program project looks like the following, so the root for package.json, ESLint, and other configs is under miniprogram, not the project root.

ESLint

.eslintignore

node_modules
miniprogram_npm
**/*.wxml

Notes

  1. Treat WXML as HTML and exclude it so ESLint rules don’t flag those files.

.eslintrc.js

globals: {
    App: true,
    Page: true,
    Component: true,
    Behavior: true,
    wx: true,
    getApp: true,
    getCurrentPages: true,
    __wxConfig: true,
  }

Declare Mini Program globals so the IDE doesn’t complain with eslint --no-undef.

.prettierrc.js

module.exports = {
  printWidth: 120,
  tabWidth: 2,
  useTabs: false,
  semi: true,
  singleQuote: true,
  quoteProps: 'as-needed',
  trailingComma: 'es5',
  bracketSpacing: true,
  arrowParens: 'always',
  htmlWhitespaceSensitivity: 'css',
  endOfLine: 'lf',
  overrides: [
    {
      files: '*.wxml',
      options: {parser: 'html'},
    },
    {
      files: '*.wxss',
      options: {parser: 'css'},
    },
    {
      files: '*.wxs',
      options: {parser: 'babel'},
    },
  ],
};

Assign different parsers to WXML/WXSS to keep formatting correct.

lint-staged

  "lint-staged": {
    "**/*.{js,wxs}": [
      "eslint --fix"
    ],
    "**/*.{wxml}": [
      "prettier --write"
    ]
  }

Run ESLint for JS/WXS and Prettier for WXML.

Note: ESLint includes the Prettier plugin in this setup, so JS files still go through Prettier—no separate command needed above.

Final Thoughts

Code style is more than a consistent look; it encodes best practices that prevent common mistakes. Treat it seriously.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover