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

Babel Macro #150

Open
nick-at-hawkfish opened this issue Jan 27, 2020 · 4 comments
Open

Babel Macro #150

nick-at-hawkfish opened this issue Jan 27, 2020 · 4 comments
Labels
discussion Discussions and proposals enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed proposal

Comments

@nick-at-hawkfish
Copy link

It would be nice to have a babel macro that automatically runs the babel plugin to remove the runtime. That way, this project would work out of the box with "zero" config tools like create-react-app.

I'm thinking it could be something like:

import {html} from 'htm/preact/macro'

This is similar conceptually to CSS-IN-JS libraries providing a macro for their tools. See https://emotion.sh/docs/babel-macros

@developit
Copy link
Owner

Interesting! If it's not a huge undertaking I think that might be nice. The idea of it baking in the JSX pragma based on the path (like you've shown here) is nice.

@developit developit added discussion Discussions and proposals enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed proposal labels Jan 29, 2020
@klvenky
Copy link

klvenky commented Aug 27, 2020

Is this taken? I am pretty new to OSS and the repo. I am not sure where to start. Can someone help me out by pointing some resources?

@developit
Copy link
Owner

Hi @klvenky - sorry for missing your question. Nobody has taken it yet, though I'm afraid I actually don't know where to start! I've never written or used a "babel macro" before. Maybe @nick-at-hawkfish knows?

@Haroenv
Copy link

Haroenv commented Apr 6, 2023

it likely is something like this:

const { createMacro, MacroError } = require("babel-plugin-macros");
const html = require("html");

module.exports = createMacro(prevalMacros);

function prevalMacros({ references, state, babel }) {
  references.default.forEach(referencePath => {
    if (referencePath.parentPath.type === "TaggedTemplateExpression") {
      asTag(referencePath.parentPath.get("quasi"), state, babel);
    } else if (referencePath.parentPath.type === "CallExpression") {
      asFunction(referencePath.parentPath.get("arguments"), state, babel);
    } else {
      throw new MacroError(
        `dedent.macro can only be used as tagged template expression or function call. You tried ${
          referencePath.parentPath.type
        }.`
      );
    }
  });
}

function asTag(quasiPath, { file: { opts: { filename } } }, babel) {
  const string = quasiPath.parentPath.get("quasi").evaluate().value;
  const { types: t } = babel;

  quasiPath.parentPath.replaceWith(t.stringLiteral(html(string)));
}

function asFunction(argumentsPaths, { file: { opts: { filename } } }, babel) {
  const string = argumentsPaths[0].evaluate().value;
  const { types: t } = babel;

  argumentsPaths[0].parentPath.replaceWith(t.stringLiteral(html(string)));
}

I haven't tried it though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Discussions and proposals enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed proposal
Projects
None yet
Development

No branches or pull requests

4 participants