-
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
Can we run babel-plugin-macros in the browser #186
Comments
rubber ducked myself 🐣. I figured out how to pass args.
I'll reopen if I have more questions but this might be resolvable on my own. |
Thanks for sharing. Perhaps your experience will help someone else out down the line! |
Thanks @conartist6. I'm actually very close to running babel-plugin-macros in the browser with limited hacks. However I ran into one issue that seems quite possible to fix. babel-plugin-macros calls babel-plugin-macros/src/index.js Lines 300 to 302 in 8fc6383
Would you be open to discussing changes to prevent This is the only issue preventing me from running babel-plugin-macros in the browser right now which is pretty incredible so cheers 🍻. |
How is it failing right now? It looks to me like the code should be more or less fine when the file is not found. Also I notice that you've pointed to a spot where |
We're on the same page about passing the options. The error isn't due to failure to find the file. The error is due to Here is the error I get.
Cosmiconfig requires import fresh which calls resolveFrom which requires module. Unfortunately module is a node built-in so it fails to resolve in the browser. I can try to get around this by mocking module, but it gets insanely messy. Would be way easier if we could somehow skip the cosmiconfig search altogether when we're in a browser environment. It does look like the current code works even when the config isn't found which makes two solutions possible.
Hopefully that makes sense? |
Everything about cosmiconfig, including its require and attempt to find a config file, is wrapped in a try catch block: babel-plugin-macros/src/index.js Lines 269 to 280 in 8fc6383
It only throws the error because it cannot find any config: babel-plugin-macros/src/index.js Lines 304 to 316 in 8fc6383
You could suppress the error for now just by providing an empty config. |
TLDR: got it working but agree the error looks like it should be caught so closing til I can figure out whats going on.
It definitely looks like the error I showed should be getting caught there and I don't have a good explanation for why it isn't.
Unfortunately I get the module resolution error inside that try catch statement 🤔 so I don't even hit the empty config issue. My best guess is this has something to do with the fact that I'm using nextjs. Maybe they have an early exit or something if any module fails to resolve during server side rendering so we're not even hitting the try catch statement. I'm not sure 🤷♂️ . I wrote a patch which implements option 1 and checks if we're in a browser environment before attempting to find a config file. The patch just wraps the try-catch statement you linked with diff --git a/node_modules/babel-plugin-macros/dist/index.js b/node_modules/babel-plugin-macros/dist/index.js
index 4501c0e..bf2fb0c 100644
--- a/node_modules/babel-plugin-macros/dist/index.js
+++ b/node_modules/babel-plugin-macros/dist/index.js
@@ -252,19 +252,21 @@ function applyMacros({
}
function getConfigFromFile(configName, filename) {
- try {
- const loaded = getConfigExplorer().search(filename);
-
- if (loaded) {
+ if (typeof window === 'undefined') {
+ try {
+ const loaded = getConfigExplorer().search(filename);
+
+ if (loaded) {
+ return {
+ options: loaded.config[configName],
+ path: loaded.filepath
+ };
+ }
+ } catch (e) {
return {
- options: loaded.config[configName],
- path: loaded.filepath
+ error: e
};
}
- } catch (e) {
- return {
- error: e
- };
}
return {}; With that change I was able to get a babel macro running in the browser. Not sure where to go from here. |
Thanks for spending the time to look at this. I know it is an absolute wall of text 😅 |
Certainly! I spend a lot of time building open source things (mostly not this) but I really like getting a glimpse into the ways people are using things and whether the designs I support hold up to unforeseen conditions and challenges. In this case I think causally related errors would have helped a lot in figuring out what's going on. It's difficult to understand the flow of exception states when |
Not sure if I'll have the bandwidth to follow up on this but to close this out I published a demo with babel-plugin-macros running in the browser https://twin-playground.vercel.app/ and I open sourced the code in this repo. All of the weird hacks I did to get this working are explained in code comments on this commit. |
I am trying to transpile code containing a babel macro in a browser environment.
The use case is for a live coding playground.
Unfortunately when I try to use babelPluginMacros in a browser environment I run into issues because babelPluginMacros assumes a file system is available.
babel-plugin-macros/src/index.js
Lines 65 to 73 in 8fc6383
Relevant code or config
What you did
tldr: monkey patching through webpack config but it isn't working
First I ran into issues with
fs
since I'm running in the browser, so I set my webpack config to not fallback for fs.Then I ran into issues with
fs.statSync
which is called byresolve.sync
that I linked to earlier. So I added an empty function mock for resolve.Now I'm getting an error for
resolve.sync
is not a function which makes sense cause I setresolve
to be an empty function.I can keep going down this monkey-patching rabbit hole but I feel like I must be doing something wrong.
Ideas for Solutions
macrosPlugin
takes args. Is there any way for me to specify those? If I could override nodeResolvePath and require then I would be done.babel-plugin-macros/src/index.js
Lines 80 to 85 in 8fc6383
resolvePath
. Once I get tointeropRequire
I think I can just alias it with webpack.babel-plugin-macros/src/index.js
Lines 213 to 215 in 8fc6383
I'm completely new to this stuff so would love any feedback from someone smarter than me.
Apologies for the issue on a dormant repo. If there is a better place to ask please let me know and I'll close this and move it.
babel-plugin-macros
version: 3.1.0node
version: 16.12.0npm
version: 8.1.0The text was updated successfully, but these errors were encountered: