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

Document Babel macros support and common uses #3858

Open
gaearon opened this issue Jan 18, 2018 · 17 comments
Open

Document Babel macros support and common uses #3858

gaearon opened this issue Jan 18, 2018 · 17 comments

Comments

@gaearon
Copy link
Contributor

gaearon commented Jan 18, 2018

Need to keep track:

@gaearon gaearon added this to the 2.0.0 milestone Jan 18, 2018
@gaearon
Copy link
Contributor Author

gaearon commented Jan 18, 2018

cc @kentcdodds do you have a list somewhere/

@threepointone
Copy link

partial application with https://github.com/citycide/param.macro

@threepointone
Copy link

@kentcdodds
Copy link
Contributor

I should note that some plugins have still not upgraded from babel-macros to babel-plugin-macros. In any case, the documentation of babel-plugin-macros indicates that people should use the keyword babel-plugin-macros in their package.json to facilitate finding available macros.

@threepointone
Copy link

idx! https://github.com/dralletje/idx.macro

@kentcdodds
Copy link
Contributor

kentcdodds commented Jan 18, 2018

Also, I'll be giving a talk at a meetup in two weeks about using babel-plugin-macros with create-react-app. I'll make sure that's recorded and add a link to the docs 👍

@kentcdodds
Copy link
Contributor

I just added a macros.md file to the babel-plugin-macros docs. Feel free to direct people there!

@kentcdodds
Copy link
Contributor

Here's the recording of my talk 😄

@Timer Timer added this to Meh in 2.0 Mar 9, 2018
@Downchuck
Copy link
Contributor

A sterling example of a painful case of css plugin:
umijs/babel-plugin-import#166

@lifeiscontent
Copy link
Contributor

not sure if this is create-react-app related or webpack related as @kentcdodds pointed out, but the errors when something doesn't compile in babel-macros are very unhelpful. kentcdodds/babel-plugin-macros#79

@kentcdodds
Copy link
Contributor

I believe it has nothing to do with babel macros specifically and is more generally babel...

@lifeiscontent
Copy link
Contributor

right, sorry for not clarifying that @kentcdodds

@caedmon
Copy link

caedmon commented Sep 15, 2018

Not sure if this is the right place to ask this, but is babel-plugin-macros supposed to be working with 2.0.0-next.a671462c? I've tried to use it but haven't been able to get more than the rather opaque 'Failed to compile.' when I try to use a macro.

@mxstbr
Copy link
Contributor

mxstbr commented Oct 15, 2018

styled-components v4 also has a Babel macro! 🎉 https://www.styled-components.com/docs/tooling#babel-macro

@petetnt
Copy link
Contributor

petetnt commented Oct 18, 2018

#5481 graphql-tag.macro documented in this one

@iansu iansu modified the milestones: 2.1.x, 3.x Mar 10, 2019
@ForestJohnson
Copy link

ForestJohnson commented Jan 24, 2020

I was looking at how to print a warning message regarding

import styled from 'styled-components/macro'

versus

import styled from 'styled-components'  

The problem is that users are going to be instructed to import styled from 'styled-components' for quite some time and they will have no idea why the displayName config (display Styled Component Names in the class name) is not working.

This is important so the DEV / QA person can tell whether a given element in the Browser Developer Tools is a TabularWhoosieWhatsitContainer or if it's a TabularWhoosieWhatsitFlexRow. Currently when they look at it they see something like <div class='sc-xHyW3'>and <div class='sc-JTy5as'>

When the users are not instructed to update to 'styled-components/macro', this issue is happening because the babel plugin for styled-components will never be loaded because it will never be included in the babel config that gets generated here

I checked whats trivially possible to do in the styled-components repo by putting console.log in the very top of the file in various places:

node_modules/styled-components/dist/styled-components.browser.cjs.js
node_modules/styled-components/dist/styled-components.browser.esm.js
node_modules/styled-components/dist/styled-components.cjs.js
node_modules/styled-components/dist/styled-components.esm.js
node_modules/styled-components/dist/styled-components.js
node_modules/styled-components/dist/styled-components-macro.cjs.js
node_modules/styled-components/dist/styled-components-macro.esm.js
node_modules/styled-components/dist/styled-components.min.js

I found that when I import styled from 'styled-components' the logs from styled-components.browser show up in the browser.

When I import styled from 'styled-components/macro' the logs from styled-components-macro show up in the build output.

So there is no great way to warn users about this at build time from within the styled-components repo, that I am aware of.

It may be possible to warn the user from inside create-react-app, but its easier said than done. It's not as easy as checking what packages they have installed because styled-components and styled-components/macro are the same package.

The tool that issues the warning would potentially have to be a macro/linter itself, that would go through your code looking for styled-components imports. I don't know how to set that up.

The alternative would be to issue the warning in the browser. But that seems like it might be risky, ugly, bad practice etc.

I suppose the other-OTHER alternative would be to modify babel-preset-react-app so that it loads babel-plugin-styled-components when present, I have an example of that here:

ForestJohnson@ccfe22b

The above does work (node_modules/babel-plugin-styled-components/lib/index.js will be executed at build time, and the displayNames will show up correctly in browser with import styled from 'styled-components' ) but it requires you to bust the Webpack/Babel cache for the change to "take", and I have no freaking clue how to get that to happen reliably at the right time. As a relatively naive user the only reliable way I found to do that was to completely delete and re-create the whole project folder.

@Annie-Huang
Copy link

using https://styled-components.com/docs/tooling#babel-macro will failed all my snapshot unit test, errors complains:
TypeError: macro_1.default. is not a function

Where XXX can be span, li, etc
Does anyone got the same problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
2.0
  
Meh
Development

No branches or pull requests