跪拜 Guibai
← All articles
Frontend

Vite Build Outputs, Reborn as HAP, HAR, HSP on HarmonyOS

By Nayana ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Anyone shipping to HarmonyOS devices needs to know that HAR copies itself into every consumer, while HSP stays single-instance in memory and on disk. Mistaking them bloats the .app or breaks runtime sharing, and the rules for dependency visibility and resource paths differ enough from npm/Webpack habits to cause silent bugs.

Summary

HarmonyOS's packaging pipeline mirrors what Vite does when it splits a bundle: an entry point, route chunks, and shared vendor code. But the artifact names change, and the rules are stricter. The entry is an entry-type HAP, optional business blocks become feature-type HAPs, and shared code comes in two flavors. HAR is a static archive that gets compiled into every consuming module, so each consumer carries its own copy. HSP is a dynamic shared package that stays as one file in the .app and loads once per process.

Those two options answer the same question a Vite developer solves with manualChunks: where does the duplicate code go? HAR is the default for a single-module project, like components and utilities inside one HAP. HSP is the right call when multiple HAPs reference a large piece of business code, resources, or native .so files, because it only exists once in the final bundle. The trade-off is explicit: HSP increases build time and memory because more language-compilation tasks get triggered, so small utilities stay in HAR.

The guide also walks through the quirks a Vue dev will hit: HSP dependencies don't propagate transitively (A depends on B, B on C, but A cannot import from C), and resource paths inside HSP are resolved relative to the caller, not the file writing them, so you need $r() and an exported manager class. An integration-state HSP exists for sharing across multiple apps within one organization; it holds no bundleName at build time and gets re-signed with the host's identity when consumed.

Takeaways
— HAP is the only installable unit; an app must have at least one entry-type HAP, and only one entry HAP per device type.
— HAR is compiled into each consuming HAP or HSP at build time, so it never appears as a separate file in the final .app; multiple consumers mean multiple copies.
— HSP is compiled independently and loaded at runtime, with only one copy in the .app and one instance in the process.
— Feature-type HAP is for optional modules that users can download later; it is not the same as route lazy-loading.
— Integration-state HSP is an intermediate form for sharing across multiple apps within one organization; it gets the host's bundleName and is re-signed when consumed.
— HSP dependencies do not transitively expose APIs: if A depends on B and B on C, A cannot import from C, and dependency cycles are forbidden.
— Resource paths in HSP are resolved relative to the caller, so an HSP must use $r() for its own resources and export a manager class for external images and text.
— Use HAR for single-HAP reuse, HSP when multiple HAPs share a large chunk, and feature HAP for late-install features.
Conclusions

The hardest boundary for a Vite developer is that HAR's duplication is invisible after compilation: you only feel it as package bloat, while HSP's deduplication comes with a measurable compile-time and memory penalty.

The article's resource-path rule inverts a core Webpack/Vite intuition: a relative path in an HSP file does not resolve from that file, but from the caller, so $r() is not a suggestion but a requirement.

The integration-state HSP is the rare packaging mechanism designed specifically for cross-app sharing; it exists because the strict bundleName binding of normal HSPs made another option necessary.

Concepts & terms
HAP (Harmony Ability Package)
The basic unit of installation and execution on HarmonyOS, containing code, resources, libraries, and configuration. An app has at least one, of type entry or feature.
HAR (Harmony Archive)
A static shared package that is compiled into each consuming HAP/HSP at build time. Each consumer gets its own copy, making it analogous to a library bundled by Vite.
HSP (Harmony Shared Package)
A dynamic shared package compiled independently. Only one copy exists in the .app and one instance in the process at runtime, analogous to a shared chunk from manualChunks.
App Pack (.app)
The bundle of all HAPs and HSPs submitted for listing. The system installs the individual .hap and .hsp files extracted from it.
Integration-state HSP
An intermediate HSP form used when multiple apps in one organization share code. It has no fixed bundleName at build time; when consumed, the toolchain swaps in the host's bundleName and re-signs it.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗