WeChat Mini Program Bundler
weapp-minipack is a JavaScript bundler specifically designed for WeChat mini-programs, with a strong emphasis on TypeScript support. Currently at version 0.1.20, it provides both a command-line interface (CLI) for quick project setup and a programmatic API for more custom build workflows. The tool aims to simplify the development process for mini-programs by handling TypeScript compilation, asset copying, and offering a robust plugin system for extending its functionality, such as WXSS minification. Its release cadence is likely irregular given its early stage of development, but it actively integrates with standard tools like `esbuild` for performance. A key differentiator is its focus on the unique structure and requirements of WeChat mini-programs, providing specific configurations like `miniprogramProjectPath` and direct integration for environment variable injection.
Common errors
-
Error: Cannot find module 'weapp-minipack'
cause The package was not installed or installed incorrectly, or there's a CommonJS/ESM import mismatch.fixEnsure `weapp-minipack` is installed locally (`npm install weapp-minipack` or `yarn add weapp-minipack`) if using programmatic API, or globally (`npm install -g weapp-minipack`) for CLI. Verify `import` vs `require` usage matches your project type. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` in an ECMAScript module context (.mjs file or `"type": "module"` in `package.json`).fixRefactor your imports to use `import { Entry } from 'weapp-minipack';` and `import path from 'path';`. If you need to read JSON or CJS modules, consider dynamic `import()` or `createRequire`. -
Error: configPath is required or config options must be provided directly.
cause When creating a new `Entry` instance, neither a `configPath` pointing to a config file nor a direct `config` object was provided.fixProvide a `configPath` option like `{ configPath: path.resolve(__dirname, 'minipack.config.js') }` or pass the configuration object directly as `{ config: { /* your config here */ } }` to the `Entry` constructor.
Warnings
- gotcha The package is currently in version `0.1.x`. This indicates an early stage of development where the API might not be stable, and breaking changes could occur in minor or even patch releases.
- gotcha Configuration for `weapp-minipack` is primarily done via a separate JavaScript file (e.g., `minipack.config.js`) or passed directly as an object to the `Entry` constructor. It does not integrate with `package.json` for configuration.
- gotcha While the library ships TypeScript types, its examples in the README predominantly use CommonJS `require` syntax. If integrating into a pure ESM project, you might encounter issues unless your build setup correctly handles CJS interop.
Install
-
npm install weapp-minipack -
yarn add weapp-minipack -
pnpm add weapp-minipack
Imports
- Entry
const { Entry } = require('weapp-minipack');import { Entry } from 'weapp-minipack'; - minifierStyle
const { minifierStyle } = require('weapp_minipack');import { minifierStyle } from 'weapp-minipack'; - CLI usage
weapp-minipack -c ./minipack.config.js
Quickstart
import { Entry } from 'weapp-minipack';
import path from 'path';
// Define your minipack configuration
const minipackConfig = {
watchEntry: path.resolve(__dirname, 'src'), // Source directory for mini-program files
tsConfigPath: path.resolve(__dirname, 'tsconfig.json'), // Path to your TypeScript config
outDir: path.resolve(__dirname, 'build'), // Output directory for compiled files
isWatch: false, // Set to true to enable watch mode for development
miniprogramProjectPath: path.resolve(__dirname, 'project.config.json'),
plugins: [
{
test: /.*\.(wxss)$/,
action: ({ data }) => {
// Example: simple CSS minification or just passing through
return data.replace(/\s+/g, ' ').trim();
},
},
],
};
// Create an instance of Entry with your configuration
// For a production build, ensure isWatch is false.
const bundler = new Entry({
config: minipackConfig
});
// Initialize and start the bundling process
bundler.init().start()
.then(() => console.log('Mini-program build successful!'))
.catch(error => console.error('Mini-program build failed:', error));