[{"data":1,"prerenderedAt":789},["ShallowReactive",2],{"\u002Farticles\u002Fvue-component-library-npm":3},{"id":4,"title":5,"body":6,"date":773,"description":774,"extension":775,"head":776,"image":777,"meta":778,"navigation":779,"ogImage":776,"path":780,"rating":310,"readingTime":781,"robots":776,"schemaOrg":776,"seo":782,"sitemap":783,"stem":784,"tags":785,"__hash__":788},"articles_en\u002Farticles\u002Fvue-component-library-npm.md","Turning Components Copy-Pasted Across 6 Projects into One npm Package",{"type":7,"value":8,"toc":761},"minimark",[9,20,25,49,56,60,67,93,104,133,139,143,157,168,182,209,229,238,242,273,288,356,369,373,386,399,433,441,445,455,469,491,518,533,537,547,622,628,632,635,660,678,682,701,710,713,717,720,751,757],[10,11,12],"note",{},[13,14,15,16],"p",{},"A hands-on retrospective on taking components that had been copy-pasted across 6 H5 projects, migrating them to Vue 3 + Vant 4, and packaging them as an npm package. ",[17,18,19],"strong",{},"The conclusion up front: the component code is simple. The hard part was never the components, but the layer of \"engineering contract\" that surrounds them.",[21,22,24],"h2",{"id":23},"opening-the-components-are-simple-and-they-are-not-the-hard-part","Opening: the components are simple, and they are not the hard part",[13,26,27,28,32,33,36,37,40,41,44,45,48],{},"When I took this on, I also assumed it was grunt work. Across a dozen or so H5 projects in the ",[29,30,31],"code",{},"acme-*"," family, components like ",[29,34,35],{},"cascader",", ",[29,38,39],{},"select",", and ",[29,42,43],{},"dialog"," had been copy-pasted several times over. Copy them into a new repo, run ",[29,46,47],{},"npm publish",", and call it a day, right?",[13,50,51,52,55],{},"Only once I started did I realize: ",[17,53,54],{},"each component, taken alone, really is simple (a thin wrapper around Vant plus some styling), but \"turning it into a package that other people can safely depend on\" hides a pile of pitfalls the compiler will never report for you."," Below are the ones I think are most worth explaining in depth.",[21,57,59],{"id":58},"_1-what-can-be-extracted-and-what-cant-look-at-coupling-not-complexity","1. What can be extracted and what can't: look at coupling, not complexity",[13,61,62,63,66],{},"My first instinct was \"extract the simple ones first\". Wrong. What decides whether a component can be extracted cleanly is its ",[17,64,65],{},"dependency graph",", not its line count.",[68,69,70,80],"ul",{},[71,72,73,75,76,79],"li",{},[29,74,35],{}," is 260 lines with complicated logic, but it depends only on Vant → ",[17,77,78],{},"easy to extract",".",[71,81,82,85,86,89,90,79],{},[29,83,84],{},"city-search"," is a simple search box, yet it does ",[29,87,88],{},"import { queryCityList } from '@\u002Fapi'"," → ",[17,91,92],{},"hard to extract",[13,94,95,96,99,100,103],{},"A library component cannot assume the host has some ",[29,97,98],{},"@\u002Fapi",". The fix is ",[17,101,102],{},"dependency inversion",": push \"fetching the data\" out of the component. There are three ways to do it, and the trade-off is \"who owns loading \u002F caching \u002F error handling\":",[105,106,111],"pre",{"className":107,"code":108,"language":109,"meta":110,"style":110},"language-txt shiki shiki-themes github-light github-dark github-dark","data prop   : host fetches data and passes it in        → purest component, most work for the host\nfetcher     : pass the fetch function in as a prop      → flexible, but couples the call timing\nemit event  : component emits @search, host fills data  → decoupled interaction, usually the best\n","txt","",[29,112,113,121,127],{"__ignoreMap":110},[114,115,118],"span",{"class":116,"line":117},"line",1,[114,119,120],{},"data prop   : host fetches data and passes it in        → purest component, most work for the host\n",[114,122,124],{"class":116,"line":123},2,[114,125,126],{},"fetcher     : pass the fetch function in as a prop      → flexible, but couples the call timing\n",[114,128,130],{"class":116,"line":129},3,[114,131,132],{},"emit event  : component emits @search, host fills data  → decoupled interaction, usually the best\n",[134,135,136],"blockquote",{},[13,137,138],{},"Takeaway: to judge \"can this become a library\", draw the dependency graph first. Visual complexity will fool you; coupling won't.",[21,140,142],{"id":141},"_2-a-pitfall-that-compiles-but-doesnt-run","2. A pitfall that \"compiles but doesn't run\"",[13,144,145,146,152,153,156],{},"This was the most valuable lesson of the whole exercise. Vant 4 ",[17,147,148,149],{},"removed ",[29,150,151],{},"DatetimePicker"," (it was split into ",[29,154,155],{},"DatePicker","). Without thinking, I wrote:",[105,158,162],{"className":159,"code":160,"language":161,"meta":110,"style":110},"language-javascript shiki shiki-themes github-light github-dark github-dark","import { DatetimePicker } from 'vant'   \u002F\u002F in Vant 4, this is undefined\n","javascript",[29,163,164],{"__ignoreMap":110},[114,165,166],{"class":116,"line":117},[114,167,160],{},[13,169,170,173,174,177,178,181],{},[29,171,172],{},"vite build"," ",[17,175,176],{},"went green",". TypeScript didn't complain, esbuild didn't complain, nothing said a word. Only when the component rendered in the browser did a ",[29,179,180],{},"[Vue warn]: Failed to resolve component"," show up.",[13,183,184,185,188,189,192,193,196,197,200,201,204,205,208],{},"Why can't the build catch it? Because ",[17,186,187],{},"Vue resolves components at runtime",". The template ",[29,190,191],{},"\u003Cvan-datetime-picker>"," is compiled into ",[29,194,195],{},"resolveComponent(\"van-datetime-picker\")",", a call that ",[17,198,199],{},"looks up the component registry by string name at runtime",". The ",[29,202,203],{},"import"," yielding ",[29,206,207],{},"undefined"," is perfectly legal as far as JS is concerned (you have merely registered a local component whose value is undefined). The warning only appears when rendering actually happens and the lookup fails.",[105,210,212],{"className":159,"code":211,"language":161,"meta":110,"style":110},"\u002F\u002F What the compiled template looks like (simplified)\nconst _component_van_datetime_picker = resolveComponent(\"van-datetime-picker\")\n\u002F\u002F import is undefined → not found in the registry → warns only at runtime, silent at compile time\n",[29,213,214,219,224],{"__ignoreMap":110},[114,215,216],{"class":116,"line":117},[114,217,218],{},"\u002F\u002F What the compiled template looks like (simplified)\n",[114,220,221],{"class":116,"line":123},[114,222,223],{},"const _component_van_datetime_picker = resolveComponent(\"van-datetime-picker\")\n",[114,225,226],{"class":116,"line":129},[114,227,228],{},"\u002F\u002F import is undefined → not found in the registry → warns only at runtime, silent at compile time\n",[134,230,231],{},[13,232,233,234,237],{},"Takeaway: ",[17,235,236],{},"\"the build is green\" is the weakest guarantee there is."," For a major framework upgrade you must read the migration guide, check for removed \u002F renamed APIs, and use \"export-name existence checks + real rendering\" to cover the blind spots the compiler can't.",[21,239,241],{"id":240},"_3-migrating-v-model-one-component-is-both-consumer-and-provider","3. Migrating v-model: one component is both consumer and provider",[13,243,244,245,248,249,89,252,248,255,258,259,89,262,265,266,269,270,79],{},"The v-model contract changed completely from Vue 2 to 3: ",[29,246,247],{},"value"," + ",[29,250,251],{},"@input",[29,253,254],{},"modelValue",[29,256,257],{},"update:modelValue","; ",[29,260,261],{},".sync",[29,263,264],{},"v-model:x",". On top of that, Vant 4 changed Popup\u002FDialog visibility from ",[29,267,268],{},"v-model"," to ",[29,271,272],{},"v-model:show",[13,274,275,276,279,280,283,284,287],{},"The key point: our ",[29,277,278],{},"Dialog"," component is, ",[17,281,282],{},"on the inside",", a v-model consumer of van-dialog, and ",[17,285,286],{},"on the outside",", a v-model provider to the host. Both ends have to change together, with a watch in the middle passing values through in both directions:",[105,289,291],{"className":159,"code":290,"language":161,"meta":110,"style":110},"\u003Cvan-dialog v-model:show=\"isShow\">…\u003C\u002Fvan-dialog>\n\u003Cscript>\nexport default {\n  props: { modelValue: { type: Boolean, default: false } },\n  emits: ['update:modelValue'],\n  watch: {\n    modelValue(v){ this.isShow = v },                                  \u002F\u002F host → internal\n    isShow(v){ if (v !== this.modelValue) this.$emit('update:modelValue', v) }, \u002F\u002F internal → host\n  },\n}\n\u003C\u002Fscript>\n",[29,292,293,298,303,308,314,320,326,332,338,344,350],{"__ignoreMap":110},[114,294,295],{"class":116,"line":117},[114,296,297],{},"\u003Cvan-dialog v-model:show=\"isShow\">…\u003C\u002Fvan-dialog>\n",[114,299,300],{"class":116,"line":123},[114,301,302],{},"\u003Cscript>\n",[114,304,305],{"class":116,"line":129},[114,306,307],{},"export default {\n",[114,309,311],{"class":116,"line":310},4,[114,312,313],{},"  props: { modelValue: { type: Boolean, default: false } },\n",[114,315,317],{"class":116,"line":316},5,[114,318,319],{},"  emits: ['update:modelValue'],\n",[114,321,323],{"class":116,"line":322},6,[114,324,325],{},"  watch: {\n",[114,327,329],{"class":116,"line":328},7,[114,330,331],{},"    modelValue(v){ this.isShow = v },                                  \u002F\u002F host → internal\n",[114,333,335],{"class":116,"line":334},8,[114,336,337],{},"    isShow(v){ if (v !== this.modelValue) this.$emit('update:modelValue', v) }, \u002F\u002F internal → host\n",[114,339,341],{"class":116,"line":340},9,[114,342,343],{},"  },\n",[114,345,347],{"class":116,"line":346},10,[114,348,349],{},"}\n",[114,351,353],{"class":116,"line":352},11,[114,354,355],{},"\u003C\u002Fscript>\n",[13,357,358,359,362,363,365,366,368],{},"For popup-based components such as the cascader and date picker, I used a ",[29,360,361],{},"computed"," with get\u002Fset to pass the host's ",[29,364,254],{}," straight through to ",[29,367,272],{},", which is cleaner.",[21,370,372],{"id":371},"_4-a-library-is-in-essence-an-anti-corruption-layer","4. A library is, in essence, an \"anti-corruption layer\"",[13,374,375,377,378,381,382,385],{},[29,376,155],{}," illustrates this best. In Vant 4 its v-model data changed from a ",[29,379,380],{},"Date"," to an array of strings, ",[29,383,384],{},"['2024','06','30']",". If I passed that change through, every consumer would have to change too, and the library would become nothing more than \"a relay for upstream breaking changes\", with no value at all.",[13,387,388,389,392,393,173,395,398],{},"So I had the component adapt to the new format ",[17,390,391],{},"internally",", while still accepting \u002F emitting ",[29,394,380],{},[17,396,397],{},"externally",":",[105,400,402],{"className":159,"code":401,"language":161,"meta":110,"style":110},"\u002F\u002F In:  Date → ['Y','M','D'] fed to Vant 4\n\u002F\u002F Out: string[] from confirm → converted back to Date and emitted to the host\nconfirm({ selectedValues }){\n  const [y,m,d] = selectedValues\n  this.$emit('select', new Date(+y, +m - 1, +d))\n}\n",[29,403,404,409,414,419,424,429],{"__ignoreMap":110},[114,405,406],{"class":116,"line":117},[114,407,408],{},"\u002F\u002F In:  Date → ['Y','M','D'] fed to Vant 4\n",[114,410,411],{"class":116,"line":123},[114,412,413],{},"\u002F\u002F Out: string[] from confirm → converted back to Date and emitted to the host\n",[114,415,416],{"class":116,"line":129},[114,417,418],{},"confirm({ selectedValues }){\n",[114,420,421],{"class":116,"line":310},[114,422,423],{},"  const [y,m,d] = selectedValues\n",[114,425,426],{"class":116,"line":316},[114,427,428],{},"  this.$emit('select', new Date(+y, +m - 1, +d))\n",[114,430,431],{"class":116,"line":322},[114,432,349],{},[134,434,435],{},[13,436,233,437,440],{},[17,438,439],{},"the value of a library = absorbing upstream churn internally and giving consumers a stable external contract."," This is what the anti-corruption layer that backend engineers talk about looks like in a frontend component library. Swap the internals as much as you like; the public API has to stay stable.",[21,442,444],{"id":443},"_5-peerdependencies-and-the-two-vues-disaster","5. peerDependencies and the \"two Vues\" disaster",[13,446,447,448,451,452],{},"Many people think vue \u002F vant go into ",[29,449,450],{},"peerDependencies"," \"to reduce bundle size\". That is only a side effect. ",[17,453,454],{},"The real reason is that the framework must be a singleton.",[13,456,457,458,461,462,465,466,398],{},"If the library puts vue in ",[29,459,460],{},"dependencies"," and bundles it into its output, the host project ends up with ",[17,463,464],{},"two Vue runtimes",". The consequence is not \"wasted bytes\" but ",[17,467,468],{},"functional breakage",[68,470,471,478,484],{},[71,472,473,474,477],{},"There are two reactivity systems, and ",[29,475,476],{},"provide \u002F inject"," does not work across instances;",[71,479,480,483],{},[29,481,482],{},"app.use(Vant)"," registers on the host's app instance, while your components run on the copy of Vue bundled with the library → components can't be found and injections come back empty;",[71,485,486,487,490],{},"In the React world this disease is called ",[29,488,489],{},"Invalid hook call",": multiple copies of react each have their own dispatcher, and hooks simply crash.",[13,492,493,494,501,502,505,506,509,510,513,514,517],{},"This is also why ",[17,495,496,497,500],{},"debugging a component library with ",[29,498,499],{},"npm link"," so often blows up",": the symlink makes the library resolve the Vue\u002FReact in its own ",[29,503,504],{},"node_modules",", and instantly you have two instances. The fix is peerDeps + marking them ",[29,507,508],{},"external"," at build time + letting the host provide the one and only copy (or, for local debugging, using ",[29,511,512],{},"yalc"," or configuring ",[29,515,516],{},"resolve.dedupe",").",[105,519,521],{"className":159,"code":520,"language":161,"meta":110,"style":110},"\u002F\u002F vite.config: externalize the peers so the output never contains vue\u002Fvant\nrollupOptions: { external: ['vue', 'vant'] }\n",[29,522,523,528],{"__ignoreMap":110},[114,524,525],{"class":116,"line":117},[114,526,527],{},"\u002F\u002F vite.config: externalize the peers so the output never contains vue\u002Fvant\n",[114,529,530],{"class":116,"line":123},[114,531,532],{},"rollupOptions: { external: ['vue', 'vant'] }\n",[21,534,536],{"id":535},"_6-you-think-you-are-publishing-components-you-are-actually-publishing-a-contract","6. You think you are publishing components; you are actually publishing a \"contract\"",[13,538,539,540,543,544,398],{},"Application projects almost never touch these ",[29,541,542],{},"package.json"," fields, but for a library they are the ",[17,545,546],{},"public interface definition",[68,548,549,579,606],{},[71,550,551,556,557,560,561,564,565,568,569,571,572,574,575,578],{},[17,552,553],{},[29,554,555],{},"exports",": the authoritative entry point for modern resolution, taking precedence over ",[29,558,559],{},"main","\u002F",[29,562,563],{},"module",". It also has an underrated role, ",[17,566,567],{},"sealing off subpaths",": files not listed in ",[29,570,555],{}," simply cannot be ",[29,573,203],{},"ed by consumers, which effectively enforces the encapsulation boundary. (When writing a TS library, also note that the ",[29,576,577],{},"types"," condition must come first, otherwise type resolution falls through.)",[71,580,581,586,587,590,591,594,595,598,599,602,603,79],{},[17,582,583],{},[29,584,585],{},"sideEffects",": declares which modules have side effects. In a component library, ",[29,588,589],{},"import '.\u002Fx.css'"," is a ",[17,592,593],{},"side effect",". If you take the lazy route and write ",[29,596,597],{},"\"sideEffects\": false",", the bundler will decide that this CSS is \"unused\" and ",[17,600,601],{},"tree-shake it away",", and all your styles vanish. The correct form is ",[29,604,605],{},"\"sideEffects\": [\"**\u002F*.css\"]",[71,607,608,613,614,617,618,621],{},[17,609,610],{},[29,611,612],{},"files",": the publish allowlist. Use an allowlist (",[29,615,616],{},"[\"dist\"]",") rather than a ",[29,619,620],{},".npmignore"," denylist: when you miss something, the consequence is \"a file was left out\" rather than \"source code \u002F secrets were published by mistake\".",[13,623,624,625,627],{},"Then there are assets: the local images referenced by ",[29,626,35],{}," have to travel with the package. Let Vite automatically inline small images (under 4KB) into the output as base64; otherwise what you ship is a runtime 404.",[21,629,631],{"id":630},"_7-four-lines-of-defense-how-to-confirm-it-really-works","7. Four lines of defense: how to confirm it \"really works\"",[13,633,634],{},"Since \"the build is green\" can't be trusted, I verify in four layers, each catching a different kind of bug:",[105,636,638],{"className":107,"code":637,"language":109,"meta":110,"style":110},"① npm run build                      → template \u002F syntax compile errors\n② check vant export names with node  → runtime component resolution errors (the build can't catch these)\n③ npm pack + tar -tf                 → missing \u002F accidentally published files\n④ install the .tgz in a fresh project, then build → whether the exports map and peerDeps actually install\n",[29,639,640,645,650,655],{"__ignoreMap":110},[114,641,642],{"class":116,"line":117},[114,643,644],{},"① npm run build                      → template \u002F syntax compile errors\n",[114,646,647],{"class":116,"line":123},[114,648,649],{},"② check vant export names with node  → runtime component resolution errors (the build can't catch these)\n",[114,651,652],{"class":116,"line":129},[114,653,654],{},"③ npm pack + tar -tf                 → missing \u002F accidentally published files\n",[114,656,657],{"class":116,"line":310},[114,658,659],{},"④ install the .tgz in a fresh project, then build → whether the exports map and peerDeps actually install\n",[13,661,662,663,665,666,669,670,673,674,677],{},"Layer ④ is especially critical: it is the only way to verify that \"the ",[29,664,555],{}," field, the ",[29,667,668],{},"@scope\u002Fstyle.css"," subpath, and the peerDeps\" hold up from a ",[17,671,672],{},"real consumer's point of view",". The source projects are all Vue 2 and can't install this Vue 3 package, so I spun up a brand-new Vue 3 project specifically to install the ",[29,675,676],{},".tgz"," and get it running.",[21,679,681],{"id":680},"_8-publishing-risk-free-practice-with-verdaccio","8. Publishing: risk-free practice with Verdaccio",[13,683,684,685,688,689,692,693,696,697,700],{},"An internal library should not be published to public npm (the code becomes public and is almost impossible to take back). I used ",[17,686,687],{},"Verdaccio"," to run a local private registry and walk through the whole flow: the ",[29,690,691],{},"@acme"," scope is routed to ",[29,694,695],{},"localhost:4873"," via ",[29,698,699],{},".npmrc",", while all other dependencies still go to the public registry.",[105,702,704],{"className":107,"code":703,"language":109,"meta":110,"style":110},"@acme:registry=http:\u002F\u002Flocalhost:4873\u002F\n",[29,705,706],{"__ignoreMap":110},[114,707,708],{"class":116,"line":117},[114,709,703],{},[13,711,712],{},"Once that works, moving to the team's private registry (such as Azure Artifacts or Nexus) only means changing this one registry URL and the token; the commands are exactly the same. Verdaccio is a good choice for practice and for offline caching in CI.",[21,714,716],{"id":715},"wrapping-up-a-transferable-methodology","Wrapping up: a transferable methodology",[13,718,719],{},"Looking back, none of what I really learned here is tied to Vant:",[721,722,723,726,729,732,735,748],"ol",{},[71,724,725],{},"Extract based on coupling, not complexity, and decouple with dependency inversion;",[71,727,728],{},"Framework migrations have \"silent at compile time\" pitfalls, and a green build is the weakest guarantee;",[71,730,731],{},"A library is in essence an anti-corruption layer, and its external contract must be stable;",[71,733,734],{},"peerDeps exist for the framework singleton, not for smaller bundles;",[71,736,737,739,740,742,743,742,745,747],{},[29,738,542],{}," is a public interface, and ",[29,741,555],{}," \u002F ",[29,744,585],{},[29,746,612],{}," each deserve care;",[71,749,750],{},"Replace \"I think it's fine\" with layered verification.",[13,752,753,754],{},"Component logic is the \"algorithm\"; these things are the \"engineering\". Swap in a React component library, or even wrap an LLM tool SDK as an npm package someday, and the playbook is exactly the same. ",[17,755,756],{},"This is what separates \"can write components\" from \"can deliver reusable assets\".",[758,759,760],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":110,"searchDepth":123,"depth":123,"links":762},[763,764,765,766,767,768,769,770,771,772],{"id":23,"depth":123,"text":24},{"id":58,"depth":123,"text":59},{"id":141,"depth":123,"text":142},{"id":240,"depth":123,"text":241},{"id":371,"depth":123,"text":372},{"id":443,"depth":123,"text":444},{"id":535,"depth":123,"text":536},{"id":630,"depth":123,"text":631},{"id":680,"depth":123,"text":681},{"id":715,"depth":123,"text":716},"2026-06-30","A retrospective on migrating components that were copy-pasted across several H5 projects to Vue 3 + Vant 4 and shipping them as an npm package. The component code is easy; the engineering contract around it is the hard part.","md",null,"\u002Farticles\u002Fvue-component-library-npm.svg",{},true,"\u002Farticles\u002Fvue-component-library-npm","8",{"title":5,"description":774},{"loc":780},"articles\u002Fvue-component-library-npm",[786,787],"Frontend","Engineering","CwquLWhXulxkDMpW6UdfkvaQiq4qy1Jxvoa5ip8FRU4",1795761651794]