Skip to content

feat: support import.meta.ROLLUP_FILE_URL_referenceId for asset#1928

Closed
hi-ogawa wants to merge 20 commits intorolldown:mainfrom
hi-ogawa:feat-rollup-file-url
Closed

feat: support import.meta.ROLLUP_FILE_URL_referenceId for asset#1928
hi-ogawa wants to merge 20 commits intorolldown:mainfrom
hi-ogawa:feat-rollup-file-url

Conversation

@hi-ogawa
Copy link
Collaborator

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 transform and renderChunk hooks as well, but having a support of import.meta.ROLLUP_FILE_URL_referenceId seems to ease the implementation by eliminating the need of renderChunk. It's not included in this PR, but I made a prototype of AssetImportMetaUrlPlugin here hi-ogawa@b5d6e25

I 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.

@netlify
Copy link

netlify bot commented Aug 10, 2024

Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit fe6b76f
🔍 Latest deploy log https://app.netlify.com/sites/rolldown-rs/deploys/66b82423b0d8e100086f6339

@hyf0
Copy link
Member

hyf0 commented Aug 10, 2024

new URL("./some-asset.svg", import.meta) is planned to supported natively by rolldown. But it's waiting for the first-class support for assets. But we could have plugin/builtin-workaround solution if the feature is needed right now.

@hi-ogawa
Copy link
Collaborator Author

new URL("./some-asset.svg", import.meta) is planned to supported natively by rolldown. But it's waiting for the first-class support for assets. But we could have plugin/builtin-workaround solution if the feature is needed right now.

That's good to know. Probably I can wait for that and for that time being, I'll explore a plugin-based implementation. import.meta.ROLLUP_FILE_URL_ is not strictly necessary for this too, so I'll try without it. Feel free to prioritize/decide on this feature's inclusion.

@hi-ogawa
Copy link
Collaborator Author

hi-ogawa commented Nov 2, 2024

Closing as it's probably too stale

@hi-ogawa hi-ogawa closed this Nov 2, 2024
@hi-ogawa hi-ogawa deleted the feat-rollup-file-url branch November 2, 2024 08:43
github-merge-queue bot pushed a commit that referenced this pull request Feb 5, 2025
### 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants