【问题标题】:How to delete runtime import cache in deno?如何在 deno 中删除运行时导入缓存?
【发布时间】:2023-03-22 06:46:01
【问题描述】:

在node.js中我们可以使用

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

在运行时删除 require 缓存。

有没有类似的方法可以删除 deno 中的运行时导入缓存?

【问题讨论】:

    标签: node.js deno


    【解决方案1】:

    -r--reload 选项将重新编译导入的模块。

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

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

    其他关键行为

    • 远程代码在首次执行时被获取和缓存,并且在使用 --reload 标志运行代码之前永远不会更新。 (所以,这将 还在飞机上工作。)
    • 从远程 URL 加载的模块/文件旨在实现不可变和可缓存。

    您可以传递参数以重新加载特定模块:

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

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

    【讨论】:

      【解决方案2】:

      到目前为止,我还没有找到清除缓存的命令。但是可以使用deno info 从 deno 获取当前缓存目录。输出将如下所示:

      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"
      

      所以删除DENO_DIR目录里面的文件,清除缓存。 希望这会有所帮助。

      【讨论】:

        【解决方案3】:

        Deno 从 19 年 8 月开始支持动态导入,我认为你可以做的就是

        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; 
        

        【讨论】:

          【解决方案4】:

          在路径中添加随机查询字符串,一定要保持正确的extname:

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

          还支持const somePath = '../foo.tsx';等本地文件路径

          【讨论】:

            猜你喜欢
            • 2021-09-25
            • 1970-01-01
            • 1970-01-01
            • 2020-12-03
            • 1970-01-01
            • 2015-01-22
            • 2021-05-16
            • 2017-07-27
            • 2020-09-24
            相关资源
            最近更新 更多