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

Support generating build meta #235

Open
1 task done
jrson83 opened this issue Mar 21, 2023 · 4 comments · May be fixed by #296
Open
1 task done

Support generating build meta #235

jrson83 opened this issue Mar 21, 2023 · 4 comments · May be fixed by #296
Assignees
Labels
discussion enhancement New feature or request

Comments

@jrson83
Copy link

jrson83 commented Mar 21, 2023

Describe the feature

tsup already integrated the --metafile flag to tell esbuild to produce some metadata about the build in JSON format.

It is great for analyzing with bundle-buddy.com. To see support for this in unbuild would be great!

More Info:

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 changed the title Add --metafile flag to support with esbuild Support generating build meta Mar 22, 2023
@pi0
Copy link
Member

pi0 commented Mar 22, 2023

I love the idea! Renamed PR title to introduce it as an option (we could enable also via CLI flag).

@jrson83
Copy link
Author

jrson83 commented Mar 25, 2023

I love the idea! Renamed PR title to introduce it as an option (we could enable also via CLI flag).

Cool. I checked out the source to see if I'm able to implement the feature. I understand that unbuild uses rollup as bundler and esbuild as rollup plugin.

It seems that esbuilds metafile option is only part of the BuildOptions interface, which is used by the esbuild build function:

export let build: typeof types.build = (options: types.BuildOptions) {} // ...

https://esbuild.github.io/api/#build

unbuild does not use the build function, but instead the transform function which uses TransformOptions interface, on which the metafile option is not available.

https://esbuild.github.io/api/#transform

When selecting rollup on bundle-buddy.com, there is an example of how to implement a plugin function on rollups buildEnd hook, which writes a bundle-buddy compatible file:

plugins: [
  {
    buildEnd() {
      const deps = [];
      for (const id of this.getModuleIds()) {
        const m = this.getModuleInfo(id);
        if (m != null && !m.isExternal) {
          for (const target of m.importedIds) {
            deps.push({ source: m.id, target })
          }
        }
      }
      
      fs.writeFileSync(
          path.join(__dirname, 'graph.json'), 
          JSON.stringify(deps, null, 2));
    },
  }
]

But it additionally requires a sourcemap generated by rollup:

output: { 
  file: '`${outFolder}/dist.js',
  format: 'iife',
  name: 'PROJECT_NAME',
  sourcemap: true,
}

Since I just saw the issue #236, it would be possible to add a metafile option to the RollupBuildOptions interface and create a plugin I just tested:

import { writeFile } from "node:fs/promises";
import type { Plugin } from "rollup";
import { resolve } from "pathe";

export interface MetafileOptions {
  enable?: boolean;
}

export interface MetaInfo {
  source: string;
  target: string;
}

const defaults: MetafileOptions = {
  enable: false,
};

export function metafilePlugin(opts: MetafileOptions = {}): Plugin {
  opts = { ...opts, ...defaults };
  return {
    name: "unbuild-metafile",
    async buildEnd(err) {
      if (!err && opts.enable) {
        const deps: MetaInfo[] = [];

        for (const id of this.getModuleIds()) {
          const m = this.getModuleInfo(id);
          if (m != null && !m.isExternal) {
            for (const target of m.importedIds) {
              deps.push({ source: m.id, target });
            }
          }
        }

        if (Array.isArray(deps) && deps.length === 0) {
          return;
        }

        const outPath = resolve(__dirname || process.cwd(), "graph.json");
        await writeFile(outPath, JSON.stringify(deps), "utf8");
      }
    },
  } as Plugin;
}

@pi0 what you think?

@pi0
Copy link
Member

pi0 commented Jul 18, 2023

Hi @jrson83 . Sorry for checking late on this. sourcemap support is now added for rollup. Plugin implementation seems nice. Do you still like to work on this feature?

@pi0 pi0 added enhancement New feature or request discussion labels Jul 18, 2023
@jrson83
Copy link
Author

jrson83 commented Jul 18, 2023

Hi @jrson83 . Sorry for checking late on this. sourcemap support is now added for rollup. Plugin implementation seems nice. Do you still like to work on this feature?

Great, sure I will see what I can do and let you know 😀

@jrson83 jrson83 linked a pull request Aug 3, 2023 that will close this issue
8 tasks
@pi0 pi0 self-assigned this Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants