Skip to content

Commit

Permalink
Merge pull request #223 from mikrostew/bring-back-parallel
Browse files Browse the repository at this point in the history
Avoid eagerly requiring plugins and presets
  • Loading branch information
stefanpenner authored May 25, 2018
2 parents ef09f8a + e33466e commit 6ba2ed1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 42 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
language: node_js
node_js:
- "4"
- "6"
- "8"
- "10"
Expand Down
5 changes: 2 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ init:
environment:
MOCHA_REPORTER: "mocha-appveyor-reporter"
matrix:
- nodejs_version: "4"
- nodejs_version: "6"
- nodejs_version: "7"
# - nodejs_version: "8"
- nodejs_version: "8"
- nodejs_version: "10"

cache:
- '%APPDATA%\npm-cache'
Expand Down
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
40 changes: 33 additions & 7 deletions node-tests/addon-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const stripIndent = CommonTags.stripIndent;
const BroccoliTestHelper = require('broccoli-test-helper');
const createBuilder = BroccoliTestHelper.createBuilder;
const createTempDir = BroccoliTestHelper.createTempDir;
const terminateWorkerPool = require('./utils/terminate-workers');

let Addon = CoreObject.extend(AddonMixin);

describe('ember-cli-babel', function() {

const ORIGINAL_EMBER_ENV = process.env.EMBER_ENV;

beforeEach(function() {
Expand Down Expand Up @@ -50,6 +52,8 @@ describe('ember-cli-babel', function() {
afterEach(co.wrap(function* () {
yield input.dispose();
yield output.dispose();
// shut down workers after the tests are run so that mocha doesn't hang
yield terminateWorkerPool();
}));

it("should build", co.wrap(function* () {
Expand Down Expand Up @@ -817,6 +821,8 @@ describe('ember-cli-babel', function() {
});

describe('_getPresetEnvPlugins', function() {
this.timeout(5000);

function includesPlugin(haystack, needleName) {
let presetEnvBaseDir = path.dirname(require.resolve('babel-preset-env'));
let pluginPath = resolve.sync(needleName, { basedir: presetEnvBaseDir });
Expand Down Expand Up @@ -859,9 +865,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 +884,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 +899,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
19 changes: 19 additions & 0 deletions node-tests/utils/terminate-workers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

let ParallelApi = require('broccoli-babel-transpiler/lib/parallel-api');

module.exports = function terminateWorkerPool() {
// shut down any workerpool that is running at this point
let babelCoreVersion = ParallelApi.getBabelVersion();
let workerPoolId = 'v1/broccoli-babel-transpiler/workerpool/babel-core-' + babelCoreVersion;
let runningPool = process[workerPoolId];

if (runningPool) {
return runningPool.terminate()
.then(() => {
delete process[workerPoolId];
});
} else {
return Promise.resolve();
}
};

0 comments on commit 6ba2ed1

Please sign in to comment.