Cannot apply macros when imported from a module outside of default resolution paths
See original GitHub issuebabel-plugin-macros
version: 2.4.2node
version: 8.9.4npm
(oryarn
) version: npm 5.6.0
Hi, 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:
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.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:37 (4 by maintainers)
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):
It’s a REALLY, REALLY BAD
solution. Once support will be added to customize this please allow us to configure this at babelrc atplugins
definition, not only through cosmiconfig files.I would strongly recommend that you check out the sources for
create-react-app
if you haven’t already. Github’s search is orders of magnitude worse than what you can do from and IDE pointed at the codebase itself. For example on Github I couldn’t figure out where that dependencies config was used, but locally I discovered that it’s here, which means that one possibility for why you aren’t seeing your changes is the caching.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.