forked from minichate/ember-cli-conditional-compile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (112 loc) · 3.83 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
var Funnel = require('broccoli-funnel');
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var merge = require('lodash.merge');
var replace = require('broccoli-replace');
var chalk = require('chalk');
var VersionChecker = require('ember-cli-version-checker');
var TemplateCompiler = require('./lib/template-compiler');
var hash = require('object-hash');
module.exports = {
name: 'ember-cli-conditional-compile',
enableCompile: false,
init: function() {
this._super.init && this._super.init.apply(this, arguments);
var checker = new VersionChecker(this);
checker.forEmber().assertAbove('2.9.0');
this.htmlbarsVersion = checker.for('ember-cli-htmlbars', 'npm');
this.uglifyVersion = checker.for('ember-cli-uglify', 'npm');
},
included: function(app, parentAddon) {
var target = (parentAddon || app);
var config = this.project.config(target.env);
var options = {
options: {
compress: {
global_defs: config.featureFlags
}
}
};
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;
}
var templateCompilerInstance = {
name: 'conditional-compile-template',
plugin: TemplateCompiler(config.featureFlags)
}
if (this.htmlbarsVersion.satisfies('>= 1.3.0')) {
templateCompilerInstance['baseDir'] = function() {
return __dirname;
};
templateCompilerInstance['cacheKey'] = function() {
return hash(config.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);
},
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, config) {
var esTranspiler = require('broccoli-babel-transpiler');
var inlineFeatureFlags = require('babel-plugin-inline-replace-variables');
var config = this.project.config(EmberApp.env());
if (!this.enableCompile) {
return tree;
}
return esTranspiler(tree, {
plugins: [
[ inlineFeatureFlags, config.featureFlags ]
]
});
},
postprocessTree: function(type, tree) {
if (type !== 'js') return tree;
var config = this.project.config(EmberApp.env());
if (!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;
}
var excludes = [];
Object.keys(config.featureFlags).map(function(flag) {
if (config.includeDirByFlag && !config.featureFlags[flag] && config.includeDirByFlag[flag]) {
excludes = excludes.concat(config.includeDirByFlag[flag]);
}
});
if (this.enableCompile) {
excludes = excludes.concat(
/ember-cli-conditional-compile-features.js/
);
} 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(config.featureFlags || {})
}]
});
}
return new Funnel(tree, {
exclude: excludes,
description: 'Funnel: Conditionally Filtered App'
});
}
};