How ustbhuangyi/better-scroll works
Project type and purpose
• This is an open-source, monorepo JavaScript library (“better-scroll”) for mobile/PC scroll handling.
• It provides a core engine (@better-scroll/core) plus a rich plugin ecosystem (pull-up, pull-down, indicators, infinity, mouse-wheel, zoom, slide, nested-scroll, etc.).
• It also contains example integrations (Vue and React), documentation (VuePress), test suites (unit and E2E), build & release scripts, and CI/CD configurations.
File structure
• Top-level “lerna.json”, “package.json”, configs (.eslintrc.js, tsconfig.json, travis.yml, etc.), “scripts/” folder → monorepo management, build/test/release automation.
• “packages/” → each subfolder under packages is an independent npm package:
• core – core scrolling engine
• better-scroll – umbrella package bundling core + plugins
• indicators, infinity, mouse-wheel, movable, nested-scroll, observe-dom, observe-image, pull-down, pull-up, scroll-bar, slide, wheel, zoom, shared-utils → plugins or shared utilities
• examples (vue, react) → integration demos
• vuepress-docs → documentation site
• “tests/” and “jest.config.js” + “jest-e2e.config.js” → unit & end-to-end tests
• “scripts/” → build, release, check tooling
• CI/CD: .travis.yml, Gitpod config
README for additional insights
• Describes the core API, plugin mechanism, CDN usage, Vue/React integration.
• Mentions a plugin architecture: core is plugin-agnostic; features are enabled via plugin registration.
a. Components
• Monorepo Manager (Lerna + npm)
• Build & Release Scripts (scripts/, CI configs)
• Core Engine (@better-scroll/core)
• Plugin Modules (indicators, pull-up, pull-down, infinite, etc.)
• Umbrella Package (better-scroll): bundles core + selected plugins
• Examples (Vue-examples, React-examples)
• Documentation Site (VuePress)
• Test Suite (Unit tests, E2E tests)
• External Services: GitHub (repo), npm registry, TravisCI, Gitpod
b. Relationships & interactions
• Lerna orchestrates independent package versions, runs build scripts for each package and publishes to npm.
• Core Engine exposes hook points; each plugin registers itself with core via plugin APIs.
• Umbrella package imports core + a set of plugins and re-exports a unified API.
• Examples import either the umbrella package or individual packages to demonstrate usage.
• Documentation site pulls API definitions and code snippets from packages.
• CI pipeline (Travis/Gitpod) triggers lint → build → test → publish.
c. Architectural patterns & principles
• Monorepo via Lerna for unified versioning and dependencies.
• Plugin-based architecture: core engine + extension points.
• Separation of concerns: each feature in its own package, shared-utils for common helpers.
• Test-driven (unit & E2E) ensures reliability.
d. Technologies & frameworks
• JavaScript/TypeScript, Lerna, Jest, Puppeteer for E2E, VuePress for docs, Webpack/Rollup (in examples’ build scripts).
• CI/CD: TravisCI, Gitpod.
Diagrammatic elements
• Rectangles for “packages” (core, plugins, umbrella)
• Parallelograms for examples and docs
• From Lerna to each package (build & versioning)
• From plugins to core (plugin registration)
• From umbrella to core + plugins (aggregation)
• From examples to package(s) (consumer)
• From CI/CD to build/test/publish and back to GitHub/npm
• Management & CI (e.g., blue)
• Core & Plugins (e.g., green)
• Examples & Docs (e.g., orange)
• External Services (e.g., gray)