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

Is there a way to enable the allowTopLevelThis option? #3055

Closed
GianlucaGuarini opened this issue Nov 14, 2015 · 4 comments
Closed

Is there a way to enable the allowTopLevelThis option? #3055

GianlucaGuarini opened this issue Nov 14, 2015 · 4 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@GianlucaGuarini
Copy link

We have problems using babel 6 in our riot-compiler Is there a way to enable the allowTopLevelThis option?

@loganfsmyth
Copy link
Member

@GianlucaGuarini As mentioned already in that other issue, but would be invalid ES6.

In ES6, JavaScript is treated in two ways, as a "script", or as a "module". Only "module"-type javascript may use import and export, and the spec dictates that this in the context of an executing "module" is undefined.

Depending on your goals, I see two options:

  1. Exclude babel-plugin-transform-es2015-modules-commonjs so that the file will be transpiled more like a "script". If you don't actually need/want to allow ES6 module syntax, I'd recommend this.
  2. Implement your own Babel plugin, which would hoist all the import/export declarations to the top of the file, then wrap the body of the code in an IIFE that would set the this for your specific code.

@appedemic
Copy link

Specifically for this problem, the babel-plugin-transform-es2015-modules-commonjs has introduced a plugin option "allowTopLevelThis". However, there is currently no way to pass a plugin configuration option to a preset (#2756)

Regarding your options, is there a way to exclude babel-plugin-transform-es2015-modules-commonjs from the es2015 preset? Or would it require to create a custom preset?

@GianlucaGuarini
Copy link
Author

@loganfsmyth thanks for your answer I am just trying to find out whether babel 6 can consistently behave like the previous release using at least some options combined with its presets. Using a complicate list of presets just to solve this simple issue seems to me a bit an overkill also because this option is already there you just need to make it public somehow

@hzoo
Copy link
Member

hzoo commented Sep 10, 2016

We have preset options now, we could add an option to modify the modules plugins.

@hzoo hzoo closed this as completed Oct 4, 2016
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 6, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

4 participants