Hacker News new | ask | show | jobs
by light24bulbs 1427 days ago
I'd REALLY like to hear more about the macro system. I've been yelling about wanting that to become part of typescript for a long time, but I don't think they quite have the vision.

In Zig, code can run at compile time, and thats amazing. That's one of the main things I want in TypeScript. Once you have that, everything from type reflection to native graphql types without codegen is just a library away.

Yeah, I'd really, really like to hear more about that if you can point me at anything. Overall, all of the decisions Bun is making, in just about every part of the ecosystem it touches, are things that I have been really excited about. I've got a good feeling about this and I'm trying to scrape some time together to help out.

As for esbuild, I don't think you're correcting the article because it said that the parser is a `port` of esbuild, not "based on". You're probably just replying to the other comment :)

1 comments

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...

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.