[{"data":1,"prerenderedAt":2268},["ShallowReactive",2],{"series-\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library":3,"series-context-\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library":225,"article-link-en-f9754148-40bd-4df2-a144-f686e894b200":375,"series-chapters-\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library":2260},{"id":4,"title":5,"body":6,"contentId":202,"date":203,"description":204,"extension":205,"github":206,"meetTheInstructor":209,"meta":210,"navigation":211,"part":212,"path":213,"rawbody":214,"robots":212,"schemaOrg":212,"seo":215,"seriesSlug":216,"sitemap":217,"stem":218,"subtitle":219,"topics":220,"__hash__":224},"series\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library.md","The Complete Guide to Building a Vue.js Component Library",{"type":7,"value":8,"toc":196},"minimark",[9,13,16,29,33,36,52,57,60,183,186,190],[10,11,12],"p",{},"Welcome! 👋",[10,14,15],{},"Initially, this series was intended to be a single article, but it soon became apparent that it would be too lengthy. Even GitHub didn't want to display it in the file tab of pull requests. Therefore, I decided to divide it into multiple articles to make it more digestible and to focus on each part individually.",[10,17,18,19,24,25],{},"This series is somewhat of a sequel to the article ",[20,21,23],"a",{"href":22},"\u002Fposts\u002Fthe-simplest-method-to-create-a-vue-js-component-library","The Simplest Method to Create a Vue.js Component Library"," but adopts a more professional approach. ",[26,27,28],"em",{},"You will learn how the most popular component libraries are constructed behind the scenes and how to create your own.",[30,31],"callout-read-more",{"id":32},"f9754148-40bd-4df2-a144-f686e894b200",[10,34,35],{},"While it's still my preferred method when I am certain about what I want to achieve, I also received feedback from readers seeking more custom behavior and requiring more complex features.",[10,37,38,39,45,46,51],{},"That's understandable. While ",[20,40,44],{"href":41,"rel":42},"https:\u002F\u002Fgithub.com\u002Funjs\u002Funbuild",[43],"nofollow","unbuild"," is a powerful tool, it cannot match the flexibility, plugin ecosystem, and adoption of ",[20,47,50],{"href":48,"rel":49},"https:\u002F\u002Fvite.dev\u002F",[43],"Vite",".",[53,54,56],"h2",{"id":55},"what-to-expect","What to expect",[10,58,59],{},"Throughout this series, we will address the following topics:",[61,62,63,77,112,130,147,153,165],"ol",{},[64,65,66,70,71,76],"li",{},[67,68,69],"strong",{},"Getting Started with Monorepo, Linter, and Formatter",": We'll set up a monorepo to manage our component library and all the tools necessary to ensure a good development experience. We'll also configure ",[20,72,75],{"href":73,"rel":74},"https:\u002F\u002Feslint.org\u002F",[43],"ESLint"," to ensure code consistency.",[64,78,79,82,83,88,89,94,95,100,101,106,107,51],{},[67,80,81],{},"Building a First Component with Vue.js and Tailwind CSS",": We'll create our first component with ",[20,84,87],{"href":85,"rel":86},"https:\u002F\u002Fvuejs.org\u002F",[43],"Vue.js",", ",[20,90,93],{"href":91,"rel":92},"https:\u002F\u002Ftailwindcss.com\u002F",[43],"Tailwind CSS",", and ",[20,96,99],{"href":97,"rel":98},"https:\u002F\u002Fwww.tailwind-variants.org\u002F",[43],"Tailwind Variants"," using the most advanced and powerful techniques employed in popular component libraries like ",[20,102,105],{"href":103,"rel":104},"https:\u002F\u002Fwww.radix-vue.com\u002F",[43],"Radix Vue"," and ",[20,108,111],{"href":109,"rel":110},"https:\u002F\u002Fui.nuxt.com\u002F",[43],"Nuxt UI",[64,113,114,117,118,123,124,129],{},[67,115,116],{},"A Better Development Experience with TypeScript and TSConfig",": We'll enhance our development experience by adding ",[20,119,122],{"href":120,"rel":121},"https:\u002F\u002Fwww.typescriptlang.org\u002F",[43],"TypeScript"," to our component library and configuring ",[20,125,128],{"href":126,"rel":127},"https:\u002F\u002Fwww.typescriptlang.org\u002Ftsconfig",[43],"TSConfig"," to ensure that our components are typed correctly and that we can take full advantage of TypeScript's features.",[64,131,132,135,136,106,141,146],{},[67,133,134],{},"Automatic Component Testing with Vitest and Testing Library",": We'll write tests for our components using ",[20,137,140],{"href":138,"rel":139},"https:\u002F\u002Fvitest.dev\u002Fguide\u002F",[43],"Vitest",[20,142,145],{"href":143,"rel":144},"https:\u002F\u002Ftesting-library.com\u002F",[43],"Testing Library"," to ensure that our components function as expected. This is crucial for a professional component library that remains reliable over time.",[64,148,149,152],{},[67,150,151],{},"Delivering Components Continuously and with Confidence",": We'll establish a continuous integration process to ensure that our components are always delivered with confidence. For every commit of every branch, we'll run our tests to verify everything is functioning correctly. We'll also establish a release process to publish our components to npm.",[64,154,155,158,159,164],{},[67,156,157],{},"Creating a Playground and a Storybook for Components",": We'll create a playground to test our components in a real environment and a storybook with ",[20,160,163],{"href":161,"rel":162},"https:\u002F\u002Fhistoire.dev\u002F",[43],"Histoire"," to document them. This will enable us to share our components with other developers and to test them in various scenarios, both in isolation and in combination.",[64,166,167,170,171,176,177,182],{},[67,168,169],{},"Enhancing Integration with Unplugin and Nuxt Module",": We'll enhance the integration of our component library with the ecosystem by creating a Vite plugin with ",[20,172,175],{"href":173,"rel":174},"https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components",[43],"Unplugin Vue Components"," and a ",[20,178,181],{"href":179,"rel":180},"https:\u002F\u002Fnuxt.com\u002Fdocs\u002Fguide\u002Fgoing-further\u002Fmodules",[43],"Nuxt module"," to utilize our components in a Nuxt application.",[10,184,185],{},"By the end of this series, you will have a complete Vue.js component library that you can use in your projects or share with the community, like a professional developer. Isn't that exciting? 😎",[53,187,189],{"id":188},"chapters","Chapters",[191,192],"series-chapters-list",{"className":193},[194,195],"mx-auto","max-w-5xl",{"title":197,"searchDepth":198,"depth":198,"links":199},"",2,[200,201],{"id":55,"depth":198,"text":56},{"id":188,"depth":198,"text":189},"55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1","2024-11-24","Discover how to build a Vue.js component library using Vite with a guide that covers essential techniques for professional web development.","md",{"name":207,"repository":208},"huchet-vue","barbapapazes\u002Fhuchet-vue","I create this series because I love \u003Cstrong>Vue.js\u003C\u002Fstrong> and, for years, I have been unable to find a complete, up-to-date, and \u003Cstrong>professional guide\u003C\u002Fstrong> on how to build a Vue.js component library and want more software developers to feel empowered to create their own.",{},true,null,"\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library","---\ncontentId: 55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1\ntitle: The Complete Guide to Building a Vue.js Component Library\nsubtitle: The most advanced and professional course to build a Vue.js component library with Vite.\ndescription: Discover how to build a Vue.js component library using Vite with a guide that covers essential techniques for professional web development.\ndate: 2024-11-24\ntopics:\n  - vue\n  - vite\n  - component library\ngithub:\n  name: huchet-vue\n  repository: barbapapazes\u002Fhuchet-vue\nmeetTheInstructor: I create this series because I love \u003Cstrong>Vue.js\u003C\u002Fstrong> and, for years, I have been unable to find a complete, up-to-date, and \u003Cstrong>professional guide\u003C\u002Fstrong> on how to build a Vue.js component library and want more software developers to feel empowered to create their own.\nseriesSlug: the-complete-guide-to-building-a-vue-js-component-library\n---\n\nWelcome! 👋\n\nInitially, this series was intended to be a single article, but it soon became apparent that it would be too lengthy. Even GitHub didn't want to display it in the file tab of pull requests. Therefore, I decided to divide it into multiple articles to make it more digestible and to focus on each part individually.\n\nThis series is somewhat of a sequel to the article [The Simplest Method to Create a Vue.js Component Library](\u002Fposts\u002Fthe-simplest-method-to-create-a-vue-js-component-library) but adopts a more professional approach. _You will learn how the most popular component libraries are constructed behind the scenes and how to create your own._\n\n\u003CCalloutReadMore id=\"f9754148-40bd-4df2-a144-f686e894b200\" \u002F>\n\nWhile it's still my preferred method when I am certain about what I want to achieve, I also received feedback from readers seeking more custom behavior and requiring more complex features.\n\nThat's understandable. While [unbuild](https:\u002F\u002Fgithub.com\u002Funjs\u002Funbuild) is a powerful tool, it cannot match the flexibility, plugin ecosystem, and adoption of [Vite](https:\u002F\u002Fvite.dev\u002F).\n\n## What to expect\n\nThroughout this series, we will address the following topics:\n\n1. **Getting Started with Monorepo, Linter, and Formatter**: We'll set up a monorepo to manage our component library and all the tools necessary to ensure a good development experience. We'll also configure [ESLint](https:\u002F\u002Feslint.org\u002F) to ensure code consistency.\n2. **Building a First Component with Vue.js and Tailwind CSS**: We'll create our first component with [Vue.js](https:\u002F\u002Fvuejs.org\u002F), [Tailwind CSS](https:\u002F\u002Ftailwindcss.com\u002F), and [Tailwind Variants](https:\u002F\u002Fwww.tailwind-variants.org\u002F) using the most advanced and powerful techniques employed in popular component libraries like [Radix Vue](https:\u002F\u002Fwww.radix-vue.com\u002F) and [Nuxt UI](https:\u002F\u002Fui.nuxt.com\u002F).\n3. **A Better Development Experience with TypeScript and TSConfig**: We'll enhance our development experience by adding [TypeScript](https:\u002F\u002Fwww.typescriptlang.org\u002F) to our component library and configuring [TSConfig](https:\u002F\u002Fwww.typescriptlang.org\u002Ftsconfig) to ensure that our components are typed correctly and that we can take full advantage of TypeScript's features.\n4. **Automatic Component Testing with Vitest and Testing Library**: We'll write tests for our components using [Vitest](https:\u002F\u002Fvitest.dev\u002Fguide\u002F) and [Testing Library](https:\u002F\u002Ftesting-library.com\u002F) to ensure that our components function as expected. This is crucial for a professional component library that remains reliable over time.\n5. **Delivering Components Continuously and with Confidence**: We'll establish a continuous integration process to ensure that our components are always delivered with confidence. For every commit of every branch, we'll run our tests to verify everything is functioning correctly. We'll also establish a release process to publish our components to npm.\n6. **Creating a Playground and a Storybook for Components**: We'll create a playground to test our components in a real environment and a storybook with [Histoire](https:\u002F\u002Fhistoire.dev\u002F) to document them. This will enable us to share our components with other developers and to test them in various scenarios, both in isolation and in combination.\n7. **Enhancing Integration with Unplugin and Nuxt Module**: We'll enhance the integration of our component library with the ecosystem by creating a Vite plugin with [Unplugin Vue Components](https:\u002F\u002Fgithub.com\u002Funplugin\u002Funplugin-vue-components) and a [Nuxt module](https:\u002F\u002Fnuxt.com\u002Fdocs\u002Fguide\u002Fgoing-further\u002Fmodules) to utilize our components in a Nuxt application.\n\nBy the end of this series, you will have a complete Vue.js component library that you can use in your projects or share with the community, like a professional developer. Isn't that exciting? 😎\n\n## Chapters\n\n::series-chapters-list\n---\nclass: mx-auto max-w-5xl\n---\n::\n",{"title":5,"description":204},"the-complete-guide-to-building-a-vue-js-component-library",{"loc":213},"series\u002Fthe-complete-guide-to-building-a-vue-js-component-library","The most advanced and professional course to build a Vue.js component library with Vite.",[221,222,223],"vue","vite","component library","2VSSnBlkuSLlt05n9MySLBFt6vBkgd7691eX-TK2cTs",{"overview":226,"chapters":340},{"id":4,"title":5,"body":227,"contentId":202,"date":203,"description":204,"extension":205,"github":335,"meetTheInstructor":209,"meta":336,"navigation":211,"part":212,"path":213,"rawbody":214,"robots":212,"schemaOrg":212,"seo":337,"seriesSlug":216,"sitemap":338,"stem":218,"subtitle":219,"topics":339,"__hash__":224},{"type":7,"value":228,"toc":331},[229,231,233,239,241,243,251,253,255,324,326,328],[10,230,12],{},[10,232,15],{},[10,234,18,235,24,237],{},[20,236,23],{"href":22},[26,238,28],{},[30,240],{"id":32},[10,242,35],{},[10,244,38,245,45,248,51],{},[20,246,44],{"href":41,"rel":247},[43],[20,249,50],{"href":48,"rel":250},[43],[53,252,56],{"id":55},[10,254,59],{},[61,256,257,264,283,293,303,307,314],{},[64,258,259,70,261,76],{},[67,260,69],{},[20,262,75],{"href":73,"rel":263},[43],[64,265,266,82,268,88,271,94,274,100,277,106,280,51],{},[67,267,81],{},[20,269,87],{"href":85,"rel":270},[43],[20,272,93],{"href":91,"rel":273},[43],[20,275,99],{"href":97,"rel":276},[43],[20,278,105],{"href":103,"rel":279},[43],[20,281,111],{"href":109,"rel":282},[43],[64,284,285,117,287,123,290,129],{},[67,286,116],{},[20,288,122],{"href":120,"rel":289},[43],[20,291,128],{"href":126,"rel":292},[43],[64,294,295,135,297,106,300,146],{},[67,296,134],{},[20,298,140],{"href":138,"rel":299},[43],[20,301,145],{"href":143,"rel":302},[43],[64,304,305,152],{},[67,306,151],{},[64,308,309,158,311,164],{},[67,310,157],{},[20,312,163],{"href":161,"rel":313},[43],[64,315,316,170,318,176,321,182],{},[67,317,169],{},[20,319,175],{"href":173,"rel":320},[43],[20,322,181],{"href":179,"rel":323},[43],[10,325,185],{},[53,327,189],{"id":188},[191,329],{"className":330},[194,195],{"title":197,"searchDepth":198,"depth":198,"links":332},[333,334],{"id":55,"depth":198,"text":56},{"id":188,"depth":198,"text":189},{"name":207,"repository":208},{},{"title":5,"description":204},{"loc":213},[221,222,223],[341,346,350,355,360,365,370],{"contentId":342,"title":69,"description":343,"path":344,"part":345,"github":212,"topics":212},"1f6cf4af-8637-458d-93f8-52ac60dd8f87","Start your monorepo journey! Learn how to set up a linter and formatter for streamlined coding and organizing your project.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fgetting-started-with-monorepo-linter-and-formatter",1,{"contentId":347,"title":81,"description":348,"path":349,"part":198,"github":212,"topics":212},"e9f57d8c-a722-4fd7-b326-61af0ea9dbd4","Creating a simple Vue.js button component using Tailwind CSS for styling and variants, along with step-by-step library build configuration using Vite.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fbuilding-a-first-component-with-vuejs-and-tailwind-css",{"contentId":351,"title":116,"description":352,"path":353,"part":354,"github":212,"topics":212},"c2d651b0-4487-472d-b55a-b3cf195bcba8","Learn how to configure TypeScript with multiple contexts and generate types for a library to expose types to developers.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fa-better-development-experience-with-typescript-and-tsconfig",3,{"contentId":356,"title":134,"description":357,"path":358,"part":359,"github":212,"topics":212},"e95133e4-2362-4aa3-bd3d-b70afc6c4d4e","Learn how to automate testing for Vue.js components using Vitest and Testing Library for efficient and user-focused testing.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fautomatic-component-testing-with-vitest-and-testing-library",4,{"contentId":361,"title":151,"description":362,"path":363,"part":364,"github":212,"topics":212},"34bd87b4-cefd-44bb-affe-21e5e0fe7d56","Implement a CI pipeline with GitHub Actions and automate the release process to npm for Vue.js component libraries.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fdelivering-components-continuously-and-with-confidence",5,{"contentId":366,"title":157,"description":367,"path":368,"part":369,"github":212,"topics":212},"53bbc0ae-a7b8-49d3-8ae6-73b585c43efc","Learn how to create a playground and use Histoire to visualize and document your components in both isolated and combined scenarios.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fcreating-a-playground-and-a-storybook-for-components",6,{"contentId":371,"title":169,"description":372,"path":373,"part":374,"github":212,"topics":212},"8b808104-15ac-42ca-9535-cef0996ddf66","Discover how to seamlessly integrate a Vue.js component library using Unplugin Vue Components and Nuxt Module, enhancing usability and developer experience.","\u002Fseries\u002Fthe-complete-guide-to-building-a-vue-js-component-library\u002Fenhancing-integration-with-unplugin-and-nuxt-module",7,{"id":376,"title":23,"body":377,"contentId":32,"date":2252,"description":2253,"extension":205,"meta":2254,"navigation":211,"path":22,"rawbody":2255,"readingTime":573,"robots":212,"schemaOrg":212,"seo":2256,"sitemap":2257,"stem":2258,"__hash__":2259},"posts\u002Fposts\u002Fthe-simplest-method-to-create-a-vue-js-component-library.md",{"type":7,"value":378,"toc":2241},[379,390,396,420,422,437,440,443,590,593,680,686,689,698,705,749,760,859,863,868,875,882,911,918,930,944,948,966,969,972,988,991,1003,1008,1014,1029,1036,1175,1185,1189,1206,1225,1234,1337,1344,1348,1354,1393,1399,1406,1409,1420,1424,1427,1452,1455,1468,1492,1496,1499,1509,1569,1603,1614,1620,1772,1778,1785,1798,1802,1809,1812,1815,1823,1830,1860,1867,1889,1892,1906,1915,1921,1932,2003,2006,2027,2036,2040,2043,2050,2062,2071,2090,2095,2141,2154,2163,2166,2182,2185,2189,2192,2213,2222,2230,2235,2237],[10,380,381,382,386,387],{},"TL;DR: ",[383,384],"git-hub-link",{"repo":385},"barbapapazes\u002Fvue-library"," ",[26,388,389],{},"Continue reading to grasp the process behind it and understand the reasons for my choices.",[10,391,392,393],{},"A component library is a collection of reusable pieces that can be employed in various projects. It facilitates sharing resources among different projects and teams. These components may be low-level and generic like buttons, inputs, and modals, or more business-specific modules. ",[26,394,395],{},"Ultimately, it's a method to share code across projects, a crucial skill that can significantly save time.",[10,397,398,399,403,404,409,410,415,416,51],{},"However, building a component library with ",[20,400,87],{"href":401,"rel":402},"https:\u002F\u002Fvuejs.org",[43]," is harder than it seems because of the ",[20,405,408],{"href":406,"rel":407},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fscaling-up\u002Fsfc.html",[43],"Single File Components"," (SFC). In a standard TypeScript project, you usually transpile to JavaScript and bundle your files with a tool like ",[20,411,414],{"href":412,"rel":413},"https:\u002F\u002Ftsup.egoist.dev\u002F",[43],"tsup"," or ",[20,417,50],{"href":418,"rel":419},"https:\u002F\u002Fvitejs.dev\u002F",[43],[30,421],{"id":202},[423,424,427],"callout",{"color":425,"icon":426},"info","i-ph-info-duotone",[10,428,429,430,433,434],{},"Remember to ",[67,431,432],{},"deliver package code as native as possible"," and let userland tools handle transpiling and optimization. It's a principle I always keep in mind when crafting libraries, streamlining the process significantly. ",[26,435,436],{},"Each case is unique, but this is a solid guideline to follow.",[10,438,439],{},"The use of Vue.js SFC complicates this process. Let's examine why by exploring the challenges you'll encounter when constructing a Vue.js component library.",[10,441,442],{},"Consider the following component:",[444,445,449],"pre",{"className":446,"code":447,"filename":448,"language":221,"meta":197,"style":197},"language-vue shiki shiki-themes github-light github-light github-dark","\u003Cscript lang=\"ts\" setup>\nimport { useUser } from '..\u002Fcomposables'\n\nconst { user } = useUser()\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003Cdiv v-if=\"user\">\n    {{ user.name }}\n  \u003C\u002Fdiv>\n\u003C\u002Ftemplate>\n","src\u002Fcomponents\u002FUser.vue",[450,451,452,481,496,501,524,533,537,546,565,571,581],"code",{"__ignoreMap":197},[453,454,456,460,464,468,471,475,478],"span",{"class":455,"line":345},"line",[453,457,459],{"class":458},"sxrX7","\u003C",[453,461,463],{"class":462},"sovSZ","script",[453,465,467],{"class":466},"snPdu"," lang",[453,469,470],{"class":458},"=",[453,472,474],{"class":473},"sIIMD","\"ts\"",[453,476,477],{"class":466}," setup",[453,479,480],{"class":458},">\n",[453,482,483,487,490,493],{"class":455,"line":198},[453,484,486],{"class":485},"s8jYJ","import",[453,488,489],{"class":458}," { useUser } ",[453,491,492],{"class":485},"from",[453,494,495],{"class":473}," '..\u002Fcomposables'\n",[453,497,498],{"class":455,"line":354},[453,499,500],{"emptyLinePlaceholder":211},"\n",[453,502,503,506,509,513,516,518,521],{"class":455,"line":359},[453,504,505],{"class":485},"const",[453,507,508],{"class":458}," { ",[453,510,512],{"class":511},"sBjJW","user",[453,514,515],{"class":458}," } ",[453,517,470],{"class":485},[453,519,520],{"class":466}," useUser",[453,522,523],{"class":458},"()\n",[453,525,526,529,531],{"class":455,"line":364},[453,527,528],{"class":458},"\u003C\u002F",[453,530,463],{"class":462},[453,532,480],{"class":458},[453,534,535],{"class":455,"line":369},[453,536,500],{"emptyLinePlaceholder":211},[453,538,539,541,544],{"class":455,"line":374},[453,540,459],{"class":458},[453,542,543],{"class":462},"template",[453,545,480],{"class":458},[453,547,549,552,555,558,560,563],{"class":455,"line":548},8,[453,550,551],{"class":458},"  \u003C",[453,553,554],{"class":462},"div",[453,556,557],{"class":466}," v-if",[453,559,470],{"class":458},[453,561,562],{"class":473},"\"user\"",[453,564,480],{"class":458},[453,566,568],{"class":455,"line":567},9,[453,569,570],{"class":458},"    {{ user.name }}\n",[453,572,574,577,579],{"class":455,"line":573},10,[453,575,576],{"class":458},"  \u003C\u002F",[453,578,554],{"class":462},[453,580,480],{"class":458},[453,582,584,586,588],{"class":455,"line":583},11,[453,585,528],{"class":458},[453,587,543],{"class":462},[453,589,480],{"class":458},[10,591,592],{},"The composable is as follows:",[444,594,599],{"className":595,"code":596,"filename":597,"language":598,"meta":197,"style":197},"language-ts shiki shiki-themes github-light github-light github-dark","import { ref } from 'vue'\n\nexport function useUser() {\n  const user = ref({ name: 'John Doe' })\n\n  return {\n    user\n  }\n}\n","src\u002Fcomposables\u002FuseUser.ts","ts",[450,600,601,613,617,630,653,657,665,670,675],{"__ignoreMap":197},[453,602,603,605,608,610],{"class":455,"line":345},[453,604,486],{"class":485},[453,606,607],{"class":458}," { ref } ",[453,609,492],{"class":485},[453,611,612],{"class":473}," 'vue'\n",[453,614,615],{"class":455,"line":198},[453,616,500],{"emptyLinePlaceholder":211},[453,618,619,622,625,627],{"class":455,"line":354},[453,620,621],{"class":485},"export",[453,623,624],{"class":485}," function",[453,626,520],{"class":466},[453,628,629],{"class":458},"() {\n",[453,631,632,635,638,641,644,647,650],{"class":455,"line":359},[453,633,634],{"class":485},"  const",[453,636,637],{"class":511}," user",[453,639,640],{"class":485}," =",[453,642,643],{"class":466}," ref",[453,645,646],{"class":458},"({ name: ",[453,648,649],{"class":473},"'John Doe'",[453,651,652],{"class":458}," })\n",[453,654,655],{"class":455,"line":364},[453,656,500],{"emptyLinePlaceholder":211},[453,658,659,662],{"class":455,"line":369},[453,660,661],{"class":485},"  return",[453,663,664],{"class":458}," {\n",[453,666,667],{"class":455,"line":374},[453,668,669],{"class":458},"    user\n",[453,671,672],{"class":455,"line":548},[453,673,674],{"class":458},"  }\n",[453,676,677],{"class":455,"line":567},[453,678,679],{"class":458},"}\n",[10,681,682,683],{},"The component employs a composable imported from another file. ",[26,684,685],{},"It may seem trivial, but this poses a significant issue.",[10,687,688],{},"All of this follows the architecture below:",[444,690,696],{"className":691,"code":693,"filename":694,"language":695,"meta":197},[692],"language-text","src\u002F\n  components\u002F\n    User.vue\n  composables\u002F\n    useUser.ts\n  index.ts\n","Project structure","text",[450,697,693],{"__ignoreMap":197},[10,699,700,701,704],{},"The ",[450,702,703],{},"index.ts"," file acts as the library's entry point and it looks like this:",[444,706,709],{"className":595,"code":707,"filename":708,"language":598,"meta":197,"style":197},"import User from '.\u002Fcomponents\u002FUser.vue'\n\nexport { User }\n\nexport { useUser } from '.\u002Fcomposables\u002FuseUser'\n","src\u002Findex.ts",[450,710,711,723,727,734,738],{"__ignoreMap":197},[453,712,713,715,718,720],{"class":455,"line":345},[453,714,486],{"class":485},[453,716,717],{"class":458}," User ",[453,719,492],{"class":485},[453,721,722],{"class":473}," '.\u002Fcomponents\u002FUser.vue'\n",[453,724,725],{"class":455,"line":198},[453,726,500],{"emptyLinePlaceholder":211},[453,728,729,731],{"class":455,"line":354},[453,730,621],{"class":485},[453,732,733],{"class":458}," { User }\n",[453,735,736],{"class":455,"line":359},[453,737,500],{"emptyLinePlaceholder":211},[453,739,740,742,744,746],{"class":455,"line":364},[453,741,621],{"class":485},[453,743,489],{"class":458},[453,745,492],{"class":485},[453,747,748],{"class":473}," '.\u002Fcomposables\u002FuseUser'\n",[10,750,751,752,755,756,759],{},"Plus, the ",[450,753,754],{},"package.json"," includes some ",[450,757,758],{},"exports",":",[444,761,765],{"className":762,"code":763,"filename":754,"language":764,"meta":197,"style":197},"language-json shiki shiki-themes github-light github-light github-dark","{\n  \"type\": \"module\",\n  \"exports\": {\n    \".\": {\n      \"types\": \".\u002Fdist\u002Findex.d.ts\",\n      \"import\": \".\u002Fdist\u002Findex.mjs\"\n    }\n  },\n  \"main\": \"dist\u002Findex.mjs\",\n  \"types\": \"dist\u002Findex.d.ts\"\n}\n","json",[450,766,767,772,786,794,801,813,823,828,833,845,855],{"__ignoreMap":197},[453,768,769],{"class":455,"line":345},[453,770,771],{"class":458},"{\n",[453,773,774,777,780,783],{"class":455,"line":198},[453,775,776],{"class":511},"  \"type\"",[453,778,779],{"class":458},": ",[453,781,782],{"class":473},"\"module\"",[453,784,785],{"class":458},",\n",[453,787,788,791],{"class":455,"line":354},[453,789,790],{"class":511},"  \"exports\"",[453,792,793],{"class":458},": {\n",[453,795,796,799],{"class":455,"line":359},[453,797,798],{"class":511},"    \".\"",[453,800,793],{"class":458},[453,802,803,806,808,811],{"class":455,"line":364},[453,804,805],{"class":511},"      \"types\"",[453,807,779],{"class":458},[453,809,810],{"class":473},"\".\u002Fdist\u002Findex.d.ts\"",[453,812,785],{"class":458},[453,814,815,818,820],{"class":455,"line":369},[453,816,817],{"class":511},"      \"import\"",[453,819,779],{"class":458},[453,821,822],{"class":473},"\".\u002Fdist\u002Findex.mjs\"\n",[453,824,825],{"class":455,"line":374},[453,826,827],{"class":458},"    }\n",[453,829,830],{"class":455,"line":548},[453,831,832],{"class":458},"  },\n",[453,834,835,838,840,843],{"class":455,"line":567},[453,836,837],{"class":511},"  \"main\"",[453,839,779],{"class":458},[453,841,842],{"class":473},"\"dist\u002Findex.mjs\"",[453,844,785],{"class":458},[453,846,847,850,852],{"class":455,"line":573},[453,848,849],{"class":511},"  \"types\"",[453,851,779],{"class":458},[453,853,854],{"class":473},"\"dist\u002Findex.d.ts\"\n",[453,856,857],{"class":455,"line":583},[453,858,679],{"class":458},[53,860,862],{"id":861},"similar-to-a-standard-typescript-project","Similar to a standard TypeScript project",[423,864,865],{"color":425,"icon":426},[10,866,867],{},"A bundle is a single file encapsulating all your project's code. It optimizes project loading by minimizing the number of files to load. This bundle isn't transpiled (except TypeScript to JavaScript) or minified when creating an npm package.",[10,869,870,871,874],{},"Now, if we attempt to bundle this project, our ",[450,872,873],{},"dist"," folder will appear like this:",[444,876,880],{"className":877,"code":878,"filename":879,"language":695,"meta":197},[692],"dist\u002F\n  index.cjs\n","Bundled output",[450,881,878],{"__ignoreMap":197},[10,883,884,885,888,889,892,893],{},"Upon inspecting ",[450,886,887],{},"index.cjs",", you'll find a surprise: the ",[450,890,891],{},"useUser"," composable is inlined, but the component is entirely ignored. ",[26,894,895,896,899,900,415,905,910],{},"If it functions, the bundler might throw an error like ",[450,897,898],{},"No loader is configured for \".vue\" files",". Essentially, tools like ",[20,901,904],{"href":902,"rel":903},"https:\u002F\u002Fesbuild.github.io",[43],"esbuild",[20,906,909],{"href":907,"rel":908},"https:\u002F\u002Fwww.typescriptlang.org\u002Fdocs\u002Fhandbook\u002Fcompiler-options.html",[43],"tsc"," don't know how to handle Vue files. It's logical since Vue files aren't JavaScript files.",[10,912,913,914,917],{},"The problem becomes evident. We need some configuration to handle Vue files. Let's quickly browse the internet for something like ",[450,915,916],{},"vue loader",", as we're utilizing Vue files and need to transpile them to JavaScript.",[10,919,920,921,926,927],{},"The top result is ",[20,922,925],{"href":923,"rel":924},"https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fvue-loader",[43],"Vue Loader",", which is a Webpack loader. ",[26,928,929],{},"No. I refuse to use Webpack. Vite is now a standard, and there's no justification for employing Webpack in this context.",[10,931,932,933,386,935,943],{},"But before delving further, let's revisit our mantra: ",[67,934,432],{},[26,936,937,938],{},"and ",[20,939,942],{"href":940,"rel":941},"https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FKISS_principle",[43],"keep it simple, stupid!"," Transpiling Vue SFC files using Webpack seems contrary to this principle.",[53,945,947],{"id":946},"ignoring-vue-files","Ignoring Vue files",[10,949,950,951,954,955,959,960,965],{},"To deliver ",[450,952,953],{},".vue"," files without transpiling them, we instruct our bundler to disregard them. This is achieved using a tool called ",[20,956,44],{"href":957,"rel":958},"https:\u002F\u002Funbuild.unjs.io",[43]," from the ",[20,961,964],{"href":962,"rel":963},"https:\u002F\u002Funjs.io",[43],"UnJS"," ecosystem.",[10,967,968],{},"Unbuild is a straightforward tool yet highly configurable since it's constructed atop rollup.",[10,970,971],{},"For our small project, we can directly experiment with it:",[444,973,978],{"className":974,"code":975,"filename":976,"language":977,"meta":197,"style":197},"language-sh shiki shiki-themes github-light github-light github-dark","npx unbuild\n","Build the library","sh",[450,979,980],{"__ignoreMap":197},[453,981,982,985],{"class":455,"line":345},[453,983,984],{"class":466},"npx",[453,986,987],{"class":473}," unbuild\n",[10,989,990],{},"Unfortunately, it fails for the same reasons as before:",[444,992,997],{"className":993,"code":994,"filename":995,"language":996,"meta":197,"style":197},"language-txt shiki shiki-themes github-light github-light github-dark","src\u002Fcomponents\u002FShowGitHubUser.vue (1:0): Expression expected (Note that you need plugins to import files that are not JavaScript)\n","Unbuild error","txt",[450,998,999],{"__ignoreMap":197},[453,1000,1001],{"class":455,"line":345},[453,1002,994],{},[10,1004,1005],{},[26,1006,1007],{},"But unbuild has the potential for much more.",[10,1009,1010,1011,1013],{},"To tackle this problem, let's attempt the obvious solution. In the ",[450,1012,703],{}," file, remove the component export and solely export TypeScript files:",[444,1015,1017],{"className":595,"code":1016,"filename":708,"language":598,"meta":197,"style":197},"export { useUser } from '.\u002Fcomposables\u002FuseUser'\n",[450,1018,1019],{"__ignoreMap":197},[453,1020,1021,1023,1025,1027],{"class":455,"line":345},[453,1022,621],{"class":485},[453,1024,489],{"class":458},[453,1026,492],{"class":485},[453,1028,748],{"class":473},[10,1030,1031,1032,1035],{},"Now, the command ",[450,1033,1034],{},"npx unbuild"," functions properly.",[444,1037,1040],{"className":974,"code":1038,"filename":1039,"language":977,"meta":197,"style":197},"➜ npx unbuild\nℹ Automatically detected entries: src\u002Findex [esm] [dts]\nℹ Building vue-library\nℹ Cleaning dist directory: .\u002Fdist\n✔ Build succeeded for vue-library\n  dist\u002Findex.mjs (total size: 139 B, chunk size: 139 B, exports: useUser)\n\nΣ Total dist size (byte size): 531 B\n","Build output without components",[450,1041,1042,1052,1072,1082,1098,1114,1149,1153],{"__ignoreMap":197},[453,1043,1044,1047,1050],{"class":455,"line":345},[453,1045,1046],{"class":466},"➜",[453,1048,1049],{"class":473}," npx",[453,1051,987],{"class":473},[453,1053,1054,1057,1060,1063,1066,1069],{"class":455,"line":198},[453,1055,1056],{"class":466},"ℹ",[453,1058,1059],{"class":473}," Automatically",[453,1061,1062],{"class":473}," detected",[453,1064,1065],{"class":473}," entries:",[453,1067,1068],{"class":473}," src\u002Findex",[453,1070,1071],{"class":458}," [esm] [dts]\n",[453,1073,1074,1076,1079],{"class":455,"line":354},[453,1075,1056],{"class":466},[453,1077,1078],{"class":473}," Building",[453,1080,1081],{"class":473}," vue-library\n",[453,1083,1084,1086,1089,1092,1095],{"class":455,"line":359},[453,1085,1056],{"class":466},[453,1087,1088],{"class":473}," Cleaning",[453,1090,1091],{"class":473}," dist",[453,1093,1094],{"class":473}," directory:",[453,1096,1097],{"class":473}," .\u002Fdist\n",[453,1099,1100,1103,1106,1109,1112],{"class":455,"line":364},[453,1101,1102],{"class":466},"✔",[453,1104,1105],{"class":473}," Build",[453,1107,1108],{"class":473}," succeeded",[453,1110,1111],{"class":473}," for",[453,1113,1081],{"class":473},[453,1115,1116,1119,1122,1125,1128,1131,1134,1137,1139,1141,1144,1146],{"class":455,"line":369},[453,1117,1118],{"class":466},"  dist\u002Findex.mjs",[453,1120,1121],{"class":458}," (total ",[453,1123,1124],{"class":473},"size:",[453,1126,1127],{"class":511}," 139",[453,1129,1130],{"class":473}," B,",[453,1132,1133],{"class":473}," chunk",[453,1135,1136],{"class":473}," size:",[453,1138,1127],{"class":511},[453,1140,1130],{"class":473},[453,1142,1143],{"class":473}," exports:",[453,1145,520],{"class":473},[453,1147,1148],{"class":458},")\n",[453,1150,1151],{"class":455,"line":374},[453,1152,500],{"emptyLinePlaceholder":211},[453,1154,1155,1158,1161,1163,1166,1169,1172],{"class":455,"line":548},[453,1156,1157],{"class":466},"Σ",[453,1159,1160],{"class":473}," Total",[453,1162,1091],{"class":473},[453,1164,1165],{"class":473}," size",[453,1167,1168],{"class":458}," (byte ",[453,1170,1171],{"class":473},"size",[453,1173,1174],{"class":458},"): 531 B\n",[10,1176,1177,1178,1181,1182,1184],{},"This is a good start, but our components remain in the ",[450,1179,1180],{},"src"," folder and no matter how diligently you search, they're missing from the ",[450,1183,873],{}," folder.",[53,1186,1188],{"id":1187},"copying-vue-files","Copying Vue files",[10,1190,1191,1192,1195,1196,1198,1199,1201,1202,1205],{},"Now that ",[450,1193,1194],{},".ts"," files are processed correctly, we can attempt to copy the ",[450,1197,953],{}," files to the ",[450,1200,873],{}," folder using a simple ",[450,1203,1204],{},"cp"," command.",[444,1207,1210],{"className":974,"code":1208,"filename":1209,"language":977,"meta":197,"style":197},"cp -r src\u002Fcomponents\u002F dist\u002Fcomponents\u002F\n","Copy Vue components",[450,1211,1212],{"__ignoreMap":197},[453,1213,1214,1216,1219,1222],{"class":455,"line":345},[453,1215,1204],{"class":466},[453,1217,1218],{"class":511}," -r",[453,1220,1221],{"class":473}," src\u002Fcomponents\u002F",[453,1223,1224],{"class":473}," dist\u002Fcomponents\u002F\n",[10,1226,1227,1228,1230,1231,1233],{},"Subsequently, we add an ",[450,1229,758],{}," field in the ",[450,1232,754],{}," to inform the user where to locate the components:",[444,1235,1237],{"className":762,"code":1236,"filename":754,"language":764,"meta":197,"style":197},"{\n  \"type\": \"module\",\n  \"exports\": {\n    \".\": {\n      \"types\": \".\u002Fdist\u002Findex.d.ts\",\n      \"import\": \".\u002Fdist\u002Findex.mjs\"\n    },\n    \".\u002Fcomponents\u002F*\": {\n      \"import\": \".\u002Fdist\u002Fcomponents\u002F*.vue\"\n    }\n  },\n  \"main\": \"dist\u002Findex.mjs\",\n  \"types\": \"dist\u002Findex.d.ts\"\n}\n",[450,1238,1239,1243,1253,1259,1265,1275,1283,1288,1295,1304,1308,1312,1323,1332],{"__ignoreMap":197},[453,1240,1241],{"class":455,"line":345},[453,1242,771],{"class":458},[453,1244,1245,1247,1249,1251],{"class":455,"line":198},[453,1246,776],{"class":511},[453,1248,779],{"class":458},[453,1250,782],{"class":473},[453,1252,785],{"class":458},[453,1254,1255,1257],{"class":455,"line":354},[453,1256,790],{"class":511},[453,1258,793],{"class":458},[453,1260,1261,1263],{"class":455,"line":359},[453,1262,798],{"class":511},[453,1264,793],{"class":458},[453,1266,1267,1269,1271,1273],{"class":455,"line":364},[453,1268,805],{"class":511},[453,1270,779],{"class":458},[453,1272,810],{"class":473},[453,1274,785],{"class":458},[453,1276,1277,1279,1281],{"class":455,"line":369},[453,1278,817],{"class":511},[453,1280,779],{"class":458},[453,1282,822],{"class":473},[453,1284,1285],{"class":455,"line":374},[453,1286,1287],{"class":458},"    },\n",[453,1289,1290,1293],{"class":455,"line":548},[453,1291,1292],{"class":511},"    \".\u002Fcomponents\u002F*\"",[453,1294,793],{"class":458},[453,1296,1297,1299,1301],{"class":455,"line":567},[453,1298,817],{"class":511},[453,1300,779],{"class":458},[453,1302,1303],{"class":473},"\".\u002Fdist\u002Fcomponents\u002F*.vue\"\n",[453,1305,1306],{"class":455,"line":573},[453,1307,827],{"class":458},[453,1309,1310],{"class":455,"line":583},[453,1311,832],{"class":458},[453,1313,1315,1317,1319,1321],{"class":455,"line":1314},12,[453,1316,837],{"class":511},[453,1318,779],{"class":458},[453,1320,842],{"class":473},[453,1322,785],{"class":458},[453,1324,1326,1328,1330],{"class":455,"line":1325},13,[453,1327,849],{"class":511},[453,1329,779],{"class":458},[453,1331,854],{"class":473},[453,1333,1335],{"class":455,"line":1334},14,[453,1336,679],{"class":458},[10,1338,1339,1340,1343],{},"Looks promising, doesn't it? ",[26,1341,1342],{},"But it isn't."," 😔",[53,1345,1347],{"id":1346},"the-import-path-dilemma","The import path dilemma",[10,1349,1350,1351,1353],{},"In our Vue component, the ",[450,1352,891],{}," composable is imported like this:",[444,1355,1357],{"className":446,"code":1356,"filename":448,"language":221,"meta":197,"style":197},"\u003Cscript lang=\"ts\" setup>\nimport { useUser } from '..\u002Fcomposables'\n\u003C\u002Fscript>\n",[450,1358,1359,1375,1385],{"__ignoreMap":197},[453,1360,1361,1363,1365,1367,1369,1371,1373],{"class":455,"line":345},[453,1362,459],{"class":458},[453,1364,463],{"class":462},[453,1366,467],{"class":466},[453,1368,470],{"class":458},[453,1370,474],{"class":473},[453,1372,477],{"class":466},[453,1374,480],{"class":458},[453,1376,1377,1379,1381,1383],{"class":455,"line":198},[453,1378,486],{"class":485},[453,1380,489],{"class":458},[453,1382,492],{"class":485},[453,1384,495],{"class":473},[453,1386,1387,1389,1391],{"class":455,"line":354},[453,1388,528],{"class":458},[453,1390,463],{"class":462},[453,1392,480],{"class":458},[10,1394,1395,1396,1398],{},"Meanwhile, our ",[450,1397,873],{}," folder is structured as follows:",[444,1400,1404],{"className":1401,"code":1402,"filename":1403,"language":695,"meta":197},[692],"dist\u002F\n  components\u002F\n    User.vue\n  index.d.mts\n  index.d.ts\n  index.mjs\n","Output with copied components",[450,1405,1402],{"__ignoreMap":197},[10,1407,1408],{},"Can you identify the problem? 👀",[10,1410,1411,1412,1415,1416,1419],{},"The component is unusable and will produce an error like ",[450,1413,1414],{},"Cannot find module '..\u002Fcomposables' or its corresponding type declarations",", because the path leads nowhere. We've bundled all of our TypeScript files into a single file ",[450,1417,1418],{},"index.mjs",", losing the project's structure entirely.",[53,1421,1423],{"id":1422},"to-bundle-or-not-to-bundle","To bundle or not to bundle?",[10,1425,1426],{},"From here, we have two choices:",[61,1428,1429,1440],{},[64,1430,1431,1434,1435,1439],{},[67,1432,1433],{},"Bundling the Vue files"," using ",[20,1436,50],{"href":1437,"rel":1438},"https:\u002F\u002Fvitejs.dev",[43],", plugins, and extensive configuration.",[64,1441,1442,1445,1446,1451],{},[67,1443,1444],{},"Maintaining the directory structure"," and using a tool like ",[20,1447,1450],{"href":1448,"rel":1449},"https:\u002F\u002Fgithub.com\u002Funjs\u002Fmkdist",[43],"mkdist"," for file-to-file transpiling (bundleless build) on TypeScript files.",[10,1453,1454],{},"The decision largely hinges on your specific needs, but I'm convinced that simplicity is the best approach. So, let's explore the second option.",[10,1456,1457,1458,1460,1461,1463,1464,1467],{},"The goal is to transform the TypeScript files into JavaScript files, preserving the original structure of the ",[450,1459,1180],{}," folder in the ",[450,1462,873],{}," folder and ",[26,1465,1466],{},"ignoring"," the Vue files.",[423,1469,1470],{"color":425,"icon":426},[10,1471,1472,1473,1476,1477,1480,1481,1486,1487,51],{},"Using a ",[450,1474,1475],{},"\u003Cscript setup lang=\"ts\">"," block, mkdist will ignore Vue files to enable the Vue compiler to generate runtime props. If not, it will generate a ",[450,1478,1479],{},".vue.d.ts"," file to offer types for the Vue component. Remember that the ",[20,1482,1485],{"href":1483,"rel":1484},"https:\u002F\u002Fgithub.com\u002Fvitejs\u002Fvite-plugin-vue\u002Ftree\u002Fmain\u002Fpackages\u002Fplugin-vue",[43],"@vitejs\u002Fplugin-vue"," and Vite inherently understand TypeScript script blocks. For more details, refer to ",[20,1488,1491],{"href":1489,"rel":1490},"https:\u002F\u002Fgithub.com\u002Funjs\u002Fmkdist\u002Fissues\u002F14",[43],"issue mkdist#14",[53,1493,1495],{"id":1494},"configuring-unbuild","Configuring Unbuild",[10,1497,1498],{},"Yes, we will configure unbuild since it incorporates mkdist, making it exceptionally user-friendly.",[10,1500,1501,1502,1505,1506,1508],{},"First, create a ",[450,1503,1504],{},"build.config.ts"," file at the project's root. Unbuild will read this configuration file to understand how to process the project. It assumes defaults and infers many aspects from the ",[450,1507,754],{},", but we need to tell it to utilize mkdist.",[444,1510,1512],{"className":595,"code":1511,"filename":1504,"language":598,"meta":197,"style":197},"import { defineBuildConfig } from 'unbuild'\n\nexport default defineBuildConfig({\n  entries: ['.\u002Fsrc\u002F'],\n  declaration: true,\n})\n",[450,1513,1514,1526,1530,1543,1554,1564],{"__ignoreMap":197},[453,1515,1516,1518,1521,1523],{"class":455,"line":345},[453,1517,486],{"class":485},[453,1519,1520],{"class":458}," { defineBuildConfig } ",[453,1522,492],{"class":485},[453,1524,1525],{"class":473}," 'unbuild'\n",[453,1527,1528],{"class":455,"line":198},[453,1529,500],{"emptyLinePlaceholder":211},[453,1531,1532,1534,1537,1540],{"class":455,"line":354},[453,1533,621],{"class":485},[453,1535,1536],{"class":485}," default",[453,1538,1539],{"class":466}," defineBuildConfig",[453,1541,1542],{"class":458},"({\n",[453,1544,1545,1548,1551],{"class":455,"line":359},[453,1546,1547],{"class":458},"  entries: [",[453,1549,1550],{"class":473},"'.\u002Fsrc\u002F'",[453,1552,1553],{"class":458},"],\n",[453,1555,1556,1559,1562],{"class":455,"line":364},[453,1557,1558],{"class":458},"  declaration: ",[453,1560,1561],{"class":511},"true",[453,1563,785],{"class":458},[453,1565,1566],{"class":455,"line":369},[453,1567,1568],{"class":458},"})\n",[10,1570,1571,1572,1575,1576,1579,1580,1583,1584,1587,1588,1591,1592,1594,1595,1598,1599,1602],{},"It's a very straightforward configuration file. ",[26,1573,1574],{},"And I adore it."," The ",[450,1577,1578],{},"entries"," key informs unbuild of the initial file to begin transpiling. However, ",[450,1581,1582],{},".\u002Fsrc\u002F"," is a directory, indicated by the ",[450,1585,1586],{},"\u002F"," at the end. With this hint, unbuild bypasses the default bundler ",[450,1589,1590],{},"rollup"," in favor of ",[450,1593,1450],{},". The ",[450,1596,1597],{},"declaration"," key prompts unbuild to create TypeScript declaration files (",[450,1600,1601],{},".d.ts",").",[10,1604,1605,1606,1230,1608,1610,1611,1613],{},"We can also remove the ",[450,1607,758],{},[450,1609,754],{}," since we won't be bundling files anymore and restore the component export in the ",[450,1612,703],{}," file.",[10,1615,1616,1617,1619],{},"Let's rerun the ",[450,1618,1034],{}," command and witness the magic.",[444,1621,1624],{"className":974,"code":1622,"filename":1623,"language":977,"meta":197,"style":197},"➜ npx unbuild\nℹ Building vue-library\nℹ Cleaning dist directory: .\u002Fdist\n✔ Build succeeded for vue-library\n  dist (total size: 600 B)\n  └─ dist\u002Findex.d.ts (108 B)\n  └─ dist\u002Findex.mjs (112 B)\n  └─ dist\u002Fcomposables\u002FuseUser.d.ts (79 B)\n  └─ dist\u002Fcomposables\u002FuseUser.mjs (124 B)\n  └─ dist\u002Fcomponents\u002FUser.vue (177 B)\nΣ Total dist size (byte size): 600 B\n","Bundleless build output",[450,1625,1626,1634,1642,1654,1666,1683,1699,1713,1727,1741,1755],{"__ignoreMap":197},[453,1627,1628,1630,1632],{"class":455,"line":345},[453,1629,1046],{"class":466},[453,1631,1049],{"class":473},[453,1633,987],{"class":473},[453,1635,1636,1638,1640],{"class":455,"line":198},[453,1637,1056],{"class":466},[453,1639,1078],{"class":473},[453,1641,1081],{"class":473},[453,1643,1644,1646,1648,1650,1652],{"class":455,"line":354},[453,1645,1056],{"class":466},[453,1647,1088],{"class":473},[453,1649,1091],{"class":473},[453,1651,1094],{"class":473},[453,1653,1097],{"class":473},[453,1655,1656,1658,1660,1662,1664],{"class":455,"line":359},[453,1657,1102],{"class":466},[453,1659,1105],{"class":473},[453,1661,1108],{"class":473},[453,1663,1111],{"class":473},[453,1665,1081],{"class":473},[453,1667,1668,1671,1673,1675,1678,1681],{"class":455,"line":364},[453,1669,1670],{"class":466},"  dist",[453,1672,1121],{"class":458},[453,1674,1124],{"class":473},[453,1676,1677],{"class":511}," 600",[453,1679,1680],{"class":473}," B",[453,1682,1148],{"class":458},[453,1684,1685,1688,1691,1694,1697],{"class":455,"line":369},[453,1686,1687],{"class":466},"  └─",[453,1689,1690],{"class":473}," dist\u002Findex.d.ts",[453,1692,1693],{"class":458}," (108 ",[453,1695,1696],{"class":473},"B",[453,1698,1148],{"class":458},[453,1700,1701,1703,1706,1709,1711],{"class":455,"line":374},[453,1702,1687],{"class":466},[453,1704,1705],{"class":473}," dist\u002Findex.mjs",[453,1707,1708],{"class":458}," (112 ",[453,1710,1696],{"class":473},[453,1712,1148],{"class":458},[453,1714,1715,1717,1720,1723,1725],{"class":455,"line":548},[453,1716,1687],{"class":466},[453,1718,1719],{"class":473}," dist\u002Fcomposables\u002FuseUser.d.ts",[453,1721,1722],{"class":458}," (79 ",[453,1724,1696],{"class":473},[453,1726,1148],{"class":458},[453,1728,1729,1731,1734,1737,1739],{"class":455,"line":567},[453,1730,1687],{"class":466},[453,1732,1733],{"class":473}," dist\u002Fcomposables\u002FuseUser.mjs",[453,1735,1736],{"class":458}," (124 ",[453,1738,1696],{"class":473},[453,1740,1148],{"class":458},[453,1742,1743,1745,1748,1751,1753],{"class":455,"line":573},[453,1744,1687],{"class":466},[453,1746,1747],{"class":473}," dist\u002Fcomponents\u002FUser.vue",[453,1749,1750],{"class":458}," (177 ",[453,1752,1696],{"class":473},[453,1754,1148],{"class":458},[453,1756,1757,1759,1761,1763,1765,1767,1769],{"class":455,"line":583},[453,1758,1157],{"class":466},[453,1760,1160],{"class":473},[453,1762,1091],{"class":473},[453,1764,1165],{"class":473},[453,1766,1168],{"class":458},[453,1768,1171],{"class":473},[453,1770,1771],{"class":458},"): 600 B\n",[10,1773,1774,1775,1777],{},"Now, the ",[450,1776,873],{}," folder appears like this:",[444,1779,1783],{"className":1780,"code":1781,"filename":1782,"language":695,"meta":197},[692],"dist\u002F\n  components\u002F\n    User.vue\n  composables\u002F\n    useUser.d.ts\n    useUser.mjs\n  index.d.ts\n  index.mjs\n","Bundleless output structure",[450,1784,1781],{"__ignoreMap":197},[10,1786,1787,1788,1790,1791,1794,1795,1797],{},"This mirrors the structure of the ",[450,1789,1180],{}," folder, and the ",[450,1792,1793],{},"User.vue"," component is now usable in the ",[450,1796,873],{}," folder. The relative import path remains intact, and the component can be utilized in any Vue project. :partying_face:",[53,1799,1801],{"id":1800},"local-development","Local development",[10,1803,1804,1805,1808],{},"Many tutorials halt here, suggesting you're ready to publish your package to npm. But how can you create a complex library if you can't ",[67,1806,1807],{},"see the outcome of your work","? If you can't test it while building it?",[10,1810,1811],{},"Basically, you can't.",[10,1813,1814],{},"Let's examine how to use the library in a local project, within the same repository for simplicity.",[10,1816,1817,1818,51],{},"I'll demonstrate the simplest method using a ",[20,1819,1822],{"href":1820,"rel":1821},"https:\u002F\u002Fpnpm.io\u002F",[43],"pnpm workspace",[10,1824,1825,1826,1829],{},"Start by creating a ",[450,1827,1828],{},"pnpm-workspace.yaml"," file at the project's root:",[444,1831,1835],{"className":1832,"code":1833,"filename":1828,"language":1834,"meta":197,"style":197},"language-yaml shiki shiki-themes github-light github-light github-dark","packages:\n  - .\n  - playground\n","yaml",[450,1836,1837,1845,1853],{"__ignoreMap":197},[453,1838,1839,1842],{"class":455,"line":345},[453,1840,1841],{"class":462},"packages",[453,1843,1844],{"class":458},":\n",[453,1846,1847,1850],{"class":455,"line":198},[453,1848,1849],{"class":458},"  - ",[453,1851,1852],{"class":511},".\n",[453,1854,1855,1857],{"class":455,"line":354},[453,1856,1849],{"class":458},[453,1858,1859],{"class":473},"playground\n",[10,1861,1862,1863,1866],{},"Then, create a new Vite project in a ",[450,1864,1865],{},"playground"," folder:",[444,1868,1871],{"className":974,"code":1869,"filename":1870,"language":977,"meta":197,"style":197},"npx create-vite playground --template vue-ts\n","Create the playground",[450,1872,1873],{"__ignoreMap":197},[453,1874,1875,1877,1880,1883,1886],{"class":455,"line":345},[453,1876,984],{"class":466},[453,1878,1879],{"class":473}," create-vite",[453,1881,1882],{"class":473}," playground",[453,1884,1885],{"class":511}," --template",[453,1887,1888],{"class":473}," vue-ts\n",[10,1890,1891],{},"Since we're using a pnpm workspace, install the dependencies from the project's root:",[444,1893,1896],{"className":974,"code":1894,"filename":1895,"language":977,"meta":197,"style":197},"pnpm install\n","Install workspace dependencies",[450,1897,1898],{"__ignoreMap":197},[453,1899,1900,1903],{"class":455,"line":345},[453,1901,1902],{"class":466},"pnpm",[453,1904,1905],{"class":473}," install\n",[423,1907,1908],{"color":425,"icon":426},[10,1909,1910,1911,1914],{},"This step isn't mandatory. You can create a new Vite project and install dependencies within it. Pnpm simplifies this by installing dependencies for all workspaces in the root. A simple ",[450,1912,1913],{},"pnpm install"," installs dependencies from all workspaces.",[10,1916,1917,1918,1920],{},"Now, we can use this ",[450,1919,1865],{}," library within a \"real\" project to test our library.",[10,1922,1923,1924,1927,1928,1931],{},"For instance, open the ",[450,1925,1926],{},"src\u002FApp.vue"," file and import the ",[450,1929,1930],{},"User"," component:",[444,1933,1936],{"className":446,"code":1934,"filename":1935,"language":221,"meta":197,"style":197},"\u003Cscript setup lang=\"ts\">\nimport { User } from '..\u002F..\u002Fsrc'\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003CUser \u002F>\n\u003C\u002Ftemplate>\n","playground\u002Fsrc\u002FApp.vue",[450,1937,1938,1954,1966,1974,1978,1986,1995],{"__ignoreMap":197},[453,1939,1940,1942,1944,1946,1948,1950,1952],{"class":455,"line":345},[453,1941,459],{"class":458},[453,1943,463],{"class":462},[453,1945,477],{"class":466},[453,1947,467],{"class":466},[453,1949,470],{"class":458},[453,1951,474],{"class":473},[453,1953,480],{"class":458},[453,1955,1956,1958,1961,1963],{"class":455,"line":198},[453,1957,486],{"class":485},[453,1959,1960],{"class":458}," { User } ",[453,1962,492],{"class":485},[453,1964,1965],{"class":473}," '..\u002F..\u002Fsrc'\n",[453,1967,1968,1970,1972],{"class":455,"line":354},[453,1969,528],{"class":458},[453,1971,463],{"class":462},[453,1973,480],{"class":458},[453,1975,1976],{"class":455,"line":359},[453,1977,500],{"emptyLinePlaceholder":211},[453,1979,1980,1982,1984],{"class":455,"line":364},[453,1981,459],{"class":458},[453,1983,543],{"class":462},[453,1985,480],{"class":458},[453,1987,1988,1990,1992],{"class":455,"line":369},[453,1989,551],{"class":458},[453,1991,1930],{"class":462},[453,1993,1994],{"class":458}," \u002F>\n",[453,1996,1997,1999,2001],{"class":455,"line":374},[453,1998,528],{"class":458},[453,2000,543],{"class":462},[453,2002,480],{"class":458},[10,2004,2005],{},"And run the project:",[444,2007,2010],{"className":974,"code":2008,"filename":2009,"language":977,"meta":197,"style":197},"cd playground && pnpm dev\n","Start the playground",[450,2011,2012],{"__ignoreMap":197},[453,2013,2014,2017,2019,2022,2024],{"class":455,"line":345},[453,2015,2016],{"class":511},"cd",[453,2018,1882],{"class":473},[453,2020,2021],{"class":458}," && ",[453,2023,1902],{"class":466},[453,2025,2026],{"class":473}," dev\n",[10,2028,2029,2030,2032,2033],{},"You'll see the ",[450,2031,1930],{}," component displayed in the browser. 🎉 So simple, yet so powerful. ",[26,2034,2035],{},"I use this method for virtually every library I build, and it works brilliantly.",[53,2037,2039],{"id":2038},"publishing-to-npm","Publishing to npm",[10,2041,2042],{},"This part is straightforward once you grasp the workflow.",[10,2044,2045,2046,2049],{},"Begin by naming your library. I'll use ",[450,2047,2048],{},"@barbapapazes\u002Fvue-library"," for this example.",[10,2051,2052,2053,2058,2059,1205],{},"Next, create an account on ",[20,2054,2057],{"href":2055,"rel":2056},"https:\u002F\u002Fwww.npmjs.com",[43],"npm"," and log in using the ",[450,2060,2061],{},"npm login",[10,2063,2064,2065,2070],{},"Then, install ",[20,2066,2069],{"href":2067,"rel":2068},"https:\u002F\u002Fchangelogen.unjs.io",[43],"changelogen"," to generate a changelog, update the version number following semantic versioning and commit convention, and to pre-fill the GitHub release.",[444,2072,2075],{"className":974,"code":2073,"filename":2074,"language":977,"meta":197,"style":197},"pnpm i -D changelogen\n","Install Changelogen",[450,2076,2077],{"__ignoreMap":197},[453,2078,2079,2081,2084,2087],{"class":455,"line":345},[453,2080,1902],{"class":466},[453,2082,2083],{"class":473}," i",[453,2085,2086],{"class":511}," -D",[453,2088,2089],{"class":473}," changelogen\n",[10,2091,2092,2093,759],{},"Next, add two scripts to your ",[450,2094,754],{},[444,2096,2098],{"className":762,"code":2097,"filename":754,"language":764,"meta":197,"style":197},"{\n  \"scripts\": {\n    \"prepack\": \"unbuild\",\n    \"release\": \"changelogen --release && npm publish --access public && git push --follow-tags\"\n  }\n}\n",[450,2099,2100,2104,2111,2123,2133,2137],{"__ignoreMap":197},[453,2101,2102],{"class":455,"line":345},[453,2103,771],{"class":458},[453,2105,2106,2109],{"class":455,"line":198},[453,2107,2108],{"class":511},"  \"scripts\"",[453,2110,793],{"class":458},[453,2112,2113,2116,2118,2121],{"class":455,"line":354},[453,2114,2115],{"class":511},"    \"prepack\"",[453,2117,779],{"class":458},[453,2119,2120],{"class":473},"\"unbuild\"",[453,2122,785],{"class":458},[453,2124,2125,2128,2130],{"class":455,"line":359},[453,2126,2127],{"class":511},"    \"release\"",[453,2129,779],{"class":458},[453,2131,2132],{"class":473},"\"changelogen --release && npm publish --access public && git push --follow-tags\"\n",[453,2134,2135],{"class":455,"line":364},[453,2136,674],{"class":458},[453,2138,2139],{"class":455,"line":369},[453,2140,679],{"class":458},[10,2142,700,2143,2146,2147,2149,2150,2153],{},[450,2144,2145],{},"prepack"," script runs the ",[450,2148,44],{}," command before publishing the package to npm. The ",[450,2151,2152],{},"release"," script generates a changelog, publishes the package to npm, and pushes the tags to GitHub.",[423,2155,2156],{"color":425,"icon":426},[10,2157,2158,2159,2162],{},"Your project should be on GitHub for the ",[450,2160,2161],{},"--release"," option to work. If not, omit it and create the release manually on your repository.",[10,2164,2165],{},"Now, publish your package to npm using the following command:",[444,2167,2170],{"className":974,"code":2168,"filename":2169,"language":977,"meta":197,"style":197},"npm run release\n","Publish the library",[450,2171,2172],{"__ignoreMap":197},[453,2173,2174,2176,2179],{"class":455,"line":345},[453,2175,2057],{"class":466},[453,2177,2178],{"class":473}," run",[453,2180,2181],{"class":473}," release\n",[10,2183,2184],{},"And that's it! You've just published your first Vue.js component library to npm. 🚀",[53,2186,2188],{"id":2187},"everything-is-ready","Everything is ready",[10,2190,2191],{},"We're done for today. We've covered numerous topics:",[2193,2194,2195,2198,2201,2204,2207,2210],"ul",{},[64,2196,2197],{},"How to build a Vue.js component library with TypeScript.",[64,2199,2200],{},"How to manage Vue files in a TypeScript project.",[64,2202,2203],{},"How to use unbuild to transpile TypeScript files without bundling Vue files.",[64,2205,2206],{},"How to leverage mkdist to maintain the project structure.",[64,2208,2209],{},"How to utilize a pnpm workspace to test the library in a local project.",[64,2211,2212],{},"How to publish the library to npm.",[10,2214,2215,2216,2218,2219,51],{},"For a more detailed and complex example, check GitHub: ",[383,2217],{"repo":385}," but everything explained here stems from ",[67,2220,2221],{},"my real-world experience",[10,2223,2224,2225,51],{},"I hope you enjoyed this tutorial and that it helps you build your own Vue.js component library. If you have questions, feel free to reach out to me on ",[20,2226,2229],{"href":2227,"rel":2228},"https:\u002F\u002Fx.com\u002Fsoubiran_",[43],"X (Twitter)",[10,2231,2232],{},[26,2233,2234],{},"You can go even further by using Vite to build your component library:",[30,2236],{"id":202},[2238,2239,2240],"style",{},"html pre.shiki code .sxrX7, html code.shiki .sxrX7{--shiki-light:#24292E;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sovSZ, html code.shiki .sovSZ{--shiki-light:#22863A;--shiki-default:#22863A;--shiki-dark:#85E89D}html pre.shiki code .snPdu, html code.shiki .snPdu{--shiki-light:#6F42C1;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sIIMD, html code.shiki .sIIMD{--shiki-light:#032F62;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s8jYJ, html code.shiki .s8jYJ{--shiki-light:#D73A49;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sBjJW, html code.shiki .sBjJW{--shiki-light:#005CC5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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":197,"searchDepth":198,"depth":198,"links":2242},[2243,2244,2245,2246,2247,2248,2249,2250,2251],{"id":861,"depth":198,"text":862},{"id":946,"depth":198,"text":947},{"id":1187,"depth":198,"text":1188},{"id":1346,"depth":198,"text":1347},{"id":1422,"depth":198,"text":1423},{"id":1494,"depth":198,"text":1495},{"id":1800,"depth":198,"text":1801},{"id":2038,"depth":198,"text":2039},{"id":2187,"depth":198,"text":2188},"2024-09-24","Manage the complexities of Vue SFCs with powerful tools like unbuild and mkdist to create a component library, saving you time and effort.",{},"---\ncontentId: f9754148-40bd-4df2-a144-f686e894b200\ntitle: The Simplest Method to Create a Vue.js Component Library\ndescription: Manage the complexities of Vue SFCs with powerful tools like unbuild and mkdist to create a component library, saving you time and effort.\ndate: 2024-09-24\n---\n\nTL;DR: \u003CGitHubLink repo=\"barbapapazes\u002Fvue-library\" \u002F> _Continue reading to grasp the process behind it and understand the reasons for my choices._\n\nA component library is a collection of reusable pieces that can be employed in various projects. It facilitates sharing resources among different projects and teams. These components may be low-level and generic like buttons, inputs, and modals, or more business-specific modules. _Ultimately, it's a method to share code across projects, a crucial skill that can significantly save time._\n\nHowever, building a component library with [Vue.js](https:\u002F\u002Fvuejs.org) is harder than it seems because of the [Single File Components](https:\u002F\u002Fvuejs.org\u002Fguide\u002Fscaling-up\u002Fsfc.html) (SFC). In a standard TypeScript project, you usually transpile to JavaScript and bundle your files with a tool like [tsup](https:\u002F\u002Ftsup.egoist.dev\u002F) or [Vite](https:\u002F\u002Fvitejs.dev\u002F).\n\n\u003CCalloutReadMore id=\"55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1\" \u002F>\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nRemember to **deliver package code as native as possible** and let userland tools handle transpiling and optimization. It's a principle I always keep in mind when crafting libraries, streamlining the process significantly. _Each case is unique, but this is a solid guideline to follow._\n::\nThe use of Vue.js SFC complicates this process. Let's examine why by exploring the challenges you'll encounter when constructing a Vue.js component library.\n\nConsider the following component:\n\n```vue [src\u002Fcomponents\u002FUser.vue]\n\u003Cscript lang=\"ts\" setup>\nimport { useUser } from '..\u002Fcomposables'\n\nconst { user } = useUser()\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003Cdiv v-if=\"user\">\n    {{ user.name }}\n  \u003C\u002Fdiv>\n\u003C\u002Ftemplate>\n```\n\nThe composable is as follows:\n\n```ts [src\u002Fcomposables\u002FuseUser.ts]\nimport { ref } from 'vue'\n\nexport function useUser() {\n  const user = ref({ name: 'John Doe' })\n\n  return {\n    user\n  }\n}\n```\n\nThe component employs a composable imported from another file. _It may seem trivial, but this poses a significant issue._\n\nAll of this follows the architecture below:\n\n```text [Project structure]\nsrc\u002F\n  components\u002F\n    User.vue\n  composables\u002F\n    useUser.ts\n  index.ts\n```\n\nThe `index.ts` file acts as the library's entry point and it looks like this:\n\n```ts [src\u002Findex.ts]\nimport User from '.\u002Fcomponents\u002FUser.vue'\n\nexport { User }\n\nexport { useUser } from '.\u002Fcomposables\u002FuseUser'\n```\n\nPlus, the `package.json` includes some `exports`:\n\n```json [package.json]\n{\n  \"type\": \"module\",\n  \"exports\": {\n    \".\": {\n      \"types\": \".\u002Fdist\u002Findex.d.ts\",\n      \"import\": \".\u002Fdist\u002Findex.mjs\"\n    }\n  },\n  \"main\": \"dist\u002Findex.mjs\",\n  \"types\": \"dist\u002Findex.d.ts\"\n}\n```\n\n## Similar to a standard TypeScript project\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nA bundle is a single file encapsulating all your project's code. It optimizes project loading by minimizing the number of files to load. This bundle isn't transpiled (except TypeScript to JavaScript) or minified when creating an npm package.\n::\nNow, if we attempt to bundle this project, our `dist` folder will appear like this:\n\n```text [Bundled output]\ndist\u002F\n  index.cjs\n```\n\nUpon inspecting `index.cjs`, you'll find a surprise: the `useUser` composable is inlined, but the component is entirely ignored. _If it functions, the bundler might throw an error like `No loader is configured for \".vue\" files`. Essentially, tools like [esbuild](https:\u002F\u002Fesbuild.github.io) or [tsc](https:\u002F\u002Fwww.typescriptlang.org\u002Fdocs\u002Fhandbook\u002Fcompiler-options.html) don't know how to handle Vue files. It's logical since Vue files aren't JavaScript files._\n\nThe problem becomes evident. We need some configuration to handle Vue files. Let's quickly browse the internet for something like `vue loader`, as we're utilizing Vue files and need to transpile them to JavaScript.\n\nThe top result is [Vue Loader](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fvue-loader), which is a Webpack loader. _No. I refuse to use Webpack. Vite is now a standard, and there's no justification for employing Webpack in this context._\n\nBut before delving further, let's revisit our mantra: **deliver package code as native as possible** _and [keep it simple, stupid!](https:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FKISS_principle)_ Transpiling Vue SFC files using Webpack seems contrary to this principle.\n\n## Ignoring Vue files\n\nTo deliver `.vue` files without transpiling them, we instruct our bundler to disregard them. This is achieved using a tool called [unbuild](https:\u002F\u002Funbuild.unjs.io) from the [UnJS](https:\u002F\u002Funjs.io) ecosystem.\n\nUnbuild is a straightforward tool yet highly configurable since it's constructed atop rollup.\n\nFor our small project, we can directly experiment with it:\n\n```sh [Build the library]\nnpx unbuild\n```\n\nUnfortunately, it fails for the same reasons as before:\n\n```txt [Unbuild error]\nsrc\u002Fcomponents\u002FShowGitHubUser.vue (1:0): Expression expected (Note that you need plugins to import files that are not JavaScript)\n```\n\n_But unbuild has the potential for much more._\n\nTo tackle this problem, let's attempt the obvious solution. In the `index.ts` file, remove the component export and solely export TypeScript files:\n\n```ts [src\u002Findex.ts]\nexport { useUser } from '.\u002Fcomposables\u002FuseUser'\n```\n\nNow, the command `npx unbuild` functions properly.\n\n```sh [Build output without components]\n➜ npx unbuild\nℹ Automatically detected entries: src\u002Findex [esm] [dts]\nℹ Building vue-library\nℹ Cleaning dist directory: .\u002Fdist\n✔ Build succeeded for vue-library\n  dist\u002Findex.mjs (total size: 139 B, chunk size: 139 B, exports: useUser)\n\nΣ Total dist size (byte size): 531 B\n```\n\nThis is a good start, but our components remain in the `src` folder and no matter how diligently you search, they're missing from the `dist` folder.\n\n## Copying Vue files\n\nNow that `.ts` files are processed correctly, we can attempt to copy the `.vue` files to the `dist` folder using a simple `cp` command.\n\n```sh [Copy Vue components]\ncp -r src\u002Fcomponents\u002F dist\u002Fcomponents\u002F\n```\n\nSubsequently, we add an `exports` field in the `package.json` to inform the user where to locate the components:\n\n```json [package.json]\n{\n  \"type\": \"module\",\n  \"exports\": {\n    \".\": {\n      \"types\": \".\u002Fdist\u002Findex.d.ts\",\n      \"import\": \".\u002Fdist\u002Findex.mjs\"\n    },\n    \".\u002Fcomponents\u002F*\": {\n      \"import\": \".\u002Fdist\u002Fcomponents\u002F*.vue\"\n    }\n  },\n  \"main\": \"dist\u002Findex.mjs\",\n  \"types\": \"dist\u002Findex.d.ts\"\n}\n```\n\nLooks promising, doesn't it? _But it isn't._ :pensive:\n\n## The import path dilemma\n\nIn our Vue component, the `useUser` composable is imported like this:\n\n```vue [src\u002Fcomponents\u002FUser.vue]\n\u003Cscript lang=\"ts\" setup>\nimport { useUser } from '..\u002Fcomposables'\n\u003C\u002Fscript>\n```\n\nMeanwhile, our `dist` folder is structured as follows:\n\n```text [Output with copied components]\ndist\u002F\n  components\u002F\n    User.vue\n  index.d.mts\n  index.d.ts\n  index.mjs\n```\n\nCan you identify the problem? :eyes:\n\nThe component is unusable and will produce an error like `Cannot find module '..\u002Fcomposables' or its corresponding type declarations`, because the path leads nowhere. We've bundled all of our TypeScript files into a single file `index.mjs`, losing the project's structure entirely.\n\n## To bundle or not to bundle?\n\nFrom here, we have two choices:\n\n1. **Bundling the Vue files** using [Vite](https:\u002F\u002Fvitejs.dev), plugins, and extensive configuration.\n2. **Maintaining the directory structure** and using a tool like [mkdist](https:\u002F\u002Fgithub.com\u002Funjs\u002Fmkdist) for file-to-file transpiling (bundleless build) on TypeScript files.\n\nThe decision largely hinges on your specific needs, but I'm convinced that simplicity is the best approach. So, let's explore the second option.\n\nThe goal is to transform the TypeScript files into JavaScript files, preserving the original structure of the `src` folder in the `dist` folder and _ignoring_ the Vue files.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nUsing a `\u003Cscript setup lang=\"ts\">` block, mkdist will ignore Vue files to enable the Vue compiler to generate runtime props. If not, it will generate a `.vue.d.ts` file to offer types for the Vue component. Remember that the [@vitejs\u002Fplugin-vue](https:\u002F\u002Fgithub.com\u002Fvitejs\u002Fvite-plugin-vue\u002Ftree\u002Fmain\u002Fpackages\u002Fplugin-vue) and Vite inherently understand TypeScript script blocks. For more details, refer to [issue mkdist#14](https:\u002F\u002Fgithub.com\u002Funjs\u002Fmkdist\u002Fissues\u002F14).\n::\n## Configuring Unbuild\n\nYes, we will configure unbuild since it incorporates mkdist, making it exceptionally user-friendly.\n\nFirst, create a `build.config.ts` file at the project's root. Unbuild will read this configuration file to understand how to process the project. It assumes defaults and infers many aspects from the `package.json`, but we need to tell it to utilize mkdist.\n\n```ts [build.config.ts]\nimport { defineBuildConfig } from 'unbuild'\n\nexport default defineBuildConfig({\n  entries: ['.\u002Fsrc\u002F'],\n  declaration: true,\n})\n```\n\nIt's a very straightforward configuration file. _And I adore it._ The `entries` key informs unbuild of the initial file to begin transpiling. However, `.\u002Fsrc\u002F` is a directory, indicated by the `\u002F` at the end. With this hint, unbuild bypasses the default bundler `rollup` in favor of `mkdist`. The `declaration` key prompts unbuild to create TypeScript declaration files (`.d.ts`).\n\nWe can also remove the `exports` field in the `package.json` since we won't be bundling files anymore and restore the component export in the `index.ts` file.\n\nLet's rerun the `npx unbuild` command and witness the magic.\n\n```sh [Bundleless build output]\n➜ npx unbuild\nℹ Building vue-library\nℹ Cleaning dist directory: .\u002Fdist\n✔ Build succeeded for vue-library\n  dist (total size: 600 B)\n  └─ dist\u002Findex.d.ts (108 B)\n  └─ dist\u002Findex.mjs (112 B)\n  └─ dist\u002Fcomposables\u002FuseUser.d.ts (79 B)\n  └─ dist\u002Fcomposables\u002FuseUser.mjs (124 B)\n  └─ dist\u002Fcomponents\u002FUser.vue (177 B)\nΣ Total dist size (byte size): 600 B\n```\n\nNow, the `dist` folder appears like this:\n\n```text [Bundleless output structure]\ndist\u002F\n  components\u002F\n    User.vue\n  composables\u002F\n    useUser.d.ts\n    useUser.mjs\n  index.d.ts\n  index.mjs\n```\n\nThis mirrors the structure of the `src` folder, and the `User.vue` component is now usable in the `dist` folder. The relative import path remains intact, and the component can be utilized in any Vue project. :partying_face:\n\n## Local development\n\nMany tutorials halt here, suggesting you're ready to publish your package to npm. But how can you create a complex library if you can't **see the outcome of your work**? If you can't test it while building it?\n\nBasically, you can't.\n\nLet's examine how to use the library in a local project, within the same repository for simplicity.\n\nI'll demonstrate the simplest method using a [pnpm workspace](https:\u002F\u002Fpnpm.io\u002F).\n\nStart by creating a `pnpm-workspace.yaml` file at the project's root:\n\n```yaml [pnpm-workspace.yaml]\npackages:\n  - .\n  - playground\n```\n\nThen, create a new Vite project in a `playground` folder:\n\n```sh [Create the playground]\nnpx create-vite playground --template vue-ts\n```\n\nSince we're using a pnpm workspace, install the dependencies from the project's root:\n\n```sh [Install workspace dependencies]\npnpm install\n```\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nThis step isn't mandatory. You can create a new Vite project and install dependencies within it. Pnpm simplifies this by installing dependencies for all workspaces in the root. A simple `pnpm install` installs dependencies from all workspaces.\n::\nNow, we can use this `playground` library within a \"real\" project to test our library.\n\nFor instance, open the `src\u002FApp.vue` file and import the `User` component:\n\n```vue [playground\u002Fsrc\u002FApp.vue]\n\u003Cscript setup lang=\"ts\">\nimport { User } from '..\u002F..\u002Fsrc'\n\u003C\u002Fscript>\n\n\u003Ctemplate>\n  \u003CUser \u002F>\n\u003C\u002Ftemplate>\n```\n\nAnd run the project:\n\n```sh [Start the playground]\ncd playground && pnpm dev\n```\n\nYou'll see the `User` component displayed in the browser. :tada: So simple, yet so powerful. _I use this method for virtually every library I build, and it works brilliantly._\n\n## Publishing to npm\n\nThis part is straightforward once you grasp the workflow.\n\nBegin by naming your library. I'll use `@barbapapazes\u002Fvue-library` for this example.\n\nNext, create an account on [npm](https:\u002F\u002Fwww.npmjs.com) and log in using the `npm login` command.\n\nThen, install [changelogen](https:\u002F\u002Fchangelogen.unjs.io) to generate a changelog, update the version number following semantic versioning and commit convention, and to pre-fill the GitHub release.\n\n```sh [Install Changelogen]\npnpm i -D changelogen\n```\n\nNext, add two scripts to your `package.json`:\n\n```json [package.json]\n{\n  \"scripts\": {\n    \"prepack\": \"unbuild\",\n    \"release\": \"changelogen --release && npm publish --access public && git push --follow-tags\"\n  }\n}\n```\n\nThe `prepack` script runs the `unbuild` command before publishing the package to npm. The `release` script generates a changelog, publishes the package to npm, and pushes the tags to GitHub.\n\n::callout{icon=\"i-ph-info-duotone\" color=\"info\"}\nYour project should be on GitHub for the `--release` option to work. If not, omit it and create the release manually on your repository.\n::\nNow, publish your package to npm using the following command:\n\n```sh [Publish the library]\nnpm run release\n```\n\nAnd that's it! You've just published your first Vue.js component library to npm. :rocket:\n\n## Everything is ready\n\nWe're done for today. We've covered numerous topics:\n\n- How to build a Vue.js component library with TypeScript.\n- How to manage Vue files in a TypeScript project.\n- How to use unbuild to transpile TypeScript files without bundling Vue files.\n- How to leverage mkdist to maintain the project structure.\n- How to utilize a pnpm workspace to test the library in a local project.\n- How to publish the library to npm.\n\nFor a more detailed and complex example, check GitHub: \u003CGitHubLink repo=\"barbapapazes\u002Fvue-library\" \u002F> but everything explained here stems from **my real-world experience**.\n\nI hope you enjoyed this tutorial and that it helps you build your own Vue.js component library. If you have questions, feel free to reach out to me on [X (Twitter)](https:\u002F\u002Fx.com\u002Fsoubiran_).\n\n_You can go even further by using Vite to build your component library:_\n\n\u003CCalloutReadMore id=\"55d2cdcf-ba27-4bfe-9bc1-492e866ff3b1\" \u002F>\n",{"title":23,"description":2253},{"loc":22},"posts\u002Fthe-simplest-method-to-create-a-vue-js-component-library","fl1Azx_QYu0xi71aAzUy1yqsnhmA8cSXhTATEINPWwo",[2261,2262,2263,2264,2265,2266,2267],{"contentId":342,"title":69,"description":343,"path":344,"part":345,"topics":212},{"contentId":347,"title":81,"description":348,"path":349,"part":198,"topics":212},{"contentId":351,"title":116,"description":352,"path":353,"part":354,"topics":212},{"contentId":356,"title":134,"description":357,"path":358,"part":359,"topics":212},{"contentId":361,"title":151,"description":362,"path":363,"part":364,"topics":212},{"contentId":366,"title":157,"description":367,"path":368,"part":369,"topics":212},{"contentId":371,"title":169,"description":372,"path":373,"part":374,"topics":212},1795016665652]