MiniProgram Build Tool
miniprogram-build is a command-line interface (CLI) tool designed to streamline the development workflow for WeChat MiniPrograms. It handles common build tasks such as compiling TypeScript to JavaScript, SCSS/Sass/CSS to MiniProgram's WXSS, HTML/WXML to WXML, and JSON/JSONC to JSON, alongside image compression and file copying. The current stable version is 7.0.3, with frequent patch releases indicating active maintenance. Unlike a framework, it serves as an automation and enhancement tool for existing MiniProgram projects, emphasizing a build-and-watch approach. Its key differentiators include comprehensive support for modern web technologies (TypeScript, SCSS) within the MiniProgram ecosystem and direct integration with the WeChat Developer Tools for features like automatic preview and project upload.
Common errors
-
Error: Command 'miniprogram-build' not found
cause The `miniprogram-build` command is not accessible in the current shell's PATH. This usually happens if it's installed locally but not run via `npx` or an npm script.fixIf installed locally as a dev dependency, run commands using `npx miniprogram-build [command]` or define a script in `package.json` like `"dev": "miniprogram-build dev"` and then `npm run dev`. -
Error: Failed to connect to WeChat DevTools. Please ensure DevTools is running and port settings are enabled.
cause The WeChat Developer Tools are either not running, or the 'Service Port' feature (required for CLI communication) is not enabled in its settings.fixOpen your WeChat Developer Tools, navigate to 'Settings' -> 'Security Settings', and enable 'Service Port' (服务端口). Ensure the DevTools application remains open while using `autopreview` or `upload` commands. -
Error: Cannot find tsconfig.json in the current directory or its parents.
cause The project uses TypeScript, but `miniprogram-build` cannot locate the `tsconfig.json` file to configure its TypeScript compilation.fixCreate a `tsconfig.json` file in your project's root or specify its path using the `--tsconfig` option, e.g., `miniprogram-build build --tsconfig ./config/tsconfig.json`.
Warnings
- gotcha The automatic preview and upload functionalities require the WeChat Developer Tools to be installed and 'Port Settings' enabled within the IDE. Without this, `autopreview` and `upload` commands will fail.
- gotcha The `--production` flag (or `NODE_ENV=production`) significantly alters the build output, often applying optimizations and compressions. Ensure you test your MiniProgram thoroughly in production mode before deployment, as subtle differences might arise.
- breaking Upgrading to a new major version (e.g., from v6.x to v7.x) may introduce breaking changes to configuration options, CLI flags, or internal processing logic. Always consult the official Changelog (if available) or GitHub releases for specific migration steps.
- gotcha When defining custom variables using `--var.KEY=value`, ensure that the variables are correctly referenced in your source files (e.g., `{{__APP_ID__}}` in JavaScript/JSON). Incorrect syntax or missing definitions will lead to unresolved variables in the output.
Install
-
npm install miniprogram-build -
yarn add miniprogram-build -
pnpm add miniprogram-build
Imports
- miniprogram-build
const cli = require('miniprogram-build'); cli(process.argv.slice(2));import cli from 'miniprogram-build'; cli(process.argv.slice(2));
- build
import { Build } from 'miniprogram-build';import { build } from 'miniprogram-build'; build({ src: 'src', dist: 'dist', production: true }); - dev
import { Dev } from 'miniprogram-build';import { dev } from 'miniprogram-build'; dev({ src: 'src', dist: 'dist' });
Quickstart
{
"name": "my-miniprogram-project",
"version": "1.0.0",
"description": "A simple WeChat MiniProgram project",
"scripts": {
"dev": "miniprogram-build dev",
"build": "miniprogram-build build --production",
"init": "miniprogram-build init"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"miniprogram-build": "^7.0.0",
"typescript": "^5.0.0",
"tslib": "^2.0.0"
}
}
// To get started, first install as a dev dependency:
npm install miniprogram-build typescript tslib -D
// Then, create a default configuration file:
npx miniprogram-build init
// Or view all available commands:
npx miniprogram-build -h
// To run in development mode (build and watch):
npm run dev
// To build for production:
npm run build