Release Custom Component

Mini program natively supports the introduction of third-party npm module, so the customized component also supports publishing to npm for developers to reuse and share.

Customized Component Directory Recommended for Publishing

The following directory structure is for reference only.

File Structure

copy
├── src // used for individually customized component
│   ├── index.js
│   ├── index.json
│   ├── index.axml
│   └── index.acss
├── ├── demo //used for demo of customized component
│   ├── ├── index.js
│   ├── ├── index.json
│   ├── ├── index.axml
│   ├── └── index.acss
├── app.js // used for demo of customized component Mini Program
├── app.json
└── app.acss

JSON Sample

copy
// package.json
{
  "name": "your-custom-component",
  "version": "1.0.0",
  "description": "your-custom-component",
  "repository": {
    "type": "git",
    "url": "your-custom-component-repository-url"
  },
  "files": [
    "es"
  ],
  "keywords": [
    "custom-component",
    "mini-program"
  ],
  "devDependencies": {
    "rc-tools": "6.x"
  },
  "scripts": {
    "build": "rc-tools run compile && node scripts/cp.js && node scripts/rm.js",
    "pub": "git push origin && npm run build && npm publish"
  }
}

js File Sample

copy
// scripts/cp.js
const fs = require('fs-extra');
const path = require('path');
// copy file
fs.copySync(path.join(__dirname, '../src'), path.join(__dirname, '../es'), {
  filter(src, des){
    return !src.endsWith('.js');
  }
});
copy
// scripts/rm.js
const fs = require('fs-extra');
const path = require('path');
// remove unnecessary file
const dirs = fs.readdirSync(path.join(__dirname, '../es'));
dirs.forEach((item) => {
  if (item.includes('app.') || item.includes('DS_Store') || item.includes('demo')) {
    fs.removeSync(path.join(__dirname, '../es/', item));
  } else {
    const moduleDirs = fs.readdirSync(path.join(__dirname, '../es/', item));
    moduleDirs.forEach((item2) => {
      if (item2.includes('demo')) {
        fs.removeSync(path.join(__dirname, '../es/', item, item2));
      }
    });
  }
});
fs.removeSync(path.join(__dirname, '../lib/'));