Hacker News new | ask | show | jobs
by lioeters 1427 days ago
Searching Bun's documentation (currently the long README on GitHub) for "macro", I only see a brief usage example as part of the build configuration, like:

  [macros]
  # Remap any import like this:
  #     import {graphql} from 'react-relay';
  # To:
  #     import {graphql} from 'macro:bun-macro-relay';
  react-relay = { "graphql" = "bun-macro-relay" }
..Or as a transpiler option:

  const transpiler = new Bun.Transpiler({
    loader: "tsx",
    define: {
      "process.env.NODE_ENV": JSON.stringify("development"),
      user_undefined: "undefined",
    },
    platform: "bun",
    macro: {
      inline: {
        whatDidIPass: `${import.meta.dir}/inline.macro.js`,
      },
      react: {
        bacon: `${import.meta.dir}/macro-check.js`,
      },
    },
  });
..But not within the transpiled code itself.

Ooh, hold on - there's more in /test/macro. Here's hello-fetch-macro.tsx.

  import { fetchSync } from "macro:./fetchSync.tsx";

  const synchronousFetch = fetchSync(`https://example.com`);

  console.log(synchronousFetch);
Looks like the special import syntax converts the function to run at build time.

https://github.com/oven-sh/bun/blob/c3bf97002d6f0b117060dabf...

1 comments

I actually found out a bit more about it and added it to the blog post. I found links to the macro examples and so on, added it. It uses jsx/tsx with special tags like <string> and <array>. Honestly, that strikes me as completely genius. I really hope it can emit types before the typescript compilation step, not sure. Tried to read the zig but...it's deep.