Skip to content

Commit a77e24e

Browse files
author
Robert Jackson
authored
Fix issue with deduplcation of babel plugin when parallelized (#322)
Fix issue with deduplcation of babel plugin when parallelized
2 parents ca0653f + eca93f2 commit a77e24e

3 files changed

Lines changed: 107 additions & 43 deletions

File tree

lib/ember-addon-main.js

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ dBabelVersion: ${hasValidBabelVersion};`
135135

136136
// add the babel-plugin-htmlbars-inline-precompile to the list of plugins
137137
// used by `ember-cli-babel` addon
138-
if (!this._isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
138+
if (!utils.isInlinePrecompileBabelPluginRegistered(babelPlugins)) {
139139
let pluginInfo = this.astPlugins();
140140
let templateCompilerPath = this.templateCompilerPath();
141141

@@ -184,51 +184,11 @@ dBabelVersion: ${hasValidBabelVersion};`
184184
}
185185
}
186186

187-
if (this._shouldColocateTemplates()) {
188-
const { hasPlugin, addPlugin } = require('ember-cli-babel-plugin-helpers');
189-
let colocatedPluginPath = require.resolve('./colocated-babel-plugin');
190-
191-
if (!hasPlugin(babelPlugins, colocatedPluginPath)) {
192-
addPlugin(babelPlugins, colocatedPluginPath);
193-
}
187+
if (this._shouldColocateTemplates() && !utils.isColocatedBabelPluginRegistered(babelPlugins)) {
188+
babelPlugins.push(require.resolve('./colocated-babel-plugin'));
194189
}
195190
},
196191

197-
/**
198-
* This function checks if 'babel-plugin-htmlbars-inline-precompile' is already present in babelPlugins.
199-
* The plugin object will be different for non parallel API and parallel API.
200-
* For parallel api, check the `baseDir` of a plugin to see if it has current dirname
201-
* For non parallel api, check the 'modules' to see if it contains the babel plugin
202-
* @param {*} plugins
203-
*/
204-
_isInlinePrecompileBabelPluginRegistered(plugins) {
205-
return plugins.some(plugin => {
206-
if (Array.isArray(plugin)) {
207-
let [pluginPathOrInstance, options] = plugin;
208-
209-
return (
210-
pluginPathOrInstance === require.resolve('babel-plugin-htmlbars-inline-precompile') &&
211-
typeof options.modules === 'object' &&
212-
options.modules['ember-cli-htmlbars'] === 'hbs'
213-
);
214-
} else if (
215-
plugin !== null &&
216-
typeof plugin === 'object' &&
217-
plugin._parallelBabel !== undefined
218-
) {
219-
return (
220-
plugin._parallelBabel.requireFile ===
221-
path.resolve(__dirname, 'lib/require-from-worker') &&
222-
typeof plugin._parallelBabel.params === 'object' &&
223-
typeof plugin._parallelBabel.params.modules === 'object' &&
224-
plugin._parallelBabel.params.modules['ember-cli-htmlbars'] === 'hbs'
225-
);
226-
} else {
227-
return false;
228-
}
229-
});
230-
},
231-
232192
projectConfig() {
233193
return this.project.config(process.env.EMBER_ENV);
234194
},

lib/utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@ const debugGenerator = require('heimdalljs-logger');
77
const logger = debugGenerator('ember-cli-htmlbars:utils');
88
const addDependencyTracker = require('./addDependencyTracker');
99

10+
function isInlinePrecompileBabelPluginRegistered(plugins) {
11+
return plugins.some(plugin => {
12+
if (Array.isArray(plugin)) {
13+
let [pluginPathOrInstance, options] = plugin;
14+
15+
return (
16+
pluginPathOrInstance === require.resolve('babel-plugin-htmlbars-inline-precompile') &&
17+
typeof options.modules === 'object' &&
18+
options.modules['ember-cli-htmlbars'] === 'hbs'
19+
);
20+
} else if (
21+
plugin !== null &&
22+
typeof plugin === 'object' &&
23+
plugin._parallelBabel !== undefined
24+
) {
25+
return (
26+
plugin._parallelBabel.requireFile === require.resolve('./require-from-worker') &&
27+
typeof plugin._parallelBabel.params === 'object' &&
28+
typeof plugin._parallelBabel.params.modules === 'object' &&
29+
plugin._parallelBabel.params.modules['ember-cli-htmlbars'] === 'hbs'
30+
);
31+
} else {
32+
return false;
33+
}
34+
});
35+
}
36+
37+
function isColocatedBabelPluginRegistered(plugins) {
38+
return plugins.some(
39+
plugin => typeof plugin === 'string' && plugin === require.resolve('./colocated-babel-plugin')
40+
);
41+
}
42+
1043
function buildOptions(projectConfig, templateCompilerPath, pluginInfo) {
1144
let EmberENV = projectConfig.EmberENV || {};
1245

@@ -221,4 +254,6 @@ module.exports = {
221254
setup,
222255
makeCacheKey,
223256
setupPlugins,
257+
isColocatedBabelPluginRegistered,
258+
isInlinePrecompileBabelPluginRegistered,
224259
};

node-tests/utils_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,73 @@ describe('utils', function() {
111111
});
112112
});
113113
});
114+
115+
describe('isInlinePrecompileBabelPluginRegistered', function() {
116+
let nonParallelizablePlugin, parallelizablePlugin;
117+
118+
beforeEach(function() {
119+
let modules = {
120+
'ember-cli-htmlbars': 'hbs',
121+
'ember-cli-htmlbars-inline-precompile': 'default',
122+
'htmlbars-inline-precompile': 'default',
123+
};
124+
125+
nonParallelizablePlugin = [
126+
require.resolve('babel-plugin-htmlbars-inline-precompile'),
127+
{ precompile: null, modules },
128+
'ember-cli-htmlbars:inline-precompile',
129+
];
130+
131+
parallelizablePlugin = {
132+
baseDir: () => __dirname,
133+
cacheKey: () => `${Date.now()}`,
134+
_parallelBabel: {
135+
requireFile: require.resolve('../lib/require-from-worker'),
136+
buildUsing: 'build',
137+
params: {
138+
templateCompilerPath: null,
139+
parallelConfigs: [],
140+
modules,
141+
},
142+
},
143+
};
144+
[
145+
require.resolve('babel-plugin-htmlbars-inline-precompile'),
146+
{ precompile: null, modules },
147+
'ember-cli-htmlbars:inline-precompile',
148+
];
149+
});
150+
151+
it('is false when no plugins exist', function() {
152+
let plugins = [];
153+
154+
assert.strictEqual(utils.isInlinePrecompileBabelPluginRegistered(plugins), false);
155+
});
156+
157+
it('detects when the non-parallelizable version of the plugin has been installed', function() {
158+
let plugins = [nonParallelizablePlugin];
159+
160+
assert.strictEqual(utils.isInlinePrecompileBabelPluginRegistered(plugins), true);
161+
});
162+
163+
it('detects when the parallelizable version of the plugin has been installed', function() {
164+
let plugins = [parallelizablePlugin];
165+
166+
assert.strictEqual(utils.isInlinePrecompileBabelPluginRegistered(plugins), true);
167+
});
168+
});
169+
170+
describe('isColocatedBabelPluginRegistered', function() {
171+
it('is false when no plugins exist', function() {
172+
let plugins = [];
173+
174+
assert.strictEqual(utils.isColocatedBabelPluginRegistered(plugins), false);
175+
});
176+
177+
it('detects when the plugin exists', function() {
178+
let plugins = [require.resolve('../lib/colocated-babel-plugin')];
179+
180+
assert.strictEqual(utils.isColocatedBabelPluginRegistered(plugins), true);
181+
});
182+
});
114183
});

0 commit comments

Comments
 (0)