Skip to content

Commit dddbada

Browse files
chriskrychorwjblue
authored andcommitted
Properly handle class properties proposal
Historically, we only add the `@babel/plugin-proposal-class-properties` so that we make sure the ordering is right with the decorators proposal (otherwise, it can end up compiling in the wrong order). With a recent version of `@babel/preset-env` and, transitively, `caniuse-lite`, this resulted in cases where we added that plugin but *not* related plugins for private class properties, which in turn triggered a Babel assertion about not adding the properties together as appropriate when the caniuse database (correctly) reported that . The fix is: 1. Bump to a more recent version of `@babel/preset-env`, which comes with a correspondingly bumped version of `caniuse-lite`, which in turn correctly understands what the latest versions of targeted browsers are. 2. Include in `ember-cli-babel` itself a check for whether we even *need* to add the plugin, and only provide it when the provided `targets` indicate that they require it. Resolves #419
1 parent 4c3b909 commit dddbada

6 files changed

+2566
-1334
lines changed

lib/babel-options-util.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function _getPresetEnv(config, project) {
3030
}
3131

3232
function _getModulesPlugin() {
33-
const resolvePath = require("./relative-module-paths")
34-
.resolveRelativeModulePath;
33+
const resolvePath =
34+
require("./relative-module-paths").resolveRelativeModulePath;
3535

3636
return [
3737
[require.resolve("babel-plugin-module-resolver"), { resolvePath }],
@@ -296,7 +296,12 @@ function _getHelperVersion(project) {
296296
return APP_BABEL_RUNTIME_VERSION.get(project);
297297
}
298298

299-
function _buildClassFeaturePluginConstraints(constraints, config, parent, project) {
299+
function _buildClassFeaturePluginConstraints(
300+
constraints,
301+
config,
302+
parent,
303+
project
304+
) {
300305
// With versions of ember-cli-typescript < 4.0, class feature plugins like
301306
// @babel/plugin-proposal-class-properties were run before the TS transform.
302307
if (!_shouldHandleTypeScript(config, parent, project)) {
@@ -307,7 +312,14 @@ function _buildClassFeaturePluginConstraints(constraints, config, parent, projec
307312
return constraints;
308313
}
309314

310-
function _addDecoratorPlugins(plugins, options, config, parent, project) {
315+
function _addDecoratorPlugins({
316+
plugins,
317+
options,
318+
config,
319+
parent,
320+
project,
321+
isClassPropertiesRequired,
322+
}) {
311323
const { hasPlugin, addPlugin } = require("ember-cli-babel-plugin-helpers");
312324

313325
if (hasPlugin(plugins, "@babel/plugin-proposal-decorators")) {
@@ -341,7 +353,7 @@ function _addDecoratorPlugins(plugins, options, config, parent, project) {
341353
)} has added the class-properties plugin to its build, but ember-cli-babel provides these by default now! You can remove the transforms, or the addon that provided them, such as @ember-decorators/babel-transforms.`
342354
);
343355
}
344-
} else {
356+
} else if (isClassPropertiesRequired) {
345357
addPlugin(
346358
plugins,
347359
[

lib/get-babel-options.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ const {
1414
_getPresetEnv,
1515
} = require("./babel-options-util");
1616

17-
module.exports = function getBabelOptions(config, appInstance) {
18-
let { parent, project } = appInstance;
17+
module.exports = function getBabelOptions(config, cliBabelInstance) {
18+
let { parent, project } = cliBabelInstance;
1919
let addonProvidedConfig = _getAddonProvidedConfig(config);
20-
let shouldIncludeHelpers = _shouldIncludeHelpers(config, appInstance);
20+
let shouldIncludeHelpers = _shouldIncludeHelpers(config, cliBabelInstance);
2121
let shouldHandleTypeScript = _shouldHandleTypeScript(config, parent, project);
2222
let shouldIncludeDecoratorPlugins = _shouldIncludeDecoratorPlugins(config);
2323

24-
let emberCLIBabelConfig = config["ember-cli-babel"];
24+
let emberCLIBabelConfig = config["ember-cli-babel"];
2525
let shouldRunPresetEnv = true;
2626

2727
if (emberCLIBabelConfig) {
@@ -38,13 +38,16 @@ module.exports = function getBabelOptions(config, appInstance) {
3838
}
3939

4040
if (shouldIncludeDecoratorPlugins) {
41-
userPlugins = _addDecoratorPlugins(
42-
userPlugins.slice(),
43-
addonProvidedConfig.options,
41+
userPlugins = _addDecoratorPlugins({
42+
plugins: userPlugins.slice(),
43+
options: addonProvidedConfig.options,
4444
config,
4545
parent,
46-
project
47-
);
46+
project,
47+
isClassPropertiesRequired: cliBabelInstance.isPluginRequired(
48+
"proposal-class-properties"
49+
),
50+
});
4851
}
4952

5053
options.plugins = []
@@ -56,7 +59,8 @@ module.exports = function getBabelOptions(config, appInstance) {
5659
_getEmberDataPackagesPolyfill(config, parent),
5760
_shouldCompileModules(config, project) && _getModulesPlugin(),
5861
userPostTransformPlugins
59-
).filter(Boolean);
62+
)
63+
.filter(Boolean);
6064

6165
options.presets = [
6266
shouldRunPresetEnv && _getPresetEnv(addonProvidedConfig, project),

0 commit comments

Comments
 (0)