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

add a way to clear import cache for some file #27820

Open
steve02081504 opened this issue Jan 25, 2025 · 17 comments
Open

add a way to clear import cache for some file #27820

steve02081504 opened this issue Jan 25, 2025 · 17 comments

Comments

@steve02081504
Copy link

it'll useful for hot reload codes

@dsherret
Copy link
Member

The --reload flag?

@steve02081504
Copy link
Author

The --reload flag?

no it'll reload hole server/app

wanna a way that you can just clear some file's import cache so you can reload that file without full restart.

// a.js
console.log("hi")
// b.js
await import("./a.js")
await import("./a.js") // no  output

delete require.cache["./a.;s"]

await import("./a.js")

@steve02081504
Copy link
Author

@petamoriken
Copy link
Contributor

Since ECMAScript specifies that ES modules are cached, unfortunately your proposal would violate the spec.

@petamoriken
Copy link
Contributor

It is possible to load a module again as a separate module by using search params.

await import("./a.js");
await import("./a.js?2");

@steve02081504
Copy link
Author

It is possible to load a module again as a separate module by using search params.

await import("./a.js");
await import("./a.js?2");

not work for my repo, just like this:

//a.js
import b from 'b.js'
// c.js
await import("./a.js");
await import("./a.js?2");// will not reload b.js, bad :(

@BlackAsLight
Copy link

Why do you need it to reload?

@steve02081504
Copy link
Author

Why do you need it to reload?

user may update part/reload a same name part, and the js files in part may change.

@BlackAsLight
Copy link

Isn't that what the --watch flag is for?

@steve02081504
Copy link
Author

Isn't that what the --watch flag is for?

no I don't wanna restart all codes :(

@petamoriken
Copy link
Contributor

It's a dirty workaround, but you can use CommonJS require() for modules you want to purge the cache.

@steve02081504
Copy link
Author

steve02081504 commented Jan 28, 2025

It's a dirty workaround, but you can use CommonJS require() for modules you want to purge the cache.

same issue as before:

//a.js
import b from 'b.js'
// c.js
require("./a.js");
require("./a.js");// will not reload b.js, bad :(

@petamoriken
Copy link
Contributor

In the case of ConmonJS, delete require.cache[specifier] can be used.

@steve02081504
Copy link
Author

In the case of ConmonJS, delete require.cache[specifier] can be used.

can we add an obj like import.cache?
I don't think the authors fount currently has would be convenient/willing to migrate to CJS, that's not that realistic.

@petamoriken
Copy link
Contributor

We can't. If you want to make it happen, you should encourage TC39 to change the ECMAScript specification.

@0f-0b
Copy link
Contributor

0f-0b commented Jan 30, 2025

Why not use web workers? You can reload all modules by restarting the worker.

If code inside a buggy module goes into an infinite loop, you probably wouldn't want the entire app to freeze. Using workers you also get the ability to terminate ill-behaved code.

@steve02081504
Copy link
Author

Why not use web workers? You can reload all modules by restarting the worker.

If code inside a buggy module goes into an infinite loop, you probably wouldn't want the entire app to freeze. Using workers you also get the ability to terminate ill-behaved code.

the parts of my repo offers entrypoints/files/functions, and they may share vars. web worker is hard for part writers to learn and understand, and hard for development and sharing values :)

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

No branches or pull requests

5 participants