hyrious/esbuild-repl

refactor: support virtual files

hyrious opened this issue · 1 comments

Since esbuild supports plugins, it is possible to make a website like rollup-repl, which uses plugins to provide virtual files.

What's more, we can add a new section for writing plugins online.

Good news: It is possible to achieve this without the stdin option now (kind: entry-points can be redirect to a custom onLoad callback).

esbuild.build({ entryPoints: ['a.ts'], plugins: [{ name: '1',
setup({ onResolve, onLoad }) {

    onResolve({ filter: /.*/ }, args => {
        console.log('resolve', args)
        return { path: args.path, namespace: 'v' }
    })

    onLoad({ filter: /.*/, namespace: 'v' }, args => {
        console.log('load', args)
        return { contents: 'export let a = 1' }
    })

} }] }).then(r => {
    for (let { path, text } of r.outputFiles) {
        console.log(">>>", path)
        console.log(`%c${text}`, 'background-color: rgba(127, 127, 127, .2)')
    }
})