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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: [7.16.10] [target:node:12] Class fields are not enabled #14188

Closed
1 task
jlowcs opened this issue Jan 20, 2022 · 9 comments
Closed
1 task

[Bug]: [7.16.10] [target:node:12] Class fields are not enabled #14188

jlowcs opened this issue Jan 20, 2022 · 9 comments
Labels
i: bug i: regression outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@jlowcs
Copy link
Contributor

jlowcs commented Jan 20, 2022

馃捇

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

With the following configuration:

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

	return {
		sourceType: 'unambiguous',
		presets: [
			[
				'@babel/preset-env',
				{
					useBuiltIns: 'entry',
					corejs: 3,
					loose: false,
					targets: { node: '12' },
				},
			],
			'@babel/preset-typescript',
		],
	};
};

And a simple file like this one:

export class Test {
	private foo: unknown = {};
}

Compilation fails:

image

Configuration file name

babel.config.js

Configuration

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

	return {
		sourceType: 'unambiguous',
		presets: [
			[
				'@babel/preset-env',
				{
					useBuiltIns: 'entry',
					corejs: 3,
					loose: false,
					targets: { node: '12' },
				},
			],
			'@babel/preset-typescript',
		],
	};
};

Current and expected behavior

The following file should compile, but I'm getting an error:

export class Test {
	private foo: unknown = {};
}

image

Environment

System:
OS: macOS 11.6.2
Binaries:
Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
npmPackages:
@babel/cli: ^7.16.8 => 7.16.8
@babel/core: ^7.16.10 => 7.16.10
@babel/node: ^7.16.8 => 7.16.8
@babel/parser: ^7.16.10 => 7.16.10
@babel/plugin-syntax-import-meta: ^7.10.4 => 7.10.4
@babel/plugin-transform-runtime: ^7.16.10 => 7.16.10
@babel/preset-env: ^7.16.10 => 7.16.10
@babel/preset-react: ^7.16.7 => 7.16.7
@babel/preset-typescript: ^7.16.7 => 7.16.7
@babel/register: ^7.16.9 => 7.16.9
@babel/runtime: ^7.16.7 => 7.16.7
@babel/traverse: ^7.16.10 => 7.16.10
babel-jest: ^27.4.6 => 27.4.6
babel-loader: ^8.2.3 => 8.2.3
babel-plugin-styled-components: ^2.0.2 => 2.0.2
eslint: ^8.7.0 => 8.7.0
jest: ^27.4.7 => 27.4.7
webpack: ^4.46.0 => 4.46.0

Possible solution

No response

Additional context

Might be related to #14169?

Changing the target to node 14 or 16 fixes the issue, but that opens another can of worms for me...

@babel-bot
Copy link
Collaborator

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

@nicolo-ribaudo
Copy link
Member

Could you check if @babel/preset-env 7.16.11 fixes this bug?

@jlowcs
Copy link
Contributor Author

jlowcs commented Jan 20, 2022

@nicolo-ribaudo not quite

It does fix that specific error in the sense that it doesn't fail anymore, but it still returns code that includes the field, which is not supported by node 12 afaik:

image

And that results in an error when using webpack 4 and babel-loader.

image

That actually looks like the "can of worms" I mentioned above. It seems to be related to the version of acorn that webpack 4 uses that doesn't support that specific feature.

@jlowcs
Copy link
Contributor Author

jlowcs commented Jan 20, 2022

For reference, here's what the previous version of babel returns:

image

@JLHwung
Copy link
Contributor

JLHwung commented Jan 20, 2022

And that results in an error when using webpack 4 and babel-loader.

Webpack 4 does not support ES2022, you need to upgrade to webpack 5 or always enable @babel/plugin-proposal-class-properties.

@jlowcs
Copy link
Contributor Author

jlowcs commented Jan 20, 2022

Oh, right, apparently node 12 does support private class fields.

I guess explicit @babel/plugin-proposal-class-properties is the way to go until we have the bandwidth to migrate to webpack 5.

@jlowcs
Copy link
Contributor Author

jlowcs commented Jan 20, 2022

thank you both

@nicolo-ribaudo
Copy link
Member

I'm closing this issue since Babel now produces code that it still valid according to your targets, even if this might look like a regression (because it throws with webpack 4).

We might introduce a webpack: X target in the future, but for now yes I suggest enabling @babel/plugin-proposal-class-properties explicitly!

@jlowcs
Copy link
Contributor Author

jlowcs commented Jan 20, 2022

We might introduce a webpack: X target in the future

I love that idea!

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Apr 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: regression 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