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.config.js read but not working via babel/core parseSync. #10610

Open
TheHighriser opened this issue Oct 28, 2019 · 3 comments
Labels

Comments

@TheHighriser
Copy link

@TheHighriser TheHighriser commented Oct 28, 2019

Bug Report

Current Behavior
I have a custom script which is importing @babel/core and is using the parseSync method. I pass the option for rootMode (so it finds the babel.config.js) and the filename. I am using a plugin for allowing decorators:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
};

When I now try to parse the source code it always throws ILLEGAL token. Which is the line with the decorator. If I remove the plugin but pass parserOpts including the decorator plugin, the source code parses fine.

Input Code

NOT WORKING (With the babel.config.js in combination above): It says ILLEGAL Token

import * as babel from '@babel/core';

babel.parseSync(source, {
    rootMode : "upward",
    filename : "Something.js",
});

WORKING: Parses without any problems. If I also use the babel.config.js in combination with the parserOpts, it says that I can use the decorator plugin simultaneously with the other decorator plugin.

import * as babel from '@babel/core';

babel.parseSync(source, {
    parserOpts: {
    ...,
    plugins: [
        ['decorators', {decoratorsBeforeExport: true}]
    ]
}
});

Expected behavior/code
I would expect that the code parses fine with the plugin defined through the babel.config.js. Or is this some misunderstanding from my side? Is this not possible?

Environment

  • Babel core version(s): 7.6.4
  • Node/npm version: Node 10.15.3 / NPM 6.4.1
  • OS: Windows 10
  • Monorepo: no
  • How you are using Babel: Via Node script
@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Oct 28, 2019

Hey @TheHighriser! 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."

@JLHwung

This comment has been minimized.

Copy link
Contributor

@JLHwung JLHwung commented Oct 28, 2019

I could not reproduce this issue on my local environment.

Note that the configuration

babel.parseSync(source, {
    parserOpts: {
    ...,
    plugins: [
        ['decorators', {decoratorsBeforeExport: true}]
    ]
}
});

contradicts to the babel.config.js you have listed before

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ["@babel/plugin-proposal-decorators", { "legacy": true }]
  ]
};

because when decoratorsBeforeExport is true, legacy must be false. Could you offer a reproducing repository?

@TheHighriser

This comment has been minimized.

Copy link
Author

@TheHighriser TheHighriser commented Oct 29, 2019

Thanks for the real quick answer here. Probably this is a misunderstanding from my side.

So the question is if the babel.config.js file is able to do the same work as the options above in the parseSync. So if you would remove the plugin from the parseSync block above, would it still be able to parse the decorator (because it is configured in the babel.config.js file) - Is this possible? Because that's what I am asking myself and that's what I am not able to do?

Or are those two configs, two different things and the parser only takes options from the parserOpts block?

My main goal is to implement a parsing which is using any babel.config.js file. I though this might be possible over the parseSync method as it reads the babel.config.js. So if any user is installing my module and uses the parser it will automatically take the babel.config.js and will be able (for example) to parse his decorators. Or do I always need to additionally pass the parserOpts block with the decorator option in it? Or are these two different things?

Thanks for the help!

P.S. Sadly I have no repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.