Skip to content

Support babel macros on dependencies. #9487

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

Open
elas7 opened this issue Aug 15, 2020 · 0 comments
Open

Support babel macros on dependencies. #9487

elas7 opened this issue Aug 15, 2020 · 0 comments

Comments

@elas7
Copy link

elas7 commented Aug 15, 2020

Currently, CRA only supports babel macros when they are in the application's code. It doesn't support macros on dependencies.

Is your proposal related to a problem?

Yes.

I have an internal library meant to be used by several React applications.

This library uses the babel plugin "preval" through a babel macro to transform its code at build-time. These transformations can't happen when building the library; they need to happen when building the application because they depend on the application's environmental variables.

Unfortunately, I can't use this library on CRA applications without ejecting.

Here's a code example:

node_modules/my-library/index.js

import preval from 'babel-plugin-preval/macro'

// complex computation based on environmental variables
export const one = preval`module.exports = 1 + 2 - 1 - 1`

src/App.js

import React from 'react';
import { one } from 'my-library';

function App() {
  return <div>The number: {one}</div>;
}

export default App;

This produces the following error: MacroError: The macro you imported from “undefined” is being executed outside the context of compilation with babel-plugin-macros.

Describe the solution you'd like

This could be solved by adding babel-plugin-macros to the list of plugins on packages/babel-preset-react-app/dependencies.js.

Currently, babel-plugin-macros is present only on packages/babel-preset-react-app/create.js.

Additional context

Related issues, PRs, and resources:

This pattern works in Gatsby, but I couldn't find documentation about it, so I don't know if it's officially supported. Here's the relevant PR:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant