evanw / evanw/esbuild

Question: `transform` stage in plugins.

Open
#1,902 5 comments 8 reactions 0 assignees View on GitHub
plugins
Dominant language
Go
Stars
40.1k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

This question is about plugin **composition**, I used to be curious why rollup has 3 essential steps: `resolveId, load, transform`, now I have an answer: Plugins can be composited by doing different step.

Say we have a [svelte plugin](https://github.com/hyrious/esbuild-plugin-svelte/issues/2) and a [virtual icons plugin](https://github.com/antfu/unplugin-icons), which creates virtual module in your code and you just import it and use:

```svelte

import PlusIcon from "virtual-icon:mdi-plus.svelte"

```

If it is in rollup, the two plugins can be composited by:

```
icons plugin: resolve & load(virtual-name) => svelte code
svelte plugin: transform(svelte code) => js & css
```

Only the svelte plugin has to call svelte-compiler, so we can put compiler options (like whether to generate SSR code) in one place.

However, in esbuild plugins, we must do all works in `onLoad`, the icons plugin now has to call svelte-compiler, and we must pass compiler options to all of them.

There are other ways to deal with the same problem, for example:

- let the svelte plugin export a global state called virtual files, then let the icons plugin put these virtual files into that state. Now the icons plugin has a dependency of svelte plugin.
- let plugins can define custom loaders, like `loader: 'svelte'`, so that its `onLoad` can receive contents from other plugins.

I'm not sure which is the correct way to handle this usage.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with esbuild's plugin API, especially the onLoad hook, and trace how the Svelte plugin and virtual icons plugin would handle the example in the issue. No file or test is named; done would require a decided approach for composing these plugins and agreement on the resulting API or documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
build-system, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.