nuxt / nuxt/nuxt

Add a way to remove Nuxt template added with `addTemplate` function

Open
#30,577 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion workaround available
Dominant language
TypeScript
Stars
60.9k
Forks
5.8k
Avg merge
7h 8m
Merged PRs (30d)
162

Description

Describe the feature

For now there is undocumented feature — returning undefined inside template's getContents function will remove the template and even remove the file if write: true.

I think it is a good idea to implement direct ways to remove templates:

  1. remove() method on ResolvedNuxtTemplate which is a return of addTemplate method:
const myTemplate = addTemplate(...);
myTemplate.remove();
  1. removeTemplates() nuxt kit function with the filter property working in the same way as updateTemplates BUT make it required.
const myTemplate = addTemplate({ filename: 'sample-template.ts' });

removeTemplates({
    filter: template => template.filename === 'sample-template.ts',
)}

The second approach is by all means not safe, allowing to shoot yourself in the knee and delete important core-functionality Nuxt templates.

The first approach is safe because you only have references to templates you created or templates Nuxt or module authors provided to you.

As additional safety measure we can add persistent: boolean = true property, which will not allow to remove a template in any way other than undocumented getContents -> undefined way (meaning such persistent templates can delete themselfes only from inside).

const persistentTemplate = addTemplate({
    filename: 'i-am-strong',
    getContents: () => killMyself ? undefined as any : 'i am alive', // the only way to remove persistent template
    persistent: true, // true by default
});

persistentTemplate.remove() // Unkown method `remove` error + runtime throw!

Anyways, I think the docs should cover this getContents -> undefined trick and also allow it because for now to remove template undefined has to be cast to any: getContents: () => undefined as any.

Use case

I am writing a module which packs all separate .svg icons into a single icons-[hash].svg set.
I create and keep this set file inside Nuxt build directory as template, because I dont want to pollute users project workspace with .temp folders and other ugly-looking stuff.

When icons are changed I regenerate set file with new hash so I need a way to delete an old file in order not to pollute Nuxt build directory and create memory leaks.

In general Nuxt build directory is supposed to be the place where Nuxt and modules can store and work with templates.
And if we already have options to create and update templates, even write them as files, I think we need an ability to remove them.

For full control.

Additional information
  • Would you be willing to help implement this feature?
  • Could this feature be implemented as a module?
Final checks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the Nuxt Kit addTemplate and updateTemplates entry points and how getContents returning undefined is handled, including templates written with write: true. Compare the proposed ResolvedNuxtTemplate.remove and removeTemplates APIs, then define tests and documentation that demonstrate safe template removal and the persistent-template behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxt, typescript
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.