Search code examples
javascriptrollupjs

Get resolved file name from Rollup


I setup a new project today using Vite/Rollup for the first time. I understand that Rollup will automatically scan your JS and (with the right plugins) HTML, etc files to magically find any referenced assets/entries and replace them with their production URLs when building.

E.g.

import { something } from './src/module.js';

Might turn into

import { something } from '/assets/js/module-<hash>.js';

Now let's say I have a not further specified use-case where I want to get the URL to module.js bundle/file in production to do something with it that Rollup can't automagically resolve for me. How would I go about this?

E.g.

const url = rollupResolve('./src/module.js');
doSomethingWithUrl(url);

I've searched the documentation and the internet for the last couple hours but didn't find anything that actually worked. In fact, I wasn't even able to find anyone asking this specific question.

The closest to this I found was to use new URL('./src/module.js', import.meta.url).href, but this -- at least with default settings -- will give me an inlined version of the file if it is small enough and there doens't seem to be a way to override that behavior on a per-callsite basis or even just for that one specific file.

Any input or advice would be greatly appreciated. Thanks!


Solution

  • I found a output manifest plugin which generates a manifest of all the processed Rollup output files (i.e. entry points and assets).

    While it is a standalone file that requires an additional round trip to be able to utilize this list in the application code (instead of inlining it), that is acceptable for the time being and something that can be optimized down the line.