forked from mainmatter/mainmatter.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ember-cli-build.js
255 lines (222 loc) · 7.37 KB
/
ember-cli-build.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
'use strict';
const path = require('path');
const GlimmerApp = require('@glimmer/application-pipeline').GlimmerApp;
const BroccoliCleanCss = require('broccoli-clean-css');
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const Rollup = require('broccoli-rollup');
const typescript = require('broccoli-typescript-compiler').default;
const glob = require('glob');
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const collectPosts = require('./lib/generate-blog/lib/collect-posts');
const _ = require('lodash');
function findAllComponents() {
let routes = require('./config/routes-map')();
let routedComponents = Object.keys(routes).reduce(function(acc, key) {
let component = routes[key].component;
if (component) {
acc.push(component);
}
return acc;
}, []);
let recentContentComponents = _.chain(collectPosts(path.join(__dirname, '_posts')).posts)
.map('meta.topic')
.uniq()
.map(topic => `RecentPosts${_.capitalize(topic)}`)
.value();
let staticComponents = glob
.sync('*/', {
cwd: path.join(__dirname, 'src/ui/components'),
})
.map(component => component.replace(/\/$/, ''));
let allComponents = routedComponents.concat(staticComponents).concat(recentContentComponents);
return [...new Set(allComponents)];
}
class SimplabsApp extends GlimmerApp {
ssrTree() {
let tsTree = new Funnel('.', {
include: ['ssr/**/*', 'config/**/*'],
});
return typescript(tsTree, {
workingPath: this.project.root,
tsconfig: {
compilerOptions: {
target: 'es6',
moduleResolution: 'node',
},
},
throwOnError: process.env.NODE_ENV === 'production',
});
}
cssTree() {
let cssTree = super.cssTree(...arguments);
if (this.options.minifyCSS.enabled) {
cssTree = new BroccoliCleanCss(cssTree, { inline: ['all'] });
}
return cssTree;
}
package(jsTree) {
let mainSiteTree = this._dropBundle(jsTree, {
componentPrefix: 'PageBlog',
});
let { posts, authors } = collectPosts(path.join(__dirname, '_posts'));
let blogPostTrees = posts.map(post => {
let [blogPostTree] = this._splitBundle(jsTree, {
componentPrefix: post.componentName,
file: `blog/${post.queryPath}.js`,
moduleName: `__blog-${post.queryPath}__`,
});
return blogPostTree;
});
let blogAuthorTrees = authors.map(author => {
let [blogAuthorTree] = this._splitBundle(jsTree, {
componentPrefix: author.componentName,
file: `blog/author-${author.twitter}.js`,
moduleName: `__blog-author-${author.twitter}__`,
});
return blogAuthorTree;
});
let blogPages = _.chunk(posts, 10);
let blogPageTrees = blogPages.map((posts, i) => {
let page = i + 1;
let postPrefixes = posts.map(post => post.componentName).join('|');
let [blogPageTree] = this._splitBundle(jsTree, {
componentPrefix: `PageBlogPage${page}|${postPrefixes}`,
file: `blog/page/${page}.js`,
moduleName: `__blog-page-${page}__`,
});
return blogPageTree;
});
let [calendarTree, mainSiteNonCalendarTree] = this._splitBundle(mainSiteTree, {
componentPrefix: 'PageCalendar',
file: 'calendar.js',
moduleName: '__calendar__',
});
mainSiteTree = mainSiteNonCalendarTree;
let [legalTree, mainSiteNonLegalTree] = this._splitBundle(mainSiteTree, {
componentPrefix: 'PageLegal',
file: 'legal.js',
moduleName: '__legal__',
});
mainSiteTree = mainSiteNonLegalTree;
let [playbookTree, mainSiteNonPlaybookTree] = this._splitBundle(mainSiteTree, {
componentPrefix: 'PagePlaybook',
file: 'playbook.js',
moduleName: '__playbook__',
});
mainSiteTree = mainSiteNonPlaybookTree;
let [talksTree, mainSiteNonTalksTree] = this._splitBundle(mainSiteTree, {
componentPrefix: 'PageTalks',
file: 'talks.js',
moduleName: '__talks__',
});
mainSiteTree = mainSiteNonTalksTree;
let [recentContentTree, mainSiteNonRecentTree] = this._splitBundle(mainSiteTree, {
componentPrefix: 'Recent',
file: 'recent.js',
moduleName: '__recent__',
});
mainSiteTree = mainSiteNonRecentTree;
let appTree = super.package(mainSiteTree);
let mainTree = new MergeTrees([
appTree,
legalTree,
calendarTree,
playbookTree,
talksTree,
recentContentTree,
...blogPostTrees,
...blogAuthorTrees,
...blogPageTrees,
]);
if (process.env.PRERENDER) {
let ssrTree = this._packageSSR();
return new MergeTrees([mainTree, ssrTree]);
} else {
return mainTree;
}
}
_packageSSR() {
let jsTree = new Funnel(this.javascriptTree(), {
exclude: ['src/index.js'],
});
let ssrTree = this.ssrTree();
let appTree = new MergeTrees([jsTree, ssrTree], { overwrite: true });
return new Rollup(appTree, {
rollup: {
input: 'ssr/index.js',
output: {
file: 'ssr-app.js',
format: 'cjs',
},
onwarn(warning) {
if (warning.code === 'THIS_IS_UNDEFINED') {
return;
}
// eslint-disable-next-line no-console
console.log('Rollup warning: ', warning.message);
},
plugins: [resolve({ jsnext: true, module: true, main: true }), commonjs()],
},
});
}
_splitBundle(appTree, bundle) {
let mainBundleTree = this._dropBundle(appTree, bundle);
let bundleTree = new Funnel(appTree, {
exclude: [`src/ui/components/!(${bundle.componentPrefix})*`],
});
let bundleJsTree = new Funnel(bundleTree, {
include: ['**/*.js'],
});
let bundleModuleMap = this.buildResolutionMap(bundleJsTree);
bundleTree = new MergeTrees([bundleTree, bundleModuleMap], { overwrite: true });
bundleTree = this._packageSplitBundle(bundleTree, bundle);
return [bundleTree, mainBundleTree];
}
_dropBundle(appTree, bundle) {
let mainBundleTree = new Funnel(appTree, {
exclude: [`src/ui/components/+(${bundle.componentPrefix})*`],
});
let mainBundleJsTree = new Funnel(mainBundleTree, {
include: ['**/*.js'],
});
let mainBundleModuleMap = this.buildResolutionMap(mainBundleJsTree);
return new MergeTrees([mainBundleTree, mainBundleModuleMap], { overwrite: true });
}
_packageSplitBundle(bundleTree, bundle) {
return new Rollup(bundleTree, {
rollup: {
input: 'config/module-map.js',
output: {
file: bundle.file,
name: bundle.moduleName,
format: 'umd',
sourcemap: this.options.sourcemaps.enabled,
},
plugins: [resolve({ jsnext: true, module: true, main: true }), commonjs()],
},
});
}
}
module.exports = function(defaults) {
let allComponents = findAllComponents();
let app = new SimplabsApp(defaults, {
'css-blocks': {
entry: allComponents,
output: 'src/ui/styles/app.css',
},
minifyCSS: {
enabled: process.env.EMBER_ENV === 'production',
},
fingerprint: {
exclude: ['ssr-app.js'],
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map', 'svg'],
replaceExtensions: ['html', 'css', 'js', 'json', 'xml'],
},
rollup: {
plugins: [resolve({ jsnext: true, module: true, main: true }), commonjs()],
},
});
return app.toTree();
};