lucacasonato / esbuild_deno_loader

Deno module resolution for `esbuild`
https://jsr.io/@luca/esbuild-deno-loader
MIT License
176 stars 46 forks source link

Reload Cached Modules #132

Open vfssantos opened 4 months ago

vfssantos commented 4 months ago

Use Case I frequently need to reload remote modules during development.

In my use case, ESBuild is building a module from another localhost server. This localhost server will serve the most updated files. As the default behavior is to cache the modules in DENO_DIR, I am adding a timestamp query parameter in the URL of the imported files as a workaround to have a different build each time I change the source code from this module:

export default async (path, { ...options } = {}) => {

   // `path` is a localhost URL

    const { imports } = denoConfig;

    const importMapURL = `data:application/json,${JSON.stringify({ imports: { ...imports, ...options?.denoConfig?.imports } })}`;

    const [denoResolver, denoLoader] = denoPlugins({ loader: 'native', importMapURL });

    const config = {
        plugins: [
            denoResolver,
            denoLoader,
        ],
        bundle: true,
        format: "esm",
        write: false,
        minifyWhitespace: true,
        minifyIdentifiers: false,
        minifySyntax: true,
        jsx: "transform",
        external: ['react', 'react-dom', ...(options.shared || [])],
    };

    options.environment !== 'production' && path.searchParams.set('v', new Date().getTime());

    config.entryPoints = [path.href];

    const result = await build(config);

    const code = result?.outputFiles?.[0]?.text;

    return { code }
}

Problem I understand that this is not ideal, as each time I reload the file, a new entry is created in the cache directory, very likely causing some waste.

Possible Solution I know that cache can be avoided using 'portable' loader, but then I run into a problem with npm: modules.

Suggestion It would be great to have a configuration option similar to Deno's CLI --reload command to avoid creating these additional caches.

I'm also wondering if there's any way of achieving a similar result already and I have missed it. If so, any suggestions would be welcome!