forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ember-cli-build.js
357 lines (314 loc) · 10 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
/* jshint node: true */
// To create fast production builds (without ES3 support, minification, derequire, or JSHint)
// run the following:
//
// DISABLE_ES3=true DISABLE_JSCS=true DISABLE_JSHINT=true DISABLE_MIN=true DISABLE_DEREQUIRE=true ember serve --environment=production
var fs = require('fs');
var path = require('path');
var EmberBuild = require('emberjs-build');
var getPackages = require('./lib/packages');
var getGitInfo = require('git-repo-info');
var applyFeatureFlags = require('babel-plugin-feature-flags');
var filterImports = require('babel-plugin-filter-imports');
var vendoredPackage = require('emberjs-build/lib/vendored-package');
var htmlbarsPackage = require('emberjs-build/lib/htmlbars-package');
var replaceVersion = require('emberjs-build/lib/utils/replace-version');
var Funnel = require('broccoli-funnel');
var Rollup = require('broccoli-rollup');
var mergeTrees = require('broccoli-merge-trees');
var replace = require('broccoli-string-replace');
var uglify = require('broccoli-uglify-sourcemap');
var rollupEnifed = {
transformBundle(code, options) {
return {
code: code.replace(/\bdefine\(/, 'enifed('),
map: { mappings: null }
};
}
};
function dag() {
var es = new Funnel(path.dirname(require.resolve('dag-map')), {
files: ['dag-map.js'],
annotation: 'dag-map.js'
});
return new Rollup(es, {
rollup: {
plugins: [rollupEnifed],
entry: 'dag-map.js',
dest: 'dag-map.js',
format: 'amd',
moduleId: 'dag-map',
exports: 'named'
},
annotation: 'dag-map.js'
});
}
function backburner() {
var dist = path.dirname(require.resolve('backburner.js'));
dist = path.join(dist, 'es6');
return new Rollup(new Funnel(dist, {
files: ['backburner.js']
}), {
rollup: {
plugins: [rollupEnifed],
entry: 'backburner.js',
dest: 'backburner.js',
format: 'amd',
moduleId: 'backburner',
exports: 'named'
},
annotation: 'backburner.js'
});
}
function rsvp() {
var transpileES6 = require('emberjs-build/lib/utils/transpile-es6');
var lib = path.resolve(path.dirname(require.resolve('rsvp')), '../lib');
var rollup = new Rollup(lib, {
rollup: {
entry: 'rsvp.js',
dest: 'rsvp.js',
format: 'es',
exports: 'named'
},
annotation: 'rsvp.js'
});
return transpileES6(rollup);
}
function routeRecognizer() {
var packageJson = require('route-recognizer/package');
var packageDir = path.dirname(require.resolve('route-recognizer/package'));
var entry = path.join(packageDir, packageJson['module'] || packageJson['js:next'] || packageJson['main'].replace(/dist\//, 'dist/es6/'));
var basename = path.basename(entry);
var es6 = new Funnel(path.dirname(entry), {
files: [ basename ]
});
return new Rollup(es6, {
rollup: {
plugins: [rollupEnifed],
entry: basename,
dest: 'route-recognizer.js',
format: 'amd',
moduleId: 'route-recognizer',
exports: 'named'
},
annotation: 'route-recognizer.js'
});
}
function router() {
return new Rollup(path.resolve(path.dirname(require.resolve('router_js')), '../lib'), {
rollup: {
plugins: [rollupEnifed],
external: ['route-recognizer', 'rsvp'],
entry: 'router.js',
dest: 'router.js',
format: 'amd',
moduleId: 'router',
exports: 'named'
},
annotation: 'router.js'
});
}
var featuresJson = fs.readFileSync('./features.json', { encoding: 'utf8' });
function getFeatures(environment) {
var features = JSON.parse(featuresJson).features;
var featureName;
if (process.env.BUILD_TYPE === 'alpha') {
for (featureName in features) {
if (features[featureName] === null) {
features[featureName] = false;
}
}
}
if (process.env.OVERRIDE_FEATURES) {
var forcedFeatures = process.env.OVERRIDE_FEATURES.split(',');
for (var i = 0; i < forcedFeatures.length; i++) {
featureName = forcedFeatures[i];
features[featureName] = true;
}
}
features['ember-glimmer-allow-backtracking-rerender'] = false;
if (process.env.ALLOW_BACKTRACKING) {
features['ember-glimmer-allow-backtracking-rerender'] = true;
features['ember-glimmer-detect-backtracking-rerender'] = false;
}
var isDevelopment = (environment === 'development');
features['mandatory-setter'] = isDevelopment;
features['ember-glimmer-detect-backtracking-rerender'] = isDevelopment;
return features;
}
function babelConfigFor(environment) {
var plugins = [];
var features = getFeatures(environment);
var includeDevHelpers = true;
plugins.push(applyFeatureFlags({
imports: [
{ module: 'ember-metal/features' },
{ module: 'ember-metal', name: 'isFeatureEnabled' },
],
features: features
}));
var isProduction = (environment === 'production');
if (isProduction) {
includeDevHelpers = false;
plugins.push(filterImports({
'ember-metal/debug': ['assert', 'debug', 'deprecate', 'info', 'runInDebug', 'warn', 'debugSeal', 'debugFreeze'],
'ember-metal': ['assert', 'debug', 'deprecate', 'info', 'runInDebug', 'warn', 'debugSeal', 'debugFreeze']
}));
}
return {
plugins: plugins,
includeDevHelpers: includeDevHelpers,
helperWhiteList: [
'inherits',
'class-call-check',
'tagged-template-literal-loose',
'slice',
'defaults',
'create-class',
'interop-export-wildcard'
]
};
}
var glimmerEngine = require('glimmer-engine/ember-cli-build')({
shouldExternalizeHelpers: true,
stripRuntimeChecks: true
});
var find = require('broccoli-stew').find;
function glimmerPackage(name) {
return replace(find(glimmerEngine, 'named-amd/' + name + '/**/*.js'), {
files: ['**/*.js'],
pattern: {
match: /\/\/#\s+sourceMappingURL.*/g,
replacement: ''
},
annotation: 'strip sourceMappingURL'
});
}
function getVersion() {
var projectPath = process.cwd();
var info = getGitInfo(projectPath);
if (info.tag) {
return info.tag.replace(/^v/, '');
}
var packageVersion = require(path.join(projectPath, 'package.json')).version;
var sha = info.sha || '';
var prefix = packageVersion + '-' + (process.env.BUILD_TYPE || info.branch || process.env.TRAVIS_BRANCH);
prefix = prefix.replace('master', 'canary');
return prefix + '+' + sha.slice(0, 8);
}
// non bundled vendor
function jquery() {
var jquery = require.resolve('jquery');
return new Funnel(path.dirname(jquery), {
files: ['jquery.js'],
destDir: 'jquery',
annotation: 'jquery/jquery.js'
});
}
// TEST files
function qunit() {
var qunitjs = require.resolve('qunitjs');
return new Funnel(path.dirname(qunitjs), {
files: ['qunit.js', 'qunit.css'],
destDir: 'qunit',
annotation: 'qunit/qunit.{js|css}'
});
}
module.exports = function() {
var features = getFeatures();
var version = getVersion();
var vendorPackages = {
'external-helpers': vendoredPackage('external-helpers'),
'loader': vendoredPackage('loader'),
'rsvp': rsvp(),
'backburner': backburner(),
'router': router(),
'dag-map': dag(),
'route-recognizer': routeRecognizer(),
'simple-html-tokenizer': htmlbarsPackage('simple-html-tokenizer', { libPath: 'node_modules/glimmer-engine/dist/es6' }),
'glimmer': glimmerPackage('glimmer'),
'glimmer-compiler': glimmerPackage('glimmer-compiler'),
'glimmer-reference': glimmerPackage('glimmer-reference'),
'glimmer-runtime': glimmerPackage('glimmer-runtime'),
'glimmer-node': glimmerPackage('glimmer-node'),
'glimmer-syntax': glimmerPackage('glimmer-syntax'),
'glimmer-test-helpers': glimmerPackage('glimmer-test-helpers'),
'glimmer-util': glimmerPackage('glimmer-util'),
'glimmer-wire-format': glimmerPackage('glimmer-wire-format'),
'handlebars': glimmerPackage('handlebars') // inlined parser
};
// Replace _getBowerTree with one from npm
EmberBuild.prototype._getBowerTree = function getBowerTree() {
return mergeTrees([
qunit(),
jquery(),
replaceVersion(new Funnel('config/package_manager_files', {
destDir: '/'
}), {
version: version
})
]);
};
EmberBuild.prototype._minifySourceTree = function (tree, options) {
var srcFile = options.srcFile;
var destFile = options.destFile;
var files = [ srcFile ];
var mapSrcFile = srcFile.slice(0, -2) + 'map';
var mapDestFile = destFile.slice(0, -2) + 'map';
if (options.enableSourceMaps) {
files.push(mapSrcFile);
}
// because broccoli-uglify-sourcemap doesn't use persistent filter
var reducedTree = new Funnel(tree, {
annotation: 'reduce for minify',
files: files
});
var minifiedTree = new uglify(reducedTree, {
sourceMapConfig: {
enabled: options.enableSourceMaps
},
mangle: true,
compress: {
// this is adversely affects heuristics for IIFE eval
negate_iife: false,
// limit sequences because of memory issues during parsing
sequences: 30
},
output: {
// no difference in size
// and much easier to debug
semicolons: false
}
});
return new Funnel(minifiedTree, {
annotation: 'rename minified',
files: files,
getDestinationPath: function(relativePath) {
if (relativePath === srcFile) {
return destFile;
}
if (relativePath === mapSrcFile) {
return mapDestFile;
}
return relativePath;
}
});
};
var emberBuild = new EmberBuild({
babel: {
development: babelConfigFor('development'),
production: babelConfigFor('production')
},
features: {
development: getFeatures('development'),
production: getFeatures('production')
},
glimmer: require('glimmer-engine'),
packages: getPackages(features),
vendoredPackages: vendorPackages,
version: version
});
return emberBuild.getDistTrees();
};
module.exports.getFeatures = getFeatures;