-
Notifications
You must be signed in to change notification settings - Fork 135
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
Cannot apply macros when imported from a module outside of default resolution paths #87
Comments
I am not sure if this is related, but I am trying to go from CRA 2.0-alpha to 2.0 and they dropped support for e.g. where that file is import gql from 'graphql-tag.macro';
export default gql`
query users {
id
}
`; |
@kentcdodds I am trying to do exactly the same (allowing macros to be loaded from custom directories) and had a look at the plugin code to send a PR. |
That's an interesting idea @almilo! I think we may be able to solve @Andarist's issue as well. I'm ok with eagerly resolving the config. What if the config looked like: // babel-plugin-macros.config.js
module.exports = {
options: {
moduleDirectories: ['node_modules', 'shared'],
alias: {
'pipeline.macro': require.resolve('./index.js'),
}
}
} The |
I was strugling with the same issue. My ugly solution until there's a better option (babel config is dynamically generated for us, added this to that script): const macrosPluginPath = require.resolve('babel-plugin-macros')
fs.readFile(macrosPluginPath, 'utf8', function(err, data) {
fs.writeFile(
macrosPluginPath,
data.replace(
'return resolve.sync(source, {',
`return resolve.sync(source, {
paths: [p.resolve(__dirname, '../../'), p.resolve(process.cwd(), 'node_modules')],
`
),
'utf8',
() => {}
)
}) It's a REALLY, REALLY BAD |
I just ran into this issue, this makes it pretty hard to use macros within a project, unless using relative imports everywhere. |
How's this going ? ran into this issue too. |
I'm using my own patched version, will make a PR next week.
…On Sun, Nov 24, 2019, 10:56 Cai Huanyu ***@***.***> wrote:
how's this going ? ran into this issue too.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#87?email_source=notifications&email_token=AAHLJQDVZXYWXRMPXAXBMPLQVJFWNA5CNFSM4F7HRG7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEFAHTWQ#issuecomment-557873626>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAHLJQGLOXVTOBSVULABBJDQVJFWNANCNFSM4F7HRG7A>
.
|
I might be seeing this same issue. I am happy to open a new issue otherwise. I am trying to use In the relay wrapper package:
In the sibling, I see this terminal output:
In the browser:
Since the macro works in its local package, but not when imported into another, I suspect it is related to this issue. |
Is it possible that it's not the latest version of babel-plugin-macros?
Because I created a PR to fix this before and it was merged some time ago.
…On Fri, Jul 10, 2020, 06:31 Vincze Viktor ***@***.***> wrote:
No, the problem here is that you don't have babel-plugin-macros in your
Babel config. Because of being missing, Webpack will simply compile macros
into your bundle whereas they are supposed to be a build time script only.
Adding babel-plugin-macros to your .babelrc should solve your problem.
On Fri, Jul 10, 2020, 03:35 Joseph Stiles ***@***.***>
wrote:
> I might be seeing this same issue. I am happy to open a new issue
> otherwise. I am trying to use relay in a monorepo with yarn workspaces.
> My relay implementation, graphql schema, and the queries live in one
> package that is compiled by Typescript and then imported into a sibling
> package. The sibling is a react app that render the wrapped relay component
> and refers to the queries by a key.
>
> In the relay wrapper package:
>
> "devDependencies": {
> "babel-plugin-relay": "^9.1.0",
> "react": "16.13.1",
> "react-dom": "16.13.1",
> "react-relay": "^9.1.0",
> "react-scripts": "3.4.0",
> "relay-compiler": "^9.1.0",
> "relay-compiler-language-typescript": "^12.0.3",
> "relay-config": "^9.1.0",
> "relay-runtime": "^9.1.0",
> "typescript": "^3.9.5",
>
> react-scripts start works just fine in the relay wrapper.
>
> In the sibling, I see this terminal output:
>
> Compiled with warnings.
>
> /node_modules/babel-plugin-macros/dist/index.js
> Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
>
> /node_modules/import-fresh/index.js
> Critical dependency: the request of a dependency is an expression
>
> In the browser:
>
> MacroError: The macro you imported from "undefined" is being executed outside the context of compilation with babel-plugin-macros. This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. Please see the documentation for how to configure babel-plugin-macros properly: https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md
>
> Since the macro works in its local package, but not when imported into
> another, I suspect it is related to this issue.
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#87 (comment)>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAHLJQERWJPUF2AEBOPAY33R2ZV5XANCNFSM4F7HRG7A>
> .
>
|
@wintercounter that's a good thought. I see that
|
I'm getting this for a macro that's inside a dependecy - at least I think that's the source of the problem. Using it inside a create-react-app webapp. Some version info (can't really tell which one matters):
Moving to
But I get the same error:
That's a runtime error, that shows only when the page with the component is opened in the browser. I'm looking for guidance about what to do / inspect. |
Well I don't necessarily know anything you don't, but I see that earlier in this thread a PR was submitted. There's some useful discussion on the PR about what was causing the issue, and also the PR shows that a fix was released in |
Yep, I'm trying to do that but I'm failing since hours. I have both the Node inspector activated and the client (browser) DevTools open with breakpoints, but none are being triggered. I must be missing something obvious. |
Can you share exactly what you're running? |
If you're trying to debug this through the |
The uber-simplified case is this: I have
Since Then the component in installed into
and it's imported in the final application;
Yes, I'm using the I succeeded at adding a breakpoint at the line |
I just meant for debugging, but I guess you figured it out. It doesn't really matter what's in your files I think, just where they're located. And you knew you'd see that |
I'm actually stuck there, I can't understand the call stack and determine who's setting (or not setting) it. |
Well you said |
Ah, I actually pasted the wrong line of code! I meant
I'd like to inspect the line of code you're referring to (which appears inside I tried adding a |
I misread. For the client side debugging, I'm running the For the server side, I've edited
to (see the arrow):
This causes the debugger to kick in during script execution:
and lets me open the Chrome Node-specific DevTools and see the log there too. I don't know if this is the proper way of launching the debugger in this case, I used it once some time ago. But no breakpoint seems to be triggered on that side now. Neither when running the |
Side note: I tried moving the |
Should I add an explicit configuration to |
No, create-react-app specifies its babel configuration programatically (ugh). Now the lines in question have changed, but my question remains the same. You tell me that you have a failure on the line that checks |
I'm using the Then that component is a dependecy of the React application that is throwing this error when I open the page that contains the above-mentioned component.
This is what I see inside the debugger (current line is 60): |
So the answer to my question is |
The only way it can have gotten there is if the macro actually didn't evaluate at all, meaning that the runtime is now attempting to execute the macro evaluation code, which should never happen. |
Oh, and
I don't get that. Are you saying that there should be no text at all, or that it should be something different than a string? My component is
Mmh ok, maybe there's a missing step somewhere. |
And this is a portion of the
|
Well you would certainly need |
Ha! Of couse. But would this mean that that macro will be evaluated at |
That's not how CRA works though. It has two babel configs, one for its own files, and one for deps. While it compiles macros for its own files, it makes no assumptions about code in dependencies, other than that they are valid ES. |
I'm going to close this issue because the problem it describes was fixed a while ago and now it seems we've resolved your issue. |
Out of curiosity, why would you want that to be evaluated at the App's build time? If the component has a build time I'd expect it to be when the component was built, not when the app using the component was built. |
Thanks for the support, I'll check the links in your last comments. I'm investigating a way to handle app updates in a consistent way - actually some users see the updates, some don't and do not even after a page refresh (only a refresh bypassing the cache works, but it really is unfriendly for the average user). The app build time is a prerequisite. Moving the component out in its own package will make it easily reusable among projects. |
@conartist6 Since you have an understanding of how CRA is configured, may I ask you a couple more questions? I've checked the files you linked and I'm trying to enable the plugin for the dependencies too. I added
Am I doing it the wrong way? |
I would strongly recommend that you check out the sources for That said, CRA sounds like a terrible fit for the level of control you want. CRA is made for novices where the primary goal is "don't let them hurt themselves". How it ever became a choice for corporate products with arbitrary requirements and advanced interoperability needs beats me. |
babel-plugin-macros
version: 2.4.2node
version: 8.9.4npm
(oryarn
) version: npm 5.6.0Hi, Kent! Macros are super cool. Thanks for building this plugin. lingui-js is a really cool use case that I've been integrating lately. It uses macros to identify and extract translatable strings at build time, which simplifies react internationalization a great deal. However, I've run into an issue regarding node
resolve
that I could use your advice on before I try and submit a patch.Relevant code or config
I have a project that loads modules into my bundle using a webpack alias for a directory outside of my project root. These modules in turn import macros. For example:
require("#EXTERNAL_PLUGINS/index.js")
where #EXTERNAL_PLUGINS is a webpack alias for something like "../external_plugins"
Babel fails to load these modules if they import a macro because of a recent change to applyMacros that adds
resolve.sync
with a basedir set as the source file directory (which again, is outside my working folder and therefore doesn't have node_modules)Here is the change:
1785a0f#diff-1fdf421c05c1140f6d71444ea2b27638R155
What happened:
resolve.sync throws an exception because it is unable to locate the macro with the configured basedir. Everything works correctly when using babel-plugin-macros 2.4.1
Reproduction repository:
https://github.com/brandonc/load-macros-outside-package
Suggested solution:
I took a stab at a fix but was unable to find a suitable basedir to use with resolve.sync. This is a hard problem with zero configuration. Looking for ideas from you for a suitable change.
The text was updated successfully, but these errors were encountered: