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

Move custom options to "ember-cli-babel" options hash #105

Merged
merged 12 commits into from Dec 7, 2016
Merged
2 changes: 1 addition & 1 deletion ember-cli-build.js
Expand Up @@ -15,7 +15,7 @@ module.exports = function(defaults) {
/* end hack */

var app = new EmberApp(defaults, {
babel: {
'ember-cli-babel': {
includePolyfill: true
}
});
Expand Down
171 changes: 109 additions & 62 deletions index.js
Expand Up @@ -8,6 +8,7 @@ var resolve = require('resolve');

module.exports = {
name: 'ember-cli-babel',
configKey: 'ember-cli-babel',

init: function() {
this._super.init && this._super.init.apply(this, arguments);
Expand All @@ -16,6 +17,7 @@ module.exports = {
var dep = checker.for('ember-cli', 'npm');

this._shouldSetupRegistryInIncluded = !dep.satisfies('>=0.2.0');
this._shouldShowBabelDeprecations = !dep.lt('2.11.0-beta.2');
},

setupPreprocessorRegistry: function(type, registry) {
Expand All @@ -25,14 +27,33 @@ module.exports = {
name: 'ember-cli-babel',
ext: 'js',
toTree: function(tree) {
return require('broccoli-babel-transpiler')(tree, getBabelOptions(addon));
return require('broccoli-babel-transpiler')(tree, addon._getBabelOptions());
}
});
},

shouldIncludePolyfill: function() {
var options = getAddonOptions(this);
return options.includePolyfill === true;
var addonOptions = this._getAddonOptions();
var babelOptions = addonOptions.babel;
var customOptions = addonOptions['ember-cli-babel'];

if (this._shouldShowBabelDeprecations && !this._polyfillDeprecationPrinted &&
babelOptions && 'includePolyfill' in babelOptions) {

this._polyfillDeprecationPrinted = true;

// we can use writeDeprecateLine() here because the warning will only be shown on newer Ember CLIs
this.ui.writeDeprecateLine(
'Putting the "includePolyfill" option in "babel" is deprecated, please put it in "ember-cli-babel" instead.');
}

if (customOptions && 'includePolyfill' in customOptions) {
return customOptions.includePolyfill === true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check to see if it is in both places here as well and writeDeprecateLine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - we can't rely on writeDeprecateLine as far back as this lib supports

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue And I was proud of knowing this because of recent spelunking: #105 (comment)

It turns out that having an in-built ecosystem mental model is absurdly valuable...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue since the deprecation will only be triggered in recent CLIs we can just use writeDeprecateLine here

@nathanhammond I'll move the deprecation check in front of everything

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point RE, newer CLI's only doing the deprecation. Maybe you can leave an inline comment (so future selves don't see it's use here as an excuse to use it for other ember-CLI deprecations)? I don't feel strongly either way though...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

} else if (babelOptions && 'includePolyfill' in babelOptions) {
return babelOptions.includePolyfill === true;
} else {
return false;
}
},

importPolyfill: function(app) {
Expand Down Expand Up @@ -69,74 +90,100 @@ module.exports = {
if (this.shouldIncludePolyfill()) {
this.importPolyfill(app);
}
}
};
},

function getAddonOptions(addonContext) {
var baseOptions = (addonContext.parent && addonContext.parent.options) || (addonContext.app && addonContext.app.options);
return baseOptions && baseOptions.babel || {};
}

function getBabelOptions(addonContext) {
var options = clone(getAddonOptions(addonContext));
var ui = addonContext.ui;

// pass a console object that wraps the addon's `UI` object
options.console = {
log: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeInfoLine) {
ui.writeInfoLine(message);
} else {
ui.writeLine(message, 'INFO');
}
},

warn: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeWarnLine) {
ui.writeWarnLine(message);
} else {
ui.writeLine(message, 'WARN');
}
},

error: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeError) {
ui.writeError(message);
} else {
ui.writeLine(message, 'ERROR');
}
_getAddonOptions: function() {
return (this.parent && this.parent.options) || (this.app && this.app.options) || {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That second check shouldn't be required unless we're going way back in support. (Before 0.2.0.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},

_shouldCompileModules: function(addonOptions) {
var babelOptions = addonOptions.babel;
var customOptions = addonOptions['ember-cli-babel'];

if (this._shouldShowBabelDeprecations && !this._modulesDeprecationPrinted &&
babelOptions && 'compileModules' in babelOptions) {

this._modulesDeprecationPrinted = true;

// we can use writeDeprecateLine() here because the warning will only be shown on newer Ember CLIs
this.ui.writeDeprecateLine(
'Putting the "compileModules" option in "babel" is deprecated, please put it in "ember-cli-babel" instead.');
}
};

// Ensure modules aren't compiled unless explicitly set to compile
options.blacklist = options.blacklist || ['es6.modules'];
if (customOptions && 'compileModules' in customOptions) {
return customOptions.compileModules === true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same story: can we check to see if it is in both places here as well and writeDeprecateLine?

} else if (babelOptions && 'compileModules' in babelOptions) {
return babelOptions.compileModules === true;
} else {
return false;
}
},

// do not enable non-standard transforms
if (!('nonStandard' in options)) {
options.nonStandard = false;
}
_getBabelOptions: function() {
var addonOptions = this._getAddonOptions();
var options = clone(addonOptions.babel || {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we name options a bit more clearly? I had to look at this multiple times to figure out what it was supposed to be.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options was the original name. I kept it to try to keep the diff smaller.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


var compileModules = this._shouldCompileModules(addonOptions);

var ui = this.ui;

// pass a console object that wraps the addon's `UI` object
options.console = {
log: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeInfoLine) {
ui.writeInfoLine(message);
} else {
ui.writeLine(message, 'INFO');
}
},

warn: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeWarnLine) {
ui.writeWarnLine(message);
} else {
ui.writeLine(message, 'WARN');
}
},

error: function(message) {
// fallback needed for support of ember-cli < 2.2.0
if (ui.writeError) {
ui.writeError(message);
} else {
ui.writeLine(message, 'ERROR');
}
}
};

// Don't include the `includePolyfill` flag, since Babel doesn't care
delete options.includePolyfill;
// Ensure modules aren't compiled unless explicitly set to compile
options.blacklist = options.blacklist || ['es6.modules'];

if (options.compileModules === true) {
if (options.blacklist.indexOf('es6.modules') >= 0) {
options.blacklist.splice(options.blacklist.indexOf('es6.modules'), 1);
// do not enable non-standard transforms
if (!('nonStandard' in options)) {
options.nonStandard = false;
}

// Remove custom options from `options` hash that is passed to Babel
delete options.includePolyfill;
delete options.compileModules;
} else {
if (options.blacklist.indexOf('es6.modules') < 0) {
options.blacklist.push('es6.modules');

var blacklistModulesIndex = options.blacklist.indexOf('es6.modules');
if (compileModules) {
if (blacklistModulesIndex >= 0) {
options.blacklist.splice(blacklistModulesIndex, 1);
}
} else {
if (blacklistModulesIndex < 0) {
options.blacklist.push('es6.modules');
}
}
}

// Ember-CLI inserts its own 'use strict' directive
options.blacklist.push('useStrict');
options.highlightCode = false;
// Ember-CLI inserts its own 'use strict' directive
options.blacklist.push('useStrict');
options.highlightCode = false;

return options;
}
return options;
},
};