Comments (9)
All that said, I do think isMacrosName
has clear room for improvement. I think it should be given a second argument: the path of the file the import or require statement appears in. This would retain backwards compatibility but also ensure that any valid use cases could be met. But if I am right about what your problem is it would not be the solution you need.
from babel-plugin-macros.
Here's my summation: since babel-plugin-macros
works by "stealing" some syntaxes that would otherwise be valid JS, it must never be run on code that has not explicitly opted into being transpiled with the macros plugin. This is not a flaw, but rather an explicit design choice.
This issue seems to have been caused by a bad jest configuration. In create-react-app the default set of babel transforms includes babel-plugin-macros
, and that set of transforms is configured to run only on files which are not inside node_modules
. The submitter configured jest with transformIgnorePatterns: ['node_modules/(?!pgk_name/)']
, where pkg_name
is the name of a package which has syntactic conflicts with macros. The problem appears to be that method of reconfiguring jest, which causes it to apply a set of plugins which includes macros to module code, which is incorrect. Explicitly applying the babel-jest
transform to that package should be fine though.
from babel-plugin-macros.
Yeah I see your problem, but I'm not quite sure about the solution. Could you tell me a little more about your setup? You must be running babel on code inside node_modules
, but I am surprised that babel would have the macros plugin enabled when transpiling code in node_modules
. I would say what you're experiencing is really a very general problem: it is not safe to run arbitrary transforms on module code because unless the syntax in use is part of the ECMAScript specification you won't know how the module author expected it to be interpreted.
from babel-plugin-macros.
Let's say I was transpiling for .. of
loops in code. Because the spec tells us how they should behave I can safely transpile them down even in node_modules
as long as both the transpiled and untranspiled code have the same semantics.
But there are multiple ways of transpiling for .. of
loops: in loose
mode they are simply translated to index loops on the assumption that the target of the loop is an array. But for that code to work it requires extra knowledge of the program: specifically that any for .. of
loops are always over arrays. If you applied that same loose
transform to all of node_modules
, you would expect things to break because you've altered the meaning of the language. I think it's quite reasonable as a rule that code in node_modules
must be module code executable by node, and it must be free to use any syntax (including import paths) which is valid and legal.
from babel-plugin-macros.
Thanks for the reply, in my case I have a Create React App and I'm trying to use the @kitware/vtk.js library, that internally has a macro.js
file.
@kitware/vtk.js is published with ES Modules (import/export) so in order to use it with Jest I need to put it in the transformIgnorePatterns
, and this originates the error I described above.
I think the solution you described should work for my case, although CRA doesn't allow to configure the babel-plugin-macros so I would need to resort to craco.
from babel-plugin-macros.
I was pretty sure CRA didn't run macros on node_modules
code...
from babel-plugin-macros.
It doesn't, but if you add transformIgnorePatterns
to the Jest configuration then it seems like it starts doing it.
To be fair the best way to address this issue that comes to mind would be for the plugin to check that the imported macro is actually a Babel Macro.
from babel-plugin-macros.
To be fair the best way to address this issue that comes to mind would be for the plugin to check that the imported macro is actually a Babel Macro.
That sounds nice, but really I think it would make a mess. One of the benefits of transpilation is that it's a pure 1:1 operation. Given the same input content you should always get the same output. Reading other module files to decide how transpile a particular input would mean that results of transpilation could never be properly cached. Right there you'd be breaking jest.
It doesn't, but if you add transformIgnorePatterns to the Jest configuration then it seems like it starts doing it.
Weird. And that package publishes non-esm code too so I don't see why jest would have problems. If it doesn't understand module code it shouldn't be reading the module field in package.json
from babel-plugin-macros.
There's a bit of confusion, there are two vtk.js packages, one is a legacy package that also ships umd, one is a bundler-friendly one that doesn't ship UMD though (https://unpkg.com/browse/@kitware/[email protected]/).
from babel-plugin-macros.
Related Issues (20)
- Can automatically import a macro ? HOT 1
- Add hook after all files have been processed? HOT 6
- Failure with ts-jest and babel HOT 13
- Migrate to github actions HOT 1
- "Duplicate declaration" error due to identifier traversal change HOT 6
- Add to docs that plugin config can also come from babel plugin config HOT 1
- Initialization, scopes and assignments - possible bugs / possible improvements HOT 10
- How to show errors in vscode or other editor when macro is throwing MacroError or buildCodeFrameError? HOT 3
- How to apply macro in live coding app (made with create-react-app) HOT 2
- How to handle nests macro HOT 2
- webpack 5 build error HOT 5
- Maintenance is frozen HOT 10
- Feature: re-entrant macros. HOT 3
- Can we run babel-plugin-macros in the browser HOT 10
- Critical dependency: require function is used in a way in which dependencies cannot be statically extracted HOT 10
- Proposal: Allow macros to be defined in `.macro.mjs` files HOT 6
- Depending on vulnerable version of cosmiconfig HOT 14
- Can we bump "cosmiconfig" dependency to its latest HOT 2
- Facing issue with fortawesome integration with babel-plugin-macros. HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-macros.