Skip to content
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

@babel/node not recognizing ignores from babel.config.js when running babel-node #11892

Open
jcreamer898 opened this issue Jul 29, 2020 · 9 comments

Comments

@jcreamer898
Copy link

jcreamer898 commented Jul 29, 2020

Bug Report

Current behavior
It's possible it's a "feature", but seems like a bug.

So, I have a babel config that has an ignore function in it. Mostly because where I work we have a couple of internal node modules we publish that have some dist folders with import export statements.

The babel.config.js looks more or less like this...

module.exports = (api) => {
    api.cache(true);

    return {
        presets: ['@babel/env'],
        ignore: [function(filepath) {
            if (filepath.includes('my-npm-pkg')) {
                return false;
            }

            return filepath.includes('node_modules');
        }]
    };
};

When I run the following...

babel-node ./index.js

I get the following error...

However, if I run it with an empty ignore, like this...

babel-node --ignore ' ' ./index.js

Everything works...

Expected behavior
I'd expect that the ignore should be taken from the babel.config.js even if I don't have an --ignore in the command line args.

Environment
I'm running on node 12.17.0.

"@babel/core": "^7.10.5",
"@babel/node": "^7.10.5",
"@babel/preset-env": "^7.10.4"

I also created an example repository here...

https://github.com/jcreamer898/babel-node-ignore

Possible Solution
I'm not entirely sure, but maybe it has something to do with https://github.com/babel/babel/blob/main/packages/babel-register/src/node.js#L140?

Does there need to be a check for the babel config's ignore here? It didn't seem like that to me because when I console logged the options getting passed here, it didn't even seem to have the ignore from my babel.config.js.

https://github.com/babel/babel/blob/main/packages/babel-node/src/_babel-node.js#L94

So, my suspicion is that the --ignore ' ' is just nuking the ignore option totally, and babel.config.js is firing on a file by file basis later on as they're processing individual files?

@babel-bot
Copy link
Collaborator

Hey @jcreamer898! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@jcreamer898
Copy link
Author

For what it's also worth I finally got an ignore regex to work on the command line as well, just took me a while to get it right...

babel-node --ignore '\/node_modules\/(?!my-npm-pkg)\/' index.js

@sz-coder
Copy link
Contributor

Can you update the second screenshot? You included the same screenshot twice...

@jcreamer898
Copy link
Author

Oops, sorry, sec.

@jcreamer898
Copy link
Author

Fixed.

@sz-coder
Copy link
Contributor

sz-coder commented Jul 30, 2020

Not sure if I understand correctly but shouldn't babel-node --config-file ./babel.config.js ./index.js yield the same result as babel-node --ignore '\/node_modules\/(?!my-npm-pkg)\/' ?

Because running it with the --config-file flag gives the same error:

$ babel-node --config-file ./babel.config.js ./index.js

babel-node-ignore/node_modules/my-npm-pkg/index.js:1
export function main() {
^^^^^^

SyntaxError: Unexpected token 'export'

@sz-coder
Copy link
Contributor

sz-coder commented Jul 30, 2020

I played around with your babel.config.js a bit... seems like it doesn't matter whether the function returns true or false, both yield the same error. 🤔

ignore: [function(filepath, ctx) {
	console.log(filepath); // only prints my-npm-pkg 

	return true;
}]
ignore: [function(filepath, ctx) {
	console.log(filepath); // only prints my-npm-pkg 

	return false;
}]

@jcreamer898
Copy link
Author

Right, exactly, it's like it doesn't work at all when the config file is being used. I think the "default" --ignore is taking over whatever is in the babel config.

@Edmond-XavierCollot
Copy link

I have the same issue, the only and ignore options of my babel.config.json are ignored when running babel-node.
I have to specify the options directly in the cli
babel-node --only src,my-library src/index.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants