question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

isMacrosName doesn't provide the full path

See original GitHub issue
  • babel-plugin-macros version: 3.1.0
  • node version: 12.x
  • npm version: 6.14

Relevant code or config

const macrosRegex = /[./]macro(\.js)?$/;
const testMacrosRegex = v =>
  macrosRegex.test(v) && v.indexOf('foobar/macro') === -1;

module.exports = {
  plugins: [
    [
      'macros',
      {
        isMacrosName: testMacrosRegex,
      },
    ],
  ],
};

File structure:

node_modules
  foobar
    index.js
    macro.js
app.js
// node_modules/foobar/index.js
import macro from './macro';

// app.js
import foobar from 'foobar';

What you did:

I tried to exclude foobar/macro from babel-plugin-macros

What happened:

The file is treated as a Babel macro.

Problem description:

The problem I’m having is with relative macro-like imports such as:

import macro from './macro';

The above code is trying to import a file named macro but that is not a Babel macro.

The custom configuration isn’t able to filter out this specific import because the path provided to testMacrosRegex is ./macro rather than the absolute one (node_modules/foobar/macro).

Suggested solution:

isMacrosName should provide an “absolute-like” import (starting from the root of the project) in order to allow to filter out unwanted macro files.

Alternatively, the plugin could provide a configuration option to specify paths that should be ignored.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
conartist6commented, Jul 13, 2021

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.

1reaction
conartist6commented, Jul 13, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

tree Full Path Prefix not displayed - Apple Stack Exchange
Seems it's showing the relative path, in this case, current directory ".", specifying the full path as an option works though.
Read more >
How to get an absolute file path in Python - Stack Overflow
It's absolute path, not absolute file or directory. If you want an existence check, call os.path.exists . To the contrary, systems like ...
Read more >
File explorer doesn't show full file path - Microsoft Community
File explorer doesn't show full file path. Cannot expand the columns in the above clipping. Looks like you are not in the Details...
Read more >
Get absolute path of files using 'find' command - Ask Ubuntu
find /home/your_user -name foo . As OP is using "../" , hard to guess the full path to give in an answer. –...
Read more >
What Is a Path? (And Other File System Facts)
Relative or Absolute? Symbolic Links. What Is a Path? The following figure shows a sample directory tree containing a single root node. Microsoft...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found