-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
195 lines (170 loc) · 5.49 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const EmberApp = require("ember-cli/lib/broccoli/ember-app");
const merge = require("lodash.merge");
const replace = require("broccoli-replace");
const chalk = require("chalk");
const VersionChecker = require("ember-cli-version-checker");
const TemplateCompiler = require("./lib/template-compiler");
const hash = require("object-hash");
const fs = require("fs");
const path = require("path");
module.exports = {
name: "ember-cli-conditional-compile",
enableCompile: false,
init: function () {
this._super.init && this._super.init.apply(this, arguments);
const checker = new VersionChecker(this);
checker.for("ember-source").assertAbove("2.9.0");
this.htmlbarsVersion = checker.for("ember-cli-htmlbars", "npm");
this.uglifyVersion = checker.for("ember-cli-uglify", "npm");
this.terserVersion = checker.for("ember-cli-terser", "npm");
},
included: function (app, parentAddon) {
this.readConfig();
const target = parentAddon || app;
let options = {
options: {
compress: {
global_defs: this._config.featureFlags,
},
},
};
if (this.terserVersion.exists()) {
target.options = merge(target.options, {
"ember-cli-terser": { terser: options.options },
});
this.enableCompile = target.options["ember-cli-terser"].enabled;
} else if (this.uglifyVersion.satisfies(">= 2.0.0")) {
target.options = merge(target.options, {
"ember-cli-uglify": { uglify: options.options },
});
this.enableCompile = target.options["ember-cli-uglify"].enabled;
} else {
target.options.minifyJS = merge(target.options.minifyJS, options);
this.enableCompile = target.options.minifyJS.enabled;
}
const templateCompilerInstance = {
name: "conditional-compile-template",
plugin: TemplateCompiler(this._config.featureFlags),
};
if (this.htmlbarsVersion.satisfies(">= 1.3.0")) {
templateCompilerInstance["baseDir"] = function () {
return __dirname;
};
const featureFlags = this._config.featureFlags;
templateCompilerInstance["cacheKey"] = function () {
return hash(featureFlags);
};
} else {
console.log(
chalk.yellow(
"Upgrade to ember-cli-htmlbars >= 1.3.0 to get build caching"
)
);
}
target.registry.add("htmlbars-ast-plugin", templateCompilerInstance);
},
readConfig() {
const root = this.project.root;
const config = this.project.config(EmberApp.env());
const flagsEnv = config.featureFlagsEnvironment || EmberApp.env();
let configFactory = path.join(root, "config", "feature-flags.js");
if (fs.existsSync(configFactory)) {
this._config = Object.assign({}, require(configFactory)(flagsEnv));
} else {
// try the app environment as a fallback
const envFeatureFlags = config["featureFlags"] || {};
const envIncludeDirByFlag = config["includeDirByFlag"] || {};
this._config = {
featureFlags: envFeatureFlags,
includeDirByFlag: envIncludeDirByFlag,
};
}
},
setupPreprocessorRegistry: function (type, registry) {
registry.add("js", {
name: "ember-cli-conditional-compile",
ext: "js",
toTree: (tree) => this.transpileTree(tree),
});
},
/**
* Inline feature flags value so that babili's dead code elimintation plugin
* removes the code non reachable.
*/
transpileTree(tree) {
const esTranspiler = require("broccoli-babel-transpiler");
const inlineFeatureFlags = require("babel-plugin-inline-replace-variables");
if (!this.enableCompile) {
return tree;
}
return esTranspiler(tree, {
plugins: [[inlineFeatureFlags, this._config.featureFlags]],
});
},
postprocessTree: function (type, tree) {
if (type !== "js") return tree;
let config = this.project.config(EmberApp.env());
if (!this._config.featureFlags) {
console.log(
chalk.red(
"Could not find any feature flags." +
"You may need to add them in your config/environment.js"
)
);
return tree;
}
let excludes = [];
if (this._config.featureFlags) {
Object.keys(this._config.featureFlags).map(function (flag) {
if (
this._config.includeDirByFlag &&
!this._config.featureFlags[flag] &&
this._config.includeDirByFlag[flag]
) {
const flaggedExcludes = this._config.includeDirByFlag[flag].map(
function (glob) {
return config.modulePrefix + "/" + glob;
}
);
excludes = excludes.concat(flaggedExcludes);
}
}, this);
}
if (this.enableCompile) {
tree = replace(tree, {
files: [
config.modulePrefix +
"/initializers/ember-cli-conditional-compile-features.js",
],
patterns: [
{
match: /EMBER_CLI_CONDITIONAL_COMPILE_INJECTIONS/g,
replacement: "{}",
},
],
});
} else {
tree = replace(tree, {
files: [
config.modulePrefix +
"/initializers/ember-cli-conditional-compile-features.js",
],
patterns: [
{
match: /EMBER_CLI_CONDITIONAL_COMPILE_INJECTIONS/g,
replacement: JSON.stringify(this._config.featureFlags || {}),
},
],
});
}
return replace(tree, {
files: excludes,
patterns: [
{
match: /.*/g,
replacement: "/**/",
},
],
});
},
};