### Description
Reviving #1928. I came across
`import.meta.url.ROLLUP_FILE_URL_xxx` in one of my plugins
(hi-ogawa/vite-plugins#673) and I think having
the compat in rolldown is nice to have (though it's also possible to
have a compat in rolldown-vite or user land level via a custom plugin's
`renderChunk` replacing).
Sapphi also mentioned that Astro uses this feature
#819 (comment).
I'm hoping the complexity required on rolldown side is low and it's
worth the inclusion. Let me know what you think.
<details><summary>Example user-land plugin to replace
"import.meta.ROLLUP_FILE_URL_xxx"</summary>
From hi-ogawa/vite-plugins#673
```js
import path from "node:path";
import MagicString from "magic-string";
import type { Plugin } from "vite";
// import.meta.ROLLUP_FILE_URL_xxx on rolldown
// #1928
export function rolldownPluginRollupFileUrl(): Plugin {
return {
name: rolldownPluginRollupFileUrl.name,
renderChunk: {
order: "pre",
handler(code, chunk) {
if (!code.includes("import.meta.ROLLUP_FILE_URL_")) {
return;
}
const matches = code.matchAll(/import.meta.ROLLUP_FILE_URL_(\w+)/dg);
const output = new MagicString(code);
for (const match of matches) {
const referenceId = match[1]!;
const assetFileName = this.getFileName(referenceId);
const relativePath =
"./" +
path.relative(
path.resolve(chunk.fileName, ".."),
path.resolve(assetFileName),
);
const replacement = `new URL(${JSON.stringify(relativePath)}, import.meta.url)`;
const [start, end] = match.indices![0]!;
output.update(start, end, replacement);
}
if (output.hasChanged()) {
return {
code: output.toString(),
map: output.generateMap({ hires: "boundary" }),
};
}
return;
},
},
};
}
```
</details>
Description
For the context, I'm currently looking into supporting
new URL("./some-asset.svg", import.meta)pattern for Vite pre-bundling. Esbuild currently doesn't support it, but it's more or less possible as a user land plugin (cf. vitejs/vite#17837).While thinking about how it could be done on rolldown, it's probably possible to implement it as a plugin using
transformandrenderChunkhooks as well, but having a support ofimport.meta.ROLLUP_FILE_URL_referenceIdseems to ease the implementation by eliminating the need ofrenderChunk. It's not included in this PR, but I made a prototype ofAssetImportMetaUrlPluginhere hi-ogawa@b5d6e25I wasn't sure if
import.meta.ROLLUP_FILE_URL_is on the roadmap, but as I was interested in the code base around this, so here is my attempt. I would appreciate feedback.