Skip to content
This repository was archived by the owner on Feb 6, 2021. It is now read-only.

Commit 1b9bc2f

Browse files
authored
Merge pull request #148 from arthirm/multi-ember-app-bug-fix
Fix issue with '_registeredWithBabel' check
2 parents 8d977d8 + 89c4214 commit 1b9bc2f

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656

5757
// add the HTMLBarsInlinePrecompilePlugin to the list of plugins used by
5858
// the `ember-cli-babel` addon
59-
if (!this._registeredWithBabel) {
59+
if (!this._isBabelPluginRegistered(babelPlugins)) {
6060
let templateCompilerPath = this.templateCompilerPath();
6161
let parallelConfig = this.getParallelConfig(pluginWrappers);
6262
let pluginInfo = AstPlugins.setupDependentPlugins(pluginWrappers);
@@ -93,10 +93,28 @@ module.exports = {
9393
});
9494
babelPlugins.push(htmlBarsPlugin);
9595
}
96-
this._registeredWithBabel = true;
9796
}
9897
},
9998

99+
/**
100+
* This function checks if 'ember-cli-htmlbars-inline-precompile' is already present in babelPlugins.
101+
* The plugin object will be different for non parallel API and parallel API.
102+
* For parallel api, check the `baseDir` of a plugin to see if it has current dirname
103+
* For non parallel api, check the 'modulePaths' to see if it contains 'ember-cli-htmlbars-inline-precompile'
104+
* @param {*} plugins
105+
*/
106+
_isBabelPluginRegistered(plugins) {
107+
return plugins.some(plugin => {
108+
if (Array.isArray(plugin)) {
109+
return plugin[0] === require.resolve('babel-plugin-htmlbars-inline-precompile');
110+
} else if (plugin !== null && typeof plugin === 'object' && plugin._parallelBabel !== undefined) {
111+
return plugin._parallelBabel.requireFile === path.resolve(__dirname, 'lib/require-from-worker');
112+
} else {
113+
return false;
114+
}
115+
});
116+
},
117+
100118
_getAddonOptions() {
101119
return (this.parent && this.parent.options) || (this.app && this.app.options) || {};
102120
},

lib/ast-plugins.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5-
const HTMLBarsInlinePrecompilePlugin = require('babel-plugin-htmlbars-inline-precompile');
5+
const HTMLBarsInlinePrecompilePlugin = require.resolve('babel-plugin-htmlbars-inline-precompile');
66
const hashForDep = require('hash-for-dep');
77
const debugGenerator = require('heimdalljs-logger');
88
const _logger = debugGenerator('ember-cli-htmlbars-inline-precompile');

node-tests/test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ const expect = require('chai').expect;
55
const fs = require('fs');
66
const path = require('path');
77
const Registry = require('ember-cli-preprocess-registry');
8-
const HTMLBarsInlinePrecompilePlugin = require('babel-plugin-htmlbars-inline-precompile');
8+
const HTMLBarsInlinePrecompilePlugin = require.resolve('babel-plugin-htmlbars-inline-precompile');
99
const hashForDep = require('hash-for-dep');
1010
const pluginHashForDep = hashForDep(path.resolve(__dirname, './fixtures'));
1111
const InlinePrecompile = require('../');
12+
const defaultsDeep = require('ember-cli-lodash-subset').defaultsDeep;
1213

1314
describe('canParallelize()', function() {
1415
it('returns true for 0 plugins', function() {
@@ -257,4 +258,38 @@ describe('included()', function() {
257258
expect(cacheKey).to.equal(expectedCacheKey);
258259
});
259260
});
261+
262+
describe('different instances of inline precompile for the same parent', function() {
263+
it('should have only 1 inlinePrecompile registered for parallel babel', function() {
264+
let InlinePrecompile1 = defaultsDeep( {}, require('../'))
265+
let InlinePrecompile2 = defaultsDeep( {}, require('../'))
266+
parent = { options: { babel: { plugins: [] } } };
267+
InlinePrecompile1.parent = parent;
268+
InlinePrecompile2.parent = parent;
269+
270+
InlinePrecompile1.included();
271+
configuredPlugins = parent.options.babel.plugins;
272+
expect(configuredPlugins.length).to.eql(1);
273+
InlinePrecompile2.included();
274+
configuredPlugins = parent.options.babel.plugins;
275+
expect(configuredPlugins.length).to.eql(1);
276+
});
277+
278+
it('should have only 1 inlinePrecompile registered for non parallel babel', function() {
279+
registry.add('htmlbars-ast-plugin', nonParallelPlugin);
280+
let InlinePrecompile1 = defaultsDeep( {}, require('../'))
281+
let InlinePrecompile2 = defaultsDeep( {}, require('../'))
282+
parent = { options: { babel: { plugins: [] } } };
283+
InlinePrecompile1.parent = parent;
284+
InlinePrecompile2.parent = parent;
285+
286+
InlinePrecompile1.included();
287+
configuredPlugins = parent.options.babel.plugins;
288+
expect(configuredPlugins.length).to.eql(1);
289+
InlinePrecompile2.included();
290+
configuredPlugins = parent.options.babel.plugins;
291+
expect(configuredPlugins.length).to.eql(1);
292+
});
293+
294+
});
260295
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"ember-cli-dependency-checker": "^3.0.0",
2727
"ember-cli-htmlbars": "^3.0.0",
2828
"ember-cli-inject-live-reload": "^2.0.1",
29+
"ember-cli-lodash-subset": "^2.0.1",
2930
"ember-cli-preprocess-registry": "^3.1.1",
3031
"ember-cli-template-lint": "^1.0.0-beta.1",
3132
"ember-cli-test-loader": "^2.2.0",

0 commit comments

Comments
 (0)