#title Weekly Vue News #92 - Use Nuxt Layers to Share Components, Utils, and Configuration Between Your Apps #preview Ready for your weekly Vue & Nuxt dose?
Use Nuxt Layers to Share Components, Utils, and Configuration Between Your Apps
Hi π
In this edition, I don't have any groundbreaking news to drop, but I'm planning to update one of my most popular blog posts: "Why I Switched From WebStorm to VS Code." The new title should be "Why I Use Both IntelliJ and VS Code".
I got feedback that my website is being blocked by antivirus software. I reported these false positives and I hope that it will be resolved in the next few days. Sorry for that, I still have no idea why that happened π€·π»ββοΈ
Oh, and guess what? I've got a fantastic Nuxt tip for you in this issue! It's all about Nuxt Layers, a neat little trick that lets you share components, utils, and configuration between your apps effortlessly. It's a total game-changer for streamlining your development process and making your code more reusable.
Get ready for some fun and insightful content on Vue.js and Nuxt.js. I hope you find the tips and updates in this newsletter helpful and inspiring.
Have a lovely week βοΈ
To support me:
- π Recommend the newsletter to your friends: it really helps!
- πΈ Sponsor the newsletter
- π§΅ Retweet the latest Twitter thread
- π¨ Reply to this email: feedback is welcome
Nuxt 3 provides a powerful core feature that allows you to share components, utils, and configurations between your Nuxt 3 applications. These so-called layers are almost identical to a standard Nuxt application, let's take a look at an example.
Create Layer
For this demo, we would like to share two things from our Nuxt layer:
- the
Nuxt Tailwind module - a
BaseButton
component
To create a new Nuxt layer, visit Layer
starter:
To add the Tailwind module, we follow the getting started steps from the module documentation:
First, we install the module:
1 pnpm add -D @nuxtjs/tailwindcss
Then we add it to the modules
section in our nuxt.config.ts
:
1 export default defineNuxtConfig({
2 modules: ['@nuxtjs/tailwindcss'],
3 })
Next, we add our BaseComponent
in the components
folder:
1 <template>
2 <button class="rounded-md border-2 bg-blue-400 p-2 text-white">
3 <slot />
4 </button>
5 </template>
For this demo, let's share the layer via NPM. You can easily publish your layer by running the following command:
1 pnpm publish --access public
It will publish the layer with the name
specified in package.json
.
You can share more than components and modules with a Nuxt layer, for example:
- reusable configuration presets in nuxt.config and app.config
- utility and composable functions inside
/composables
andutils/
directories - Nuxt themes
- Nuxt module presets
The code for this demo Nuxt layer is available at StackBlitz:
Use Layer
Now let's use our Nuxt layer in a Nuxt application. The first step is to install the NPM package of our Nuxt layer:
1 pnpm add -D @mokkapps/nuxt-layer-demo
Then you need to add the dependency to extends
in nuxt.config
:
1 defineNuxtConfig({
2 extends: '@mokkapps/nuxt-layer-demo',
3 })
You can also extend from a local layer or a Git repository:
1 export default defineNuxtConfig({
2 extends: [
3 '../base', // Extend from a local layer
4 '@my-themes/awesome', // Extend from an installed npm package
5 'github:my-themes/awesome#v1', // Extend from a git repository
6 ],
7 })
Without any further configurations, you can now use BaseButton
and Tailwind classes in our Nuxt application.
The code for this Nuxt application is interactively available at StackBlitz:
ππ» Ever wondered when to use @vuejs scoped slots vs template refs and where each shines? |
ππ» Daniel Kelly explains how you can write a single component for all the links internal and external. |
ππ» This article compares them on speed, dependency resolution, offline mode, security, and custom registries. |
ππ» This article uncovers some tips and tricks for getting the most out of your browserβs developer tools. |
ππ» CHI 2023 is a conference for leading human-computer interaction research. |
ππ» This blog post lists some interesting papers related to programming, such as visual methods to debug better. |
ππ» If you've ever struggled to explain AI to your non-technical friends and family, here's a great story-based approach. |
Until next week,
Holzapfelkreuther Str. 19, 81375 Munich, Germany