Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear single module #11

Closed
Dejmek opened this issue Aug 19, 2019 · 2 comments · Fixed by #12
Closed

Clear single module #11

Dejmek opened this issue Aug 19, 2019 · 2 comments · Fixed by #12

Comments

@Dejmek
Copy link

Dejmek commented Aug 19, 2019

Could you add a possibility co clear only single module, without the recursion? I have a system that has a state holding singleton and with that recursive clear it creates multiple instances of that singleton with a different state. I would like to have an option to clear it manually one by one.

@omrilotan
Copy link
Contributor

It feels like the entire purpose of this repository is to solve the problem of recursive cleaning.

Clearing a module from cache without any parent links or children is pretty straight forward:

delete require.cache[require.resolve(<PATH_TO_MODULE>)];

Is this in your opinion a conclusion for this issue, @Dejmek ?

@Kikobeats
Copy link

Kikobeats commented Oct 6, 2019

well also could be a good abstraction.

This is my code for achieving this:

const getModuleId = item => {
  try {
    return require.resolve(item)
  } catch (err) {
    return false
  }
}

const clearSingleModule(item) => {
  const moduleId = getModuleId(item)
  if (!moduleId) continue
  if (!require.cache[moduleId]) continue
  delete require.cache[moduleId]
}

Something like clearModule.single could be helpful 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants