forked from ember-fastboot/ember-cli-fastboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (122 loc) · 4.03 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
/* jshint node: true */
'use strict';
var path = require('path');
var EventEmitter = require('events').EventEmitter;
var mergeTrees = require('broccoli-merge-trees');
var VersionChecker = require('ember-cli-version-checker');
var patchEmberApp = require('./lib/ext/patch-ember-app');
var fastbootAppModule = require('./lib/utilities/fastboot-app-module');
var filterInitializers = require('fastboot-filter-initializers');
var FastBootBuild = require('./lib/broccoli/fastboot-build');
/*
* Main entrypoint for the Ember CLI addon.
*/
module.exports = {
name: 'ember-cli-fastboot',
init() {
this._super.init && this._super.init.apply(this, arguments);
this.emitter = new EventEmitter();
},
includedCommands: function() {
return {
'fastboot': require('./lib/commands/fastboot')(this),
/* fastboot:build is deprecated and will be removed in a future version */
'fastboot:build': require('./lib/commands/fastboot-build')
};
},
on: function() {
this.emitter.on.apply(this.emitter, arguments);
},
emit: function() {
this.emitter.emit.apply(this.emitter, arguments);
},
/**
* Called at the start of the build process to let the addon know it will be
* used. At this point, we can rely on the EMBER_CLI_FASTBOOT environment
* variable being set.
*
* Once we've determined which mode we're in (browser build or FastBoot build),
* we mixin additional Ember addon hooks appropriate to the current build target.
*/
included: function(app) {
patchEmberApp(app);
},
config: function() {
if (this.app && this.app.options.__is_building_fastboot__) {
return { APP: { autoboot: false } };
}
},
/**
* Inserts placeholders into index.html that are used by the FastBoot server
* to insert the rendered content into the right spot. Also injects a module
* for FastBoot application boot.
*/
contentFor: function(type, config, contents) {
if (type === 'body') {
return "<!-- EMBER_CLI_FASTBOOT_BODY -->";
}
if (type === 'head') {
return "<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->";
}
if (type === 'app-boot') {
return fastbootAppModule(config.modulePrefix);
}
if (type === 'config-module' && this.app.options.__is_building_fastboot__) {
var linesToRemove = contents.length;
while(linesToRemove) {
// Clear out the default config from ember-cli
contents.pop();
linesToRemove--;
}
return 'return FastBoot.config();';
}
},
treeForApp: function(defaultTree) {
var trees = [defaultTree];
if (this._getEmberVersion().lt('2.10.0-alpha.1')) {
trees.push(this.treeGenerator(path.resolve(this.root, 'app-lt-2-9')));
}
return mergeTrees(trees, { overwrite: true });
},
/**
* Filters out initializers and instance initializers that should only run in
* browser mode.
*/
preconcatTree: function(tree) {
return filterInitializers(tree, this.app.name);
},
/**
* After the entire Broccoli tree has been built for the `dist` directory,
* adds the `fastboot-config.json` file to the root.
*/
postprocessTree: function(type, tree) {
if (type === 'all') {
var fastbootTree = this.buildFastBootTree();
// Merge the package.json with the existing tree
return mergeTrees([tree, fastbootTree], {overwrite: true});
}
return tree;
},
buildFastBootTree: function() {
var fastbootBuild = new FastBootBuild({
ui: this.ui,
assetMapPath: this.assetMapPath,
project: this.project,
app: this.app,
parent: this.parent
});
return fastbootBuild.toTree();
},
postBuild: function() {
this.emit('postBuild');
},
_getEmberVersion: function() {
var VersionChecker = require('ember-cli-version-checker');
var checker = new VersionChecker(this);
var emberVersionChecker = checker.for('ember-source', 'npm');
if (emberVersionChecker.version) {
return emberVersionChecker;
}
return checker.for('ember', 'bower');
},
};