Skip to content
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

Seeking feedback on a nested build plugin #52

Closed
jed opened this issue Apr 29, 2021 · 0 comments
Closed

Seeking feedback on a nested build plugin #52

jed opened this issue Apr 29, 2021 · 0 comments

Comments

@jed
Copy link
Contributor

jed commented Apr 29, 2021

Hey all,

I'm working on a web server (server.mjs) that serves a service worker (sw.js) that serves a JavaScript app (index.js), and would like to build the entire app into a single deployable file. so in my case server.mjs imports sw.js, which in turn imports index.js, and the convention I'm using is that .mjs files are bundled as usual, and .js files are bundled as IIFEs and returned as {source, map} objects. so, for example, server.mjs contains import {source, map} from './sw.js where source and map are both strings.

I've built the following plugin to try to accomplish this. but would like some feedback about a more elegant esbuild-ish way. specifically:

  1. is there a better way to prevent infinite recursion than the plugin storing a reference to remove itself from the parent build?
  2. is there any way to have the returned contents be binary instead of strings? (can't use the binary loader because i need to return an object with two properties).
  3. is there a way to get a reference to the original build function so that the plugin could be self-contained?
  4. is this a sound approach, or a terrible idea? ie, am i better off running these builds consecutively?
let myplugin = {
  name: 'myplugin',
  setup(b) {
    b.onLoad({filter: /\.js$/}, async args => {
      let {outputFiles} = await build({...b.initialOptions,
        entryPoints: [args.path],
        format: 'iife',
        outdir: '.',
        write: false,
        plugins: b.initialOptions.plugins.filter(p => p !== myplugin)
      })

      let values = Object.values(outputFiles)
      let [map, source] = values.map(value => value.text)
      let contents = JSON.stringify({source, map})
      return {contents, loader: 'json'}
    })
  }
}
@jed jed closed this as completed Nov 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant