-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.js
187 lines (168 loc) · 6.55 KB
/
addon.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const TemplateImportProcessor = require('./lib/TemplateImportProcessor').default;
const StylesRewriter = require('./lib/StylesRewriter');
const VerifyImports = require('./lib/VerifyImports');
const path = require('path');
const Funnel = require('broccoli-funnel');
const Merge = require('broccoli-merge-trees');
const Concat = require('broccoli-concat');
const stew = require('broccoli-stew');
function isApp(appOrAddon) {
return !isAddon(appOrAddon);
}
function isAddon(appOrAddon) {
return !!appOrAddon.pkg.keywords && appOrAddon.pkg.keywords.indexOf('ember-addon') > -1;
}
function isDummyAppBuild(self) {
return isAddon(self.parent) && self.parent.name === self.project.name && self.parent.pkg === self.project.pkg;
}
const imports = {
from: {},
components: new Set(),
others: new Set()
};
const Packager = require('ember-cli/lib/broccoli/default-packager');
const processJavascript = Packager.prototype.processJavascript;
Packager.prototype.processJavascript = function(tree) {
const name = this.name === 'dummy' ? 'dummy' : this.project.name();
const funnel = new Funnel(tree, {
include: [/^addon-tree-output/, new RegExp('^' + name)]
});
return new Merge([new VerifyImports(funnel, {
imports: imports
}), processJavascript.call(this, tree)], {
overwrite: true
});
}
module.exports = {
name: require('./package').name,
imports: imports,
_findApp() {
if (this.app) {
return this.app;
}
let parent = this.parent;
while (!parent.app) {
parent = parent.parent;
}
return parent.app;
},
_getAddonOptions() {
const parentOptions = this.parent && this.parent.options;
const appOptions = this._findApp().options;
const addonOptions = Object.assign({}, parentOptions, appOptions);
return Object.assign({
style: {
extension: 'scss',
plugins: {
before: [],
after: []
}
}
}, addonOptions['ember-hbs-imports']);
},
_getBabelOptions() {
const parentOptions = this.parent && this.parent.options;
const appOptions = this.app && this.app.options;
const addonOptions = parentOptions || appOptions || {};
addonOptions.babel = addonOptions.babel || {};
addonOptions.babel.plugins = addonOptions.babel.plugins || [];
return addonOptions.babel;
},
included(includer) {
this.includer = includer;
// If we are being used inside an addon, then we want the addon's scoped styles
// to be processed, but not the consuming app's. So we wrap the parent addon's
// treeForAddonStyles to process them for scoping.
if (isAddon(this.parent)) {
const original = this.parent.treeForStyles || ((t) => t);
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
this.parent.treeForStyles = function(stylesInput = path.join(this.root, this.treePaths['addon-styles'])) {
let originalOutput = original.call(this, stylesInput);
const scopedOutput = self._scopedStyles(path.join(this.root, this.treePaths.addon), this.name);
originalOutput = new Funnel(originalOutput, { srcDir: this.treePaths.styles });
return stew.mv(new Merge([ originalOutput, scopedOutput ]), this.treePaths.styles + '/' + this.name);
}
}
const name = typeof this.parent.name === 'function' ? this.parent.name() : this.parent.name;
const isDummy = isDummyAppBuild(this);
const VersionChecker = require('ember-cli-version-checker');
const checker = new VersionChecker(this);
const ember = checker.for('ember-source');
const addonOptions = this._getAddonOptions();
const options = {
styleExtension: addonOptions.style.extension,
root: path.join(this.project.root, ...(isDummy ? ['tests','dummy'] : [])),
failOnMissingImport: false,
failOnBadImport: false,
namespace: isDummy ? 'dummy' : name,
imports: {},
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2'),
useHelperWrapper: !ember.isAbove('v3.27.0-beta.2'),
embroiderStatic: addonOptions.embroiderStatic,
}
this._getBabelOptions().plugins.splice(0, 0, [require.resolve('./lib/hbs-imports-babel-plugin'), options]);
this._super.included.call(this, arguments);
},
treeForStyles(tree) {
const trees = [];
if (tree) {
trees.push(tree);
}
if (isApp(this.parent)) {
trees.push(this._scopedStyles(path.join(this.parent.root, 'app'), this.parent.name()));
}
if (isDummyAppBuild(this)) {
const config = this._getAddonOptions().style;
trees.push(this._scopedStyles(path.join(this.project.root, 'app'), this.parent.name(), `${this.parent.name()}-pod-styles.${config.extension}`));
trees.push(this._scopedStyles(path.join(this.project.root, 'tests', 'dummy', 'app'), 'dummy'));
}
return new Merge(trees);
},
_scopedStyles(tree, namespace, outputFile) {
const config = this._getAddonOptions().style;
outputFile = outputFile || 'pod-styles.' + config.extension
tree = new Funnel(tree, { include: [ '**/*.module.' + config.extension ] });
tree = new StylesRewriter(tree, {
namespace,
extension: config.extension,
before: config.plugins.before,
after: config.plugins.after,
});
tree = new Concat(tree, { allowNone: true, outputFile });
return tree;
},
setupPreprocessorRegistry(type, registry) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const self = this;
registry.add('template', {
name: 'ember-hbs-imports',
ext: 'hbs',
before: ['ember-cli-htmlbars'],
toTree: (tree) => {
const name = typeof self.parent.name === 'function' ? self.parent.name() : self.parent.name;
const isDummy = isDummyAppBuild(self);
const VersionChecker = require('ember-cli-version-checker');
const checker = new VersionChecker(this);
const ember = checker.for('ember-source');
const addonOptions = this._getAddonOptions();
const options = {
styleExtension: addonOptions.style.extension,
root: path.join(this.project.root, ...(isDummy ? ['tests','dummy'] : [])),
failOnMissingImport: false,
failOnBadImport: false,
namespace: isDummy ? 'dummy' : name,
imports: this.imports,
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2'),
useHelperWrapper: !ember.isAbove('v3.27.0-beta.2'),
embroiderStatic: addonOptions.embroiderStatic,
}
tree = new TemplateImportProcessor(tree, options);
return tree;
}
});
if (type === 'parent') {
this.parentRegistry = registry;
}
}
};