Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete runtime import cache in deno?

Tags:

node.js

deno

In node.js we can use

delete require.cache[require.resolve(somePath)];

to delete the require cache in runtime.

Is there a similar way to delete runtime import cache in deno?

like image 696
xcatliu Avatar asked May 20 '20 02:05

xcatliu


People also ask

Where does Deno cache its code?

This is because for this run, instead of compiling again, Deno is directly using the cached version in gen/. The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs.

How to force Deno to recompile TypeScript code instead of using cache?

The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs. To force Deno to recompile your TypeScript code instead of using the cached version, you could use the --recompile flag. $DENO_DIR/deps is used to store files fetched through remote url import.

How do I import a JavaScript module into Deno?

Also note a JavaScript module is imported directly into a TypeScript module, Deno has no problem handling this. Command: deno run ./remote.ts In the local import example above the add and multiply functions are imported from a locally stored arithmetic module.

Why does the compilation message disappear when running Deno?

The compilation message is gone. This is because for this run, instead of compiling again, Deno is directly using the cached version in gen/. The cache load and save code could be found in DenoDir::load_cache and DenoDir::code_cache, from src/deno_dir.rs.


4 Answers

Add a random querystring to the path, be sure to keep the correct extname:

const ext = path.extname(somePath);
const mod = (await import(`${somePath}?version=${Math.random()}${ext}`)).default;

It also support local file path like const somePath = '../foo.tsx';

like image 132
xcatliu Avatar answered Oct 24 '22 05:10

xcatliu


So far I have not found a command that clears the cache. But it is possible to get the current cache directories from deno using deno info. The output will look like this:

DENO_DIR location: "/Users/tgm/Library/Caches/deno"
Remote modules cache: "/Users/tgm/Library/Caches/deno/deps"
Emitted modules cache: "/Users/tgm/Library/Caches/deno/gen"
Language server registries cache: "/Users/tgm/Library/Caches/deno/registries"

So deleting the files inside DENO_DIR directory, clear the cache. Hope this is helpful.

like image 37
Alexander Vu Avatar answered Oct 24 '22 05:10

Alexander Vu


The -r or --reload option will recompile the imported modules.

-r, --reload=<CACHE_BLACKLIST>     Reload source code cache (recompile TypeScript)

https://deno.land/manual#other-key-behaviors

Other key behaviors

  • Remote code is fetched and cached on first execution, and never updated until the code is run with the --reload flag. (So, this will still work on an airplane.)
  • Modules/files loaded from remote URLs are intended to be immutable and cacheable.

You can pass arguments to reload specific modules:

--reload=https://deno.land/std

https://deno.land/manual/linking_to_external_code/reloading_modules

like image 13
Evandro Pomatti Avatar answered Oct 24 '22 03:10

Evandro Pomatti


Deno supports dynamic imports since August '19, what I think you can do is something like

let neededModule = getNeededModule();
import neededModule;
...
neededModule  = getAnotherModule(); //Replace in runtime
import neededModule
...
//Or even delete in runtime (with some help from the garbage collector)
neededModule = null; 
like image 2
Ahmed Hammad Avatar answered Oct 24 '22 05:10

Ahmed Hammad