Skip to content

Support for custom loaders is missing in tsdown (and not documented in migration guide) #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhdcodes opened this issue Apr 27, 2025 · 10 comments
Labels

Comments

@mhdcodes
Copy link

mhdcodes commented Apr 27, 2025

The current version of tsdown does not support [custom loaders], whereas tsup does https://tsup.egoist.dev/#custom-loader.

This isn’t mentioned anywhere in the migration guide, so users relying on this functionality will hit a wall trying to migrate, in fact we rely on this for an open source astro project codiume/orbit#998, I only discovered this when I tried to migrate from tsup to tsdown. we use custom loaders to skip parsing .astro files

Questions / Roadmap:

  • Are there plans to add loader support in a future tsdown release?

  • If not, could the migration guide be updated to explicitly note this limitation and suggest any recommended patterns or plugins?

@Gugustinette
Copy link
Collaborator

Gugustinette commented Apr 28, 2025

To add a bit more context, tsup leverage esbuild's loader feature and exposes it as a custom loader option.

Rolldown exposes a similar -experimental- option called module types.

Then having "custom loader" should already be possible by overriding Rolldown's config like so :

// tsdown.config.ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['./src/index.ts'],
  // Override Rolldown's input options here
  inputOptions: {
    moduleTypes: {
      '.custom': 'json',
    },
  }
})

However, in your case @mhdcodes , an asset loader was discussed here when a file loader (similar to the copy loader) was proposed.
I must say I'm not sure about what it is exactly as I couldn't find any reference to it, it seems the Rolldown loader issue was closed so I guess something similar should exist ?

@Gugustinette
Copy link
Collaborator

Gugustinette commented Apr 28, 2025

Alright so the Rolldown engine (in Rust) actually has the asset loader in the ModuleType enum.

But the TypeScript package's ModuleTypes enum does not exposes it for some reason.

Using the asset loader might actually work if you ignore the type error I guess ?

// tsdown.config.ts
import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['./src/index.ts'],
  inputOptions: {
    moduleTypes: {
      // @ts-expect-error
      '.astro': 'asset',
    },
  }
})

But it seems like an issue in Rolldown's TypeScript package to me.

@mhdcodes
Copy link
Author

I will try this an let you know @Gugustinette thanks for the valuable feedback.

@mhdcodes
Copy link
Author

apparently this doesn't work:

For the "moduleTypes..astro". Invalid type: Expected ("base64" | "binary" | "css" | "dataurl" | "empty" | "js" | "json" | "jsx" | "text" | "ts" | "tsx") but received "asset". 

@Gugustinette
Copy link
Collaborator

Oh yeah it seems they have a ModuleTypes validator so it's not just some type error.

I'll open the issue on Rolldown's repo.

@ryoppippi
Copy link
Contributor

can we use some rollup plugins for assets like @rollup/plugin-wasm ?

github-merge-queue bot pushed a commit to rolldown/rolldown that referenced this issue May 11, 2025
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->

Fixes #4363

The Rust `ModuleType` enum and it's `impl`s were slightly misaligned, so
fixed those as well.


### Context
I'm trying to migrate to `tsdown`, but need the custom loader support
rolldown/tsdown#162
@Gugustinette
Copy link
Collaborator

It seems the issue was resolved on Rolldown's side, now we are waiting for a release.

@MatthijsKok
Copy link
Contributor

MatthijsKok commented May 11, 2025

Should pnpx tsdown migrate create a mapping from tsup loaders to tsdown moduleTypes?
e.g. running the migration on a simple example

import { defineConfig } from 'tsup'

export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm'],
  loader: {
    '.jpg': 'copy',
  },
})

yields an almost identical config (which has an error for unknown loader field):

import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm'],
  loader: {
    '.jpg': 'copy',
  },
})

I would expect the tsdown migrate command to atleast update the loader object location into inputOptions.moduleTypes, and ideally also the copy => asset module type itself.
e.g.

import { defineConfig } from 'tsdown'

export default defineConfig({
  entry: ['src/index.ts'],
  format: ['esm'],
  inputOptions: {
    moduleTypes: {
      '.jpg': 'asset',
    },
  },
})

@MatthijsKok
Copy link
Contributor

It seems the issue was resolved on Rolldown's side, now we are waiting for a release.

Latest canary includes the fix.
This PR updates tsdown to that version: #220

@sxzz
Copy link
Member

sxzz commented May 12, 2025

Added via 7132a58

@sxzz sxzz closed this as completed in f74e7f1 May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants