isMacrosName doesn't provide the full path
See original GitHub issuebabel-plugin-macros
version: 3.1.0node
version: 12.xnpm
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:
- Created 2 years ago
- Reactions:1
- Comments:9
Top GitHub Comments
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 insidenode_modules
. The submitter configured jest withtransformIgnorePatterns: ['node_modules/(?!pgk_name/)']
, wherepkg_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 thebabel-jest
transform to that package should be fine though.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.