0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】esbuild-wasm用のsvelteプラグインを自作する

Last updated at Posted at 2026-01-02

概要

esbuild公式がexapmleで掲載しているsvelte pluginが自分の環境ではうまく動かなかったので多少修正した備忘録です。

環境

node.jsが使えない環境のため、memfsベースの仮想fsを使用しています。

script.js
import * as svelte from 'svelte/compiler';

export function sveltePlugin(fs) {
    return {
        name: 'svelte',
        setup(build) {
            build.onLoad({ filter: /\.svelte$/ }, async (args) => {
                const isHttp = /^https?:\/\//.test(args.path);

                const source = isHttp
                    ? await (await fetch(args.path)).text()
                    : await fs.readFile(args.path, 'utf8');

                try {
                    const compiled = svelte.compile(
                        source,
                        {
                            filename: args.path,
                            css: 'injected',
                            sourcemap: false,
                            dev: true,
                            fragments: 'tree'
                        }
                    );
                    return { contents: compiled.js.code, loader: 'js' };
                } catch (e) { console.error(e) }
            }
        }
    }
};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?