question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to auto import Components in Source Content folder?

See original GitHub issue

Currently, we can use components/content folder to write components that will be used in markdown files.

But, that’s not an optimum workflow especially when adding some component, which will be only used in a particular markdown file. It can be similar to Vitepress or Vuepress, but instead of writing javascript in md file, write it in nearby component, and just use it in md file.

So, it would be better to write such components alongside the markdown file itself (both in same folder inside content folder), and then directly use that component inside the markdown file.

So, how to auto import such component inside content folder?

I tried this in nuxt.config.ts: (It doesn’t work)

import { defineNuxtConfig } from 'nuxt'

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
  components: {
    "dirs": [
      {
        "path": "~/components/global",
        "global": true
      },
      "~/components",
      {
        "path": "~/content", // this doesn't work
        "global": true
      }
    ]
  },
  modules: ['@nuxt/content'],
  content: {
    // ignores: ['.*\\.vue'],
  }
})

It still gives this warning (if md-hello component (which is inside content folder) is used anywhere in any markdown file)

[Vue warn]: Failed to resolve component: md-hello
If this is a native custom element, make sure to exclude it from component resolution via compilerOptions.isCustomElement.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
farnabazcommented, Aug 24, 2022

Nuxt does not load components from content dir, because the content module adds source directories into Nuxt ignore paths. (https://github.com/nuxt/content/blob/main/src/module.ts#L286-L295) Otherwise, Vite’s server will rebuild on every file change.

Maybe we can ignore .vue files from this ignore pattern.

0reactions
farnabazcommented, Aug 24, 2022

AFAIK ignore option only applies to files inside rootDir and all files outside of root dir will not be affected by this.

Also, I couldn’t reproduce the issue in my simple reproduction, maybe I’m missing something. It would be nice if you create a reproduction for it (a github repo) to reproduce this behavior.

I will create a PR for Vue file inside the content directory.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to auto import components? : r/sveltejs - Reddit
Instead of doing the imports manually , is there a way to automatically import all of the components from a certain directory?
Read more >
Auto Import Components In Svelte Kit - Weekly Svelte - YouTube
https://github.com/yuanchuan/sveltekit-autoimporthttps://github.com/yuanchuan/sveltekit- autoimport /issues/3globals.d.ts `declare const ...
Read more >
Auto Import of React Components in Visual Studio Code [closed]
Missing modules will show up with a Code Action (AKA "Quick Fix") with an option to import. You can click on the lightbulb...
Read more >
Building a Vue Auto Component Importer - Harlan Wilton
In the wild, you can find auto component imports in most popular Vue ... compileTemplate({ id: 'tmp', source: parsed.template.content, ...
Read more >
Auto-importing custom components in VueJS - Aljax.us
I've created that script in my /src/plugins folder, but you can create it anywhere you want. Auto-loading script in plugins directory import Vue ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found