-
Notifications
You must be signed in to change notification settings - Fork 137
/
ember-addon-main.ts
99 lines (88 loc) · 4.14 KB
/
ember-addon-main.ts
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
import { join } from 'path';
import { MacrosConfig, isEmbroiderMacrosPlugin } from './node';
export = {
name: '@embroider/macros',
included(this: any, parent: any) {
this._super.included.apply(this, arguments);
this.options.babel = { plugins: [] };
let parentOptions = (parent.options = parent.options || {});
let ownOptions = (parentOptions['@embroider/macros'] = parentOptions['@embroider/macros'] || {});
const appInstance = this._findHost();
this.setMacrosConfig(MacrosConfig.for(appInstance));
// if parent is an addon it has root. If it's an app it has project.root.
let source = parent.root || parent.project.root;
if (ownOptions.setOwnConfig) {
MacrosConfig.for(appInstance).setOwnConfig(source, ownOptions.setOwnConfig);
}
if (ownOptions.setConfig) {
for (let [packageName, config] of Object.entries(ownOptions.setConfig)) {
MacrosConfig.for(appInstance).setConfig(source, packageName, config);
}
}
if (appInstance.env !== 'production') {
let macros = MacrosConfig.for(appInstance);
// tell the macros where our app is
macros.enableAppDevelopment(join(appInstance.project.configPath(), '..', '..'));
// also tell them our root project is under development. This can be
// different, in the case where this is an addon and the app is the dummy
// app.
macros.enablePackageDevelopment(appInstance.project.root);
// keep the macros in runtime mode for development & testing
macros.enableRuntimeMode();
}
// add our babel plugin to our parent's babel
this.installBabelPlugin(parent);
// and to our own babel, because we may need to inline runtime config into
// our source code
this.installBabelPlugin(this);
appInstance.import('vendor/embroider-macros-test-support.js', { type: 'test' });
// When we're used inside the traditional ember-cli build pipeline without
// Embroider, we unfortunately need to hook into here uncleanly because we
// need to delineate the point in time after which writing macro config is
// forbidden and consuming it becomes allowed. There's no existing hook with
// that timing.
const originalToTree = appInstance.toTree;
appInstance.toTree = function () {
MacrosConfig.for(appInstance).finalize();
return originalToTree.apply(appInstance, arguments);
};
},
// Other addons are allowed to call this. It's needed if an addon needs to
// emit code containing macros into that addon's parent (via a babel plugin,
// for exmple). This is only an issue in classic builds, under embroider all
// babel plugins should be thought of as *language extensions* that are
// available everywhere, we don't scope them so narrowly so this probably
// doesn't come up.
installBabelPlugin(this: any, appOrAddonInstance: any) {
let babelOptions = (appOrAddonInstance.options.babel = appOrAddonInstance.options.babel || {});
let babelPlugins = (babelOptions.plugins = babelOptions.plugins || []);
if (!babelPlugins.some(isEmbroiderMacrosPlugin)) {
let appInstance = this._findHost();
let source = appOrAddonInstance.root || appOrAddonInstance.project.root;
babelPlugins.unshift(MacrosConfig.for(appInstance).babelPluginConfig(source));
}
},
setupPreprocessorRegistry(this: any, type: 'parent' | 'self', registry: any) {
if (type === 'parent') {
// the htmlbars-ast-plugins are split into two parts because order is
// important. Weirdly, they appear to run in the reverse order that you
// register them here.
//
// MacrosConfig.astPlugins is static because in classic ember-cli, at this
// point there's not yet an appInstance, so we defer getting it and
// calling setConfig until our included hook.
let { plugins, setConfig } = MacrosConfig.astPlugins((this as any).parent.root);
this.setMacrosConfig = setConfig;
plugins.forEach((plugin, index) => {
registry.add('htmlbars-ast-plugin', {
name: `@embroider/macros/${index}`,
plugin,
baseDir() {
return join(__dirname, '..');
},
});
});
}
},
options: {},
};