This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
/
index.js
165 lines (139 loc) · 6.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const path = require('path');
const AstPlugins = require('./lib/ast-plugins');
const VersionChecker = require('ember-cli-version-checker');
const SilentError = require('silent-error');
const debugGenerator = require('heimdalljs-logger');
const semver = require('semver');
const _logger = debugGenerator('ember-cli-htmlbars-inline-precompile');
module.exports = {
name: 'ember-cli-htmlbars-inline-precompile',
init() {
this._super.init && this._super.init.apply(this, arguments);
let checker = new VersionChecker(this);
let hasIncorrectBabelVersion = checker.for('ember-cli-babel', 'npm').lt('6.7.1');
if (hasIncorrectBabelVersion) {
throw new SilentError(`ember-cli-htmlbars-inline-precompile v1.0.0 and above require the ember-cli-babel v6.7.1 or above. To use ember-cli-babel v5.x please downgrade ember-cli-htmlbars-inline-precompile to v0.3.`);
}
},
setupPreprocessorRegistry(type, registry) {
if (type === 'parent') {
this.parentRegistry = registry;
}
},
included() {
this._super.included.apply(this, arguments);
let projectEmberCliHtmlbars = this.project.findAddonByName('ember-cli-htmlbars');
if(projectEmberCliHtmlbars && projectEmberCliHtmlbars.inlinePrecompilerRegistered) {
return;
}
let parentEmberCliHtmlbars = this.parent.addons.find(a => a.name === 'ember-cli-htmlbars');
if (parentEmberCliHtmlbars && semver.gt(parentEmberCliHtmlbars.pkg.version, '4.0.2')) {
// ember-cli-htmlbars will issue a deprecation message, but we need to
// ensure that we don't attempt to add the babel plugin
return;
}
let checker = new VersionChecker(this);
let emberCLIUsesSharedBabelPlugins = checker.for('ember-cli', 'npm').lt('2.13.0-alpha.1');
let addonOptions = this._getAddonOptions();
let isProjectDependency = this.parent === this.project;
let babelPlugins;
if (emberCLIUsesSharedBabelPlugins && isProjectDependency) {
addonOptions.babel6 = addonOptions.babel6 || {};
babelPlugins = addonOptions.babel6.plugins = addonOptions.babel6.plugins || [];
} else {
addonOptions.babel = addonOptions.babel || {};
babelPlugins = addonOptions.babel.plugins = addonOptions.babel.plugins || [];
}
let pluginWrappers = this.parentRegistry.load('htmlbars-ast-plugin');
// add the HTMLBarsInlinePrecompilePlugin to the list of plugins used by
// the `ember-cli-babel` addon
if (!this._isBabelPluginRegistered(babelPlugins)) {
let templateCompilerPath = this.templateCompilerPath();
let parallelConfig = this.getParallelConfig(pluginWrappers);
let pluginInfo = AstPlugins.setupDependentPlugins(pluginWrappers);
if (this.canParallelize(pluginWrappers)) {
_logger.debug('using parallel API with broccoli-babel-transpiler');
let parallelBabelInfo = {
requireFile: path.resolve(__dirname, 'lib/require-from-worker'),
buildUsing: 'build',
params: {
templateCompilerPath,
parallelConfig
}
};
let cacheKey;
babelPlugins.push({
_parallelBabel: parallelBabelInfo,
baseDir: () => __dirname,
cacheKey: () => {
if (cacheKey === undefined) {
// parallelBabelInfo will not be used in the cache unless it is explicitly included
cacheKey = AstPlugins.makeCacheKey(templateCompilerPath, pluginInfo, JSON.stringify(parallelBabelInfo));
}
return cacheKey;
},
});
}
else {
_logger.debug('NOT using parallel API with broccoli-babel-transpiler');
let blockingPlugins = pluginWrappers.map((wrapper) => {
if (wrapper.parallelBabel === undefined) {
return wrapper.name;
}
}).filter(Boolean);
_logger.debug('Prevented by these plugins: ' + blockingPlugins);
let htmlBarsPlugin = AstPlugins.setup(pluginInfo, {
projectConfig: this.projectConfig(),
templateCompilerPath: this.templateCompilerPath(),
});
babelPlugins.push(htmlBarsPlugin);
}
}
},
/**
* This function checks if 'ember-cli-htmlbars-inline-precompile' is already present in babelPlugins.
* The plugin object will be different for non parallel API and parallel API.
* For parallel api, check the `baseDir` of a plugin to see if it has current dirname
* For non parallel api, check the 'modulePaths' to see if it contains 'ember-cli-htmlbars-inline-precompile'
* @param {*} plugins
*/
_isBabelPluginRegistered(plugins) {
return plugins.some(plugin => {
if (Array.isArray(plugin)) {
return plugin[0] === require.resolve('babel-plugin-htmlbars-inline-precompile');
} else if (plugin !== null && typeof plugin === 'object' && plugin._parallelBabel !== undefined) {
return plugin._parallelBabel.requireFile === path.resolve(__dirname, 'lib/require-from-worker');
} else {
return false;
}
});
},
_getAddonOptions() {
return (this.parent && this.parent.options) || (this.app && this.app.options) || {};
},
// verify that each registered ast plugin can be parallelized
canParallelize(pluginWrappers) {
return pluginWrappers.every((wrapper) => wrapper.parallelBabel !== undefined);
},
// return an array of the 'parallelBabel' object for each registered htmlbars-ast-plugin
getParallelConfig(pluginWrappers) {
return pluginWrappers.map((wrapper) => wrapper.parallelBabel);
},
// borrowed from ember-cli-htmlbars http://git.io/vJDrW
projectConfig() {
return this.project.config(process.env.EMBER_ENV);
},
// borrowed from ember-cli-htmlbars http://git.io/vJDrw
templateCompilerPath() {
let config = this.projectConfig();
let templateCompilerPath = config['ember-cli-htmlbars'] && config['ember-cli-htmlbars'].templateCompilerPath;
let ember = this.project.findAddonByName('ember-source');
if (ember) {
return ember.absolutePaths.templateCompiler;
} else if (!templateCompilerPath) {
templateCompilerPath = this.project.bowerDirectory + '/ember/ember-template-compiler';
}
return path.resolve(this.project.root, templateCompilerPath);
}
};