Skip to content

Commit

Permalink
Properly handle class properties proposal
Browse files Browse the repository at this point in the history
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 emberjs#419
  • Loading branch information
chriskrycho authored and rwjblue committed Dec 8, 2021
1 parent 4c3b909 commit dddbada
Show file tree
Hide file tree
Showing 6 changed files with 2,566 additions and 1,334 deletions.
22 changes: 17 additions & 5 deletions lib/babel-options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function _getPresetEnv(config, project) {
}

function _getModulesPlugin() {
const resolvePath = require("./relative-module-paths")
.resolveRelativeModulePath;
const resolvePath =
require("./relative-module-paths").resolveRelativeModulePath;

return [
[require.resolve("babel-plugin-module-resolver"), { resolvePath }],
Expand Down Expand Up @@ -296,7 +296,12 @@ function _getHelperVersion(project) {
return APP_BABEL_RUNTIME_VERSION.get(project);
}

function _buildClassFeaturePluginConstraints(constraints, config, parent, project) {
function _buildClassFeaturePluginConstraints(
constraints,
config,
parent,
project
) {
// With versions of ember-cli-typescript < 4.0, class feature plugins like
// @babel/plugin-proposal-class-properties were run before the TS transform.
if (!_shouldHandleTypeScript(config, parent, project)) {
Expand All @@ -307,7 +312,14 @@ function _buildClassFeaturePluginConstraints(constraints, config, parent, projec
return constraints;
}

function _addDecoratorPlugins(plugins, options, config, parent, project) {
function _addDecoratorPlugins({
plugins,
options,
config,
parent,
project,
isClassPropertiesRequired,
}) {
const { hasPlugin, addPlugin } = require("ember-cli-babel-plugin-helpers");

if (hasPlugin(plugins, "@babel/plugin-proposal-decorators")) {
Expand Down Expand Up @@ -341,7 +353,7 @@ function _addDecoratorPlugins(plugins, options, config, parent, project) {
)} 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.`
);
}
} else {
} else if (isClassPropertiesRequired) {
addPlugin(
plugins,
[
Expand Down
24 changes: 14 additions & 10 deletions lib/get-babel-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const {
_getPresetEnv,
} = require("./babel-options-util");

module.exports = function getBabelOptions(config, appInstance) {
let { parent, project } = appInstance;
module.exports = function getBabelOptions(config, cliBabelInstance) {
let { parent, project } = cliBabelInstance;
let addonProvidedConfig = _getAddonProvidedConfig(config);
let shouldIncludeHelpers = _shouldIncludeHelpers(config, appInstance);
let shouldIncludeHelpers = _shouldIncludeHelpers(config, cliBabelInstance);
let shouldHandleTypeScript = _shouldHandleTypeScript(config, parent, project);
let shouldIncludeDecoratorPlugins = _shouldIncludeDecoratorPlugins(config);

let emberCLIBabelConfig = config["ember-cli-babel"];
let emberCLIBabelConfig = config["ember-cli-babel"];
let shouldRunPresetEnv = true;

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

if (shouldIncludeDecoratorPlugins) {
userPlugins = _addDecoratorPlugins(
userPlugins.slice(),
addonProvidedConfig.options,
userPlugins = _addDecoratorPlugins({
plugins: userPlugins.slice(),
options: addonProvidedConfig.options,
config,
parent,
project
);
project,
isClassPropertiesRequired: cliBabelInstance.isPluginRequired(
"proposal-class-properties"
),
});
}

options.plugins = []
Expand All @@ -56,7 +59,8 @@ module.exports = function getBabelOptions(config, appInstance) {
_getEmberDataPackagesPolyfill(config, parent),
_shouldCompileModules(config, project) && _getModulesPlugin(),
userPostTransformPlugins
).filter(Boolean);
)
.filter(Boolean);

options.presets = [
shouldRunPresetEnv && _getPresetEnv(addonProvidedConfig, project),
Expand Down
Loading

0 comments on commit dddbada

Please sign in to comment.