Skip to content

Commit

Permalink
avoid eagerly requiring plugins and presets
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrostew committed May 23, 2018
1 parent ef09f8a commit a2c4784
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
41 changes: 10 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ const semver = require('semver');

let count = 0;

function addBaseDir(Plugin) {
let type = typeof Plugin;

if (type === 'function' && !Plugin.baseDir) {
Plugin.baseDir = () => __dirname;
} else if (type === 'object' && Plugin !== null && Plugin.default) {
addBaseDir(Plugin.default);
}
}

module.exports = {
name: 'ember-cli-babel',
configKey: 'ember-cli-babel',
Expand Down Expand Up @@ -225,10 +215,13 @@ module.exports = {
this._getDebugMacroPlugins(config),
this._getEmberModulesAPIPolyfill(config),
shouldCompileModules && this._getModulesPlugin(),
shouldRunPresetEnv && this._getPresetEnvPlugins(addonProvidedConfig),
userPostTransformPlugins
).filter(Boolean);

options.presets = [
shouldRunPresetEnv && this._getPresetEnvPlugins(addonProvidedConfig),
]

if (shouldCompileModules) {
options.moduleIds = true;
options.resolveModuleSource = require('amd-name-resolver').moduleResolve;
Expand All @@ -245,7 +238,6 @@ module.exports = {

if (addonOptions.disableDebugTooling) { return; }

const DebugMacros = require('babel-plugin-debug-macros').default;
const isProduction = process.env.EMBER_ENV === 'production';

let options = {
Expand All @@ -264,7 +256,7 @@ module.exports = {
}
};

return [[DebugMacros, options]];
return [[require.resolve('babel-plugin-debug-macros'), options]];
},

_getEmberModulesAPIPolyfill(config) {
Expand All @@ -273,10 +265,9 @@ module.exports = {
if (addonOptions.disableEmberModulesAPIPolyfill) { return; }

if (this._emberVersionRequiresModulesAPIPolyfill()) {
const ModulesAPIPolyfill = require('babel-plugin-ember-modules-api-polyfill');
const blacklist = this._getEmberModulesAPIBlacklist();

return [[ModulesAPIPolyfill, { blacklist }]];
return [[require.resolve('babel-plugin-ember-modules-api-polyfill'), { blacklist }]];
}
},

Expand All @@ -289,20 +280,12 @@ module.exports = {
targets
});

let presetEnvPlugins = this._presetEnv(null, presetOptions).plugins;

presetEnvPlugins.forEach(function(pluginArray) {
let Plugin = pluginArray[0];
addBaseDir(Plugin);
});

let presetEnvPlugins = this._presetEnv(presetOptions);
return presetEnvPlugins;
},

_presetEnv() {
const presetEnv = require('babel-preset-env').default;

return presetEnv.apply(null, arguments);
_presetEnv(presetOptions) {
return [require.resolve('babel-preset-env'), presetOptions];
},

_getTargets() {
Expand All @@ -317,12 +300,8 @@ module.exports = {
},

_getModulesPlugin() {
const ModulesTransform = require('babel-plugin-transform-es2015-modules-amd');

addBaseDir(ModulesTransform);

return [
[ModulesTransform, { noInterop: true }],
[require.resolve('babel-plugin-transform-es2015-modules-amd'), { noInterop: true }]
];
},

Expand Down
34 changes: 27 additions & 7 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ describe('ember-cli-babel', function() {
};

let invokingOptions;
this.addon._presetEnv = function(context, options) {
this.addon._presetEnv = function(options) {
invokingOptions = options;
return { plugins: [] };
return [];
};

this.addon.buildBabelOptions();
Expand All @@ -878,9 +878,9 @@ describe('ember-cli-babel', function() {
};

let invokingOptions;
this.addon._presetEnv = function(context, options) {
this.addon._presetEnv = function(options) {
invokingOptions = options;
return { plugins: [] };
return [];
};

this.addon.buildBabelOptions();
Expand All @@ -893,18 +893,38 @@ describe('ember-cli-babel', function() {
browsers: ['ie 9']
};

let plugins = this.addon.buildBabelOptions().plugins;
let invokingOptions;
let presetEnvOrig = this.addon._presetEnv;
this.addon._presetEnv = function(options) {
invokingOptions = options;
return presetEnvOrig(options);
};
this.addon.buildBabelOptions();
this.addon._presetEnv = presetEnvOrig;

const presetEnv = require('babel-preset-env').default;
let plugins = presetEnv(null, invokingOptions).plugins;
let found = includesPlugin(plugins, 'babel-plugin-transform-es2015-classes');

expect(found).to.be.true;
});

it('returns false when targets do not require plugin', function() {
it('does not include class transform when targets do not require plugin', function() {
this.addon.project.targets = {
browsers: ['last 2 chrome versions']
};

let plugins = this.addon.buildBabelOptions().plugins;
let invokingOptions;
let presetEnvOrig = this.addon._presetEnv;
this.addon._presetEnv = function(options) {
invokingOptions = options;
return presetEnvOrig(options);
};
this.addon.buildBabelOptions();
this.addon._presetEnv = presetEnvOrig;

const presetEnv = require('babel-preset-env').default;
let plugins = presetEnv(null, invokingOptions).plugins;
let found = includesPlugin(plugins, 'babel-plugin-transform-es2015-classes');

expect(found).to.be.false;
Expand Down

0 comments on commit a2c4784

Please sign in to comment.