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
You may need an appropriate loader to handle this file type - when ES6 classes contain public or private field declarations #10216
Comments
https://github.com/tc39/proposal-class-fields You'd need to add this plugin to your babel config: https://babeljs.io/docs/en/babel-plugin-proposal-class-properties |
In my case I don’t want to downlevel since node 12 target does accept these fields are available natively |
We are using acorn (https://github.com/acornjs/acorn) as parser and it only support stage 4 proposals. |
yep with the Maybe using https://github.com/acornjs/acorn-stage3 in webpack makes sense, at least opt-in. |
Or the ability to add acorn plugins |
This would be perfect. This little problem also breaks: -> vercel/ncc#499 Personally I could just stay aways from public or private field declarations, but some of my dependencies (namely hapi.js) are already using it … |
acornjs/acorn#830 this seems to be the acorn issue |
Yes please. I've been bitten by this at least a few times now, because I always forget that just because Babel can parse something, it doesn't mean Webpack can (why don't you guys use the same parser again?). In the relevant case my process went something like this:
|
I think it can be solved in a more comfortable way:
|
Is there any progress on this feature? I hit this problem today. One reason to want to use native private fields is that the debugging experience of transpiled private fields is pretty painful. |
@wycats no, feel free to send a PR, it should be easy |
@sokra Acorn released today a new version that supports optional chaining. |
as a temporary solution;
{
...
"resolutions": {
"acorn": "npm:acorn-with-stage3"
},
"dependencies": {
|
Just ran into this today, which was strange because I never had the issue before. Turns out my current babel config was missing a plugin I had been using over the past couple years but forgot to copy over. I added that back in and it worked again. So as another temp fix, add this to your babel config, inside
|
I need public class fields as well for the neo.mjs framework. In general, we should re-think the approach to only add stage 4 proposals. While this was fine 1-2 years ago, these days most browser engines already implement the important stage 3 ones. Let's take a look at public class fields: At this point, even Opera & Safari support them. For neo.mjs, there is a design goal to use code which runs directly in all major browsers, resulting in not using Babel at all. Without the ability to specify acorn plugins as webpack configs, I can not add class fields into the code base, since it breaks the dist builds. I did try @OnurGvnc 's workaround. While this might work for yarn (not tested), it did not work for me using npm. In short: https://github.com/rogeriochaves/npm-force-resolutions is using a preinstall hook, which is already a problem, in case the package-lock.json is included inside the .gitignore. Without this file, the preinstall will break. Well, I could add it into the repo, but even then it did not work for me. The script itself did adjust the acorn dependencies inside the package-lock correctly, but the following npm install reverted the changes and webpack was using the default acorn package again. Maybe I am missing something. Right now the only thing I can think of what I could do is to fork webpack and create an own npm package just to change the acorn dependency. This feels wrong on too many levels (starting with new webpack versions). I am hoping that we can get the public class fields included soon (should be easy => just add the acorn-with-stage3 plugin). In case there are reasonable workarounds, please let me know. Best regards, |
I managed to make this work but it's a bit of a hack. First of all, install acorn-class-fields:
Next, put this at the top of
It's important to put this block before you |
FYI: This is now fixed in at least the current version of Webpack 5.35.1, the underlying acorn parser was recently upgraded to 8.2.x which adds Private/public fields to the parser, and webpack 5 is already set to pull the newest 8.x acorn parser. As long as your I tested by: (Created new test folder and moved to it)
|
Hi @NathanaelA, just tested this and it does look really good! |
I remove the dependency package Here is a minimal code to reproduce the bug:
|
off topic: i would not recommend using public class fields to define methods. fields get assigned inside the ctor on instance level and methods belong to the prototype (unless you want to duplicate them for each instance). |
This usually use for callback, auto bind the |
@muzuiget - I'm going to say this is probably a bug in webpack, if you turn off optimization ( Interestingly enough if you use |
@NathanaelA Can you provide example? Yep, it may be bug |
Here is the demo I used from muzuiget -- @alexander-akait Webpack.config.js (Same as first issue, but added disabling minimizing).
lib.js
index.js
|
Since this reached stage 4 only recently and acorn added this only 2 days ago, we need also need to add this to webpack parser to be able to handle webpack constructs in the new syntax (e. g. imports) |
I was previously using But as soon as I updated to Thank you for fixing this!!! 🙏🙏🙏 PS. As a side-note-- So basically, JS has become the |
@r002 Can you update webpack? https://github.com/tc39/proposals/blob/master/finished-proposals.md |
@alexander-akait Yes, I updated Webpack and that solved the problem! I was just confirming with you guys that everything's copacetic now-- thank you for updating Webpack to support the new spec! 🙏🙏🙏 |
I think we can close it, idea from #10216 (comment)
Is not easy, the main problem here - handling new AST nodes, it requires many changes, including parser/plugins for optimizations/etc. Even we allow setup plugins it can still potentially break your code or behave unpredictably. So webpack supports only stage 4, when new syntax will be part of stage 4 webpack will support it. Anyway feel free to open new issues when something will be in stage 4, and yes, we are watching this too. |
What is the current behavior?
When using public/private field declarations in an es6 class. Webpack fails to bundle with the below error
If the current behavior is a bug, please provide the steps to reproduce.
index.js
webpack.config.js
What is the expected behavior?
Code gets bundled.
Other relevant information:
webpack version: 4.41.5
Node.js version: v12.12.0
Operating System: macOS Catalina
Additional tools:
The text was updated successfully, but these errors were encountered: