framework icon indicating copy to clipboard operation
framework copied to clipboard

Nuxt 3 Module custom layout style issue

Open sppmstar opened this issue 3 years ago • 6 comments

Environment

  • Operating System: Darwin
  • Node Version: v14.18.2
  • Nuxt Version: 3.0.0-rc.4-27605536.8c2c80e
  • Package Manager: [email protected]
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

Please see reproduction example here: https://github.com/sppmstar/nuxt-3-module-layout-vite-bug

Describe the bug

When adding a style block to a custom layout in a Nuxt 3 module, i get the following error:

Screen Shot 2022-06-29 at 14 44 59

If I remove the

Additional context

No response

Logs

No response

sppmstar avatar Jun 29 '22 19:06 sppmstar

any updates on this issue @danielroe ?

sppmstar avatar Jul 28 '22 15:07 sppmstar

This looks like an upstream issue with @vitejs/plugin-vue which is trying to read the virtual template from disk. You can workaround it for the moment by ensuring the template is written to disk:

- const tpl = addTemplate(resolve(layoutsDir, 'custom-layout.vue'))
+ const tpl = addTemplate({
+   src: resolve(layoutsDir, 'custom-layout.vue'),
+   write: true
+ })

Or, even more simply, using your existing file:

- const tpl = addTemplate(resolve(layoutsDir, 'custom-layout.vue'))
- addLayout(tpl, 'custom')
+ addLayout(resolve(layoutsDir, 'custom-layout.vue'), 'custom')

danielroe avatar Jul 28 '22 21:07 danielroe

@danielroe is this workaround only available in newer versions ? I tried your suggestion but i get a TypeScript warning about invalid argument (addLayout expects a NuxtTemplate in the version I am running), and am still getting the same error upon accessing localhost:3000 .

sppmstar avatar Aug 09 '22 15:08 sppmstar

I also tried the approach of adding write: true to the addTemplate call; while it does get rid of the error, the custom layout is still not rendering. Please see updated code in the reproduction example listed above: https://github.com/sppmstar/nuxt-3-module-layout-vite-bug/commit/df7c5830465c9bf5f236abd35c7b93b7b3b12af1

sppmstar avatar Aug 09 '22 15:08 sppmstar

I just tried updating my node modules and am now running Nuxt CLI v3.0.0-rc.6-27667279.0b22079 but still get the same issue.

sppmstar avatar Aug 09 '22 15:08 sppmstar

You are right, it does need a template, not a string - my mistake.

But you define layouts with definePageMeta, not within component options: https://stackblitz.com/edit/github-7l7hcr.

danielroe avatar Aug 10 '22 11:08 danielroe

hey @danielroe ,

I confirm it works now when using:

definePageMeta({
  layout: 'custom',
});

Thanks a lot !

sppmstar avatar Aug 11 '22 17:08 sppmstar

I updated the code in the example repo with the working code; hope this helps someone else :) https://github.com/sppmstar/nuxt-3-module-layout-vite-bug

sppmstar avatar Aug 11 '22 17:08 sppmstar