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

Dynamic Uncached Import (Feature Request) #14435

Closed
gherciu opened this issue Oct 9, 2024 · 5 comments
Closed

Dynamic Uncached Import (Feature Request) #14435

gherciu opened this issue Oct 9, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@gherciu
Copy link

gherciu commented Oct 9, 2024

What is the problem this feature would solve?

Imagine this setup:

file a.ts

setInterval(() => {
   import(`./b.ts?id=${Date.now()}`).then(console.log)
}, 1000)

file b.ts

import c from './c.ts'

export default c

file c.ts

const myString = 'abc'

export default myString

Now when running this will console log the result of dynamic import, and THIS IS IMPORTANT the dynamic import has a query param which means Bun should import a fresh version each time

But if while this script runs you go to c.ts and change the string manually the console log will not change, since C inside B is cached which is supper bad since I have a query param in B and I expect a fresh version of it and it's child imports also should be not cached for this exact import
Well that obsiously is kind of inconvenient, because that literally means you can't load user code since in runtime since it always will give the same result even if user manually changed this

Now I understand that this is not a bug, it is how ESM is designed to work and is the same in Node, Bun and Deno
And below is my proposal

What is the feature you are proposing to solve the problem?

What I'm proposing is a new API maybe called uncachedImport which will import that module/file and it's child imports in a uncached manier basically fresh versions each time the method is called

What alternatives have you considered?

There is require.cache which you can use to remove modules from cache, but it's not clear how to remove the child imports and I tried it I'm not even sure that it plays nicely with both import/require , and overall seems like a bad practice and couldn't make it working in Bun with both import/require

@gherciu gherciu added the enhancement New feature or request label Oct 9, 2024
@xd1gital
Copy link

xd1gital commented Oct 18, 2024

This is the last feature that prevents me from switching my app to bun. My backend manages and cordinates a lof of apis (each responsible for specific purposes). When I need to update an API, I don't have to reload the whole app. I just need to request the backend to reload (uncache) that specific API. I'm currently using "require.cache" to solve this problem.

  • added: Bun does work in most cases, but getting error randomly when trying to reload an api, I'm unable to track down the cause yet.

@7flash
Copy link

7flash commented Oct 24, 2024

I've also encountered this issue.

I am watching files for update using fs.watch (https://bun.sh/guides/read-file/watch)

Then I do "const { default: newFn } = await import(filePath)"

Unfortunately, I've noticed newFn is always the same, which defeats the whole purpose of fs.watch in this case

How did you solve it? @xd1gital

@7flash
Copy link

7flash commented Oct 24, 2024

nodejs/node#49442

@Jarred-Sumner
Copy link
Collaborator

To clear the import cache for a particular file, use delete require.cache[specifier]. This applies to ESM & CJS modules, and the next import/require reloads again

@gherciu
Copy link
Author

gherciu commented Oct 24, 2024

Hey, This doesn't work
For require Cache is just empty always, Thats one thing
And second is how do you find out what are the children's of another import

With your approach you can delete only imports of first level which you know their specifier but wat if you do not know which imports are in the children imports, these will be still imported from cache
You think of one level import, but there are many nested imports, In my case and what this feature request is saying is that all children imports would be fine to uncache

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

No branches or pull requests

4 participants