forked from ember-cli/ember-cli-htmlbars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathember-addon-main.js
More file actions
125 lines (105 loc) · 3.76 KB
/
ember-addon-main.js
File metadata and controls
125 lines (105 loc) · 3.76 KB
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
'use strict';
const path = require('path');
const SilentError = require('silent-error');
const utils = require('./utils');
let registryInvocationCounter = 0;
module.exports = {
name: require('../package').name,
parentRegistry: null,
setupPreprocessorRegistry(type, registry) {
// when this.parent === this.project, `this.parent.name` is a function 😭
let parentName =
typeof this.parent.name === 'function'
? this.parent.name()
: this.parent.name;
registry.add('template', {
name: 'ember-cli-htmlbars',
ext: 'hbs',
toTree: (tree) => {
const ColocatedTemplateProcessor = require('./colocated-broccoli-plugin');
const TemplateCompiler = require('./template-compiler-plugin');
let debugTree = require('broccoli-debug').buildDebugCallback(
`ember-cli-htmlbars:${parentName}:tree-${registryInvocationCounter++}`,
);
let inputTree = debugTree(tree, '01-input');
let colocatedTree = debugTree(
new ColocatedTemplateProcessor(inputTree),
'02-colocated-output',
);
let output = debugTree(
new TemplateCompiler(colocatedTree),
'03-output',
);
return output;
},
});
if (type === 'parent') {
this.parentRegistry = registry;
}
},
included() {
this._super.included.apply(this, arguments);
let addonOptions = this._getAddonOptions();
addonOptions.babel = addonOptions.babel || {};
addonOptions.babel.plugins = addonOptions.babel.plugins || [];
let babelPlugins = addonOptions.babel.plugins;
if (!utils.isTemplateCompilationPluginRegistered(babelPlugins)) {
babelPlugins.push([
require.resolve('babel-plugin-ember-template-compilation'),
{
// For historic reasons, our plugins are stored in reverse order, whereas
// babel-plugin-ember-template-compilation uses the sensible order.
transforms: this.astPlugins(),
compilerPath: require.resolve(this.templateCompilerPath()),
enableLegacyModules: [
'ember-cli-htmlbars',
'ember-cli-htmlbars-inline-precompile',
'htmlbars-inline-precompile',
],
},
'ember-cli-htmlbars:inline-precompile',
]);
}
if (!utils.isColocatedBabelPluginRegistered(babelPlugins)) {
babelPlugins.push(require.resolve('./colocated-babel-plugin'));
}
},
_getAddonOptions() {
return (
(this.parent && this.parent.options) ||
(this.app && this.app.options) ||
{}
);
},
templateCompilerPath() {
let app = this._findHost();
let templateCompilerPath =
app &&
app.options &&
app.options['ember-cli-htmlbars'] &&
app.options['ember-cli-htmlbars'].templateCompilerPath;
if (templateCompilerPath) {
return path.resolve(this.project.root, templateCompilerPath);
}
let ember = this.project.findAddonByName('ember-source');
if (!ember) {
throw new SilentError(
`ember-cli-htmlbars: Cannot find the ember-source addon as part of the project, please ensure that 'ember-source' is in your projects dependencies or devDependencies`,
);
}
if (ember.absolutePaths) {
return ember.absolutePaths.templateCompiler;
}
// v7+ ember-source no longer provides absolutePaths; resolve from
// the host app's directory through the package exports map
let { createRequire } = require('module');
let appRequire = createRequire(
path.join(this.project.root, 'package.json'),
);
return appRequire.resolve('ember-source/ember-template-compiler/index.js');
},
astPlugins() {
let pluginWrappers = this.parentRegistry.load('htmlbars-ast-plugin');
return utils.convertPlugins(pluginWrappers);
},
};