Skip to content

Commit

Permalink
Merge pull request #105 from Turbo87/custom-options
Browse files Browse the repository at this point in the history
Move custom options to "ember-cli-babel" options hash
  • Loading branch information
Turbo87 committed Dec 7, 2016
2 parents b9a0dc2 + b231816 commit ae220d2
Show file tree
Hide file tree
Showing 4 changed files with 393 additions and 65 deletions.
2 changes: 1 addition & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
} 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) || {};
},

_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;
} 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 || {});

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;
},
};

0 comments on commit ae220d2

Please sign in to comment.