forked from emberjs/ember-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ember-cli-build.js
408 lines (355 loc) · 11.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'use strict';
const EmberApp = require('ember-cli/lib/broccoli/ember-app');
const VersionChecker = require('ember-cli-version-checker');
const mergeTrees = require('broccoli-merge-trees');
const concatFiles = require('broccoli-concat');
const stew = require('broccoli-stew');
const writeFile = require('broccoli-file-creator');
const replace = require('broccoli-string-replace');
const Babel = require('broccoli-babel-transpiler');
const moduleResolver = require('amd-name-resolver').resolveModules({
throwOnRootAccess: false,
});
const Funnel = require('broccoli-funnel');
const ensurePosix = require('ensure-posix-path');
const path = require('path');
const packageJson = require('./package.json');
const { map, mv } = stew;
const options = {
autoImport: {
forbidEval: true,
},
fingerprint: {
enabled: false,
},
svgJar: {
sourceDirs: ['public/assets/svg'],
},
};
// Firefox requires non-minified assets for review :(
options.minifyJS = { enabled: false };
options.minifyCSS = { enabled: false };
// Stolen from relative-module-paths.js in ember-cli-babel
function getRelativeModulePath(modulePath) {
return ensurePosix(path.relative(process.cwd(), modulePath));
}
// Stolen from relative-module-paths.js in ember-cli-babel
function resolveRelativeModulePath(name, child) {
return moduleResolver(name, getRelativeModulePath(child));
}
module.exports = function (defaults) {
let checker = new VersionChecker(defaults);
let emberChecker = checker.for('ember-source');
if (emberChecker.isAbove('3.0.0')) {
options.vendorFiles = { 'jquery.js': null };
}
if (process.env.EMBER_ENV !== 'test') {
// https://github.com/ef4/ember-auto-import/issues/540
options.autoImport.publicAssetURL = 'assets/';
}
// When running ember-try on Ember < 3.13, colocation support is
// disabled in ember-cli-htmlbars and causes a build error. When
// running ember-try, we actually don't care about the "app" side
// at all – all we do is run the ember_debug tests (via a --filter
// option to ember test in the ember-try config). The only reason
// we are even building the app is to get the test harness (qunit
// and friends) to work. In the long run, we should split up the
// build and not run the app build in ember-try, but in the mean
// time, this drops all *.hbs files (but keeping everything else)
// to avoid the problem. The app will of course not work correctly
// at runtime, but it was never meant to work on old ember versions
// in the first place.
if (!emberChecker.gte('3.13.0')) {
options.trees = {
app: new Funnel('app', {
exclude: ['**/*.hbs'],
}),
};
}
let app = new EmberApp(defaults, options);
// Use `app.import` to add additional libraries to the generated
// output files.
//
// If you need to use different assets in different
// environments, specify an object as the first parameter. That
// object's keys should be the environment name and the values
// should be the asset to use in that environment.
//
// If the library that you are including contains AMD or ES6
// modules that you would like to import into your application
// please specify an object with the list of modules as keys
// along with the exports of each module as its value.
//
const env = process.env.EMBER_ENV;
app.import('vendor/babel-polyfill.js', { prepend: true });
app.import('node_modules/basiccontext/dist/basicContext.min.css');
app.import('node_modules/basiccontext/dist/themes/default.min.css');
app.import('node_modules/basiccontext/dist/basicContext.min.js');
app.import('node_modules/compare-versions/index.js');
app.import('node_modules/normalize.css/normalize.css');
// Ember Debug
let emberDebug = 'ember_debug';
let sourceMap = new Funnel('node_modules/source-map-js', {
files: ['**/*.js'],
destDir: 'ember-debug/deps',
});
sourceMap = new Babel(sourceMap, {
plugins: ['transform-commonjs'],
});
const backburner = new Funnel('node_modules/backburner.js/dist/es6', {
files: ['backburner.js'],
destDir: 'ember-debug/deps',
});
emberDebug = new Funnel(emberDebug, {
destDir: 'ember-debug',
include: ['**/*.js'],
exclude: ['vendor/startup-wrapper.js', 'vendor/loader.js'],
});
emberDebug = mergeTrees([sourceMap, backburner, emberDebug]);
emberDebug = new Babel(emberDebug, {
moduleIds: true,
getModuleId: getRelativeModulePath,
plugins: [
['@babel/plugin-transform-class-properties'],
['@babel/plugin-transform-class-static-block'],
['module-resolver', { resolvePath: resolveRelativeModulePath }],
['@babel/plugin-transform-modules-amd', { noInterop: true }],
],
});
const previousEmberVersionsSupportedString = `[${packageJson.previousEmberVersionsSupported
.map(function (item) {
return `'${item}'`;
})
.join(',')}]`;
const emberVersionsSupportedString = `[${packageJson.emberVersionsSupported
.map(function (item) {
return `'${item}'`;
})
.join(',')}]`;
let startupWrapper = new Funnel('ember_debug', {
srcDir: 'vendor',
files: ['startup-wrapper.js'],
});
startupWrapper = replace(startupWrapper, {
files: ['startup-wrapper.js'],
patterns: [
{
match: /{{EMBER_VERSIONS_SUPPORTED}}/,
replacement: emberVersionsSupportedString,
},
],
});
const loader = new Funnel('ember_debug', {
srcDir: 'vendor',
files: ['loader.js'],
});
emberDebug = mergeTrees([startupWrapper, emberDebug, loader]);
emberDebug = concatFiles(emberDebug, {
headerFiles: ['loader.js'],
inputFiles: ['**/*.js'],
outputFile: '/ember_debug.js',
sourceMapConfig: { enabled: false },
});
function wrapWithLoader(content) {
return `(function loadEmberDebugInWebpage() {
const waitForEmberLoad = new Promise((resolve) => {
if (window.requireModule) {
const has =
window.requireModule.has ||
function has(id) {
return !!(
window.requireModule.entries[id] ||
window.requireModule.entries[id + '/index']
);
};
if (has('ember')) {
return resolve();
}
}
/**
* NOTE: if the above (for some reason) fails and the consuming app has
* deprecation-workflow's throwOnUnhandled: true
* or set \`ember-global\`'s handler to 'throw'
* and is using at least \`[email protected]\`
*
* this will throw an exception in the consuming project
*/
if (window.Ember) return resolve();
window.addEventListener('Ember', resolve, { once: true });
});
waitForEmberLoad.then(() => ${content});
})()
`;
}
const emberDebugs = [];
['basic', 'chrome', 'firefox', 'bookmarklet', 'websocket'].forEach(function (
dist
) {
emberDebugs[dist] = map(emberDebug, '**/*.js', function (content) {
return wrapWithLoader(
`(function(adapter, env) {\n${content}\n}('${dist}', '${env}'))`
);
});
});
let tree = app.toTree();
const emberInspectorVersionPattern = [
{
match: /{{EMBER_INSPECTOR_VERSION}}/g,
replacement: packageJson.version,
},
];
tree = replace(tree, {
files: ['**/*.js'],
patterns: emberInspectorVersionPattern,
});
const minimumVersion = packageJson.emberVersionsSupported[0].replace(
/\./g,
'-'
);
const webExtensionRoot = `panes-${minimumVersion}`;
let tabLabel;
if (process.env.EMBER_INSPECTOR_TAB) {
tabLabel = `Ember [${process.env.EMBER_INSPECTOR_TAB}]`;
} else if (env === 'development') {
tabLabel = `Ember [DEV]`;
} else {
tabLabel = 'Ember';
}
let replacementPattern = [
{
match: /{{TAB_LABEL}}/,
replacement: tabLabel,
},
{
match: /{{PANE_ROOT}}/g,
replacement: `panes-${minimumVersion}`,
},
{
match: /{{PREVIOUS_EMBER_VERSIONS_SUPPORTED}}/g,
replacement: previousEmberVersionsSupportedString,
},
{
match: /{{EMBER_VERSIONS_SUPPORTED}}/g,
replacement: emberVersionsSupportedString,
},
];
replacementPattern = replacementPattern.concat(emberInspectorVersionPattern);
const firefoxBackgroundServiceReplacement = [
{
match: /"service_worker": "background.js"/g,
replacement: '"scripts": ["background.js"]',
},
];
const skeletonWebExtension = replace('skeletons/web-extension', {
files: ['*'],
patterns: replacementPattern,
});
const skeletonFirefoxWebExtension = replace('skeletons/web-extension', {
files: ['*'],
patterns: replacementPattern.concat(firefoxBackgroundServiceReplacement),
});
const skeletonBookmarklet = replace('skeletons/bookmarklet', {
files: ['*'],
patterns: replacementPattern,
});
let firefox = mergeTrees([
mv(mergeTrees([tree, emberDebugs.firefox]), webExtensionRoot),
skeletonFirefoxWebExtension,
]);
let chrome = mergeTrees([
mv(mergeTrees([tree, emberDebugs.chrome]), webExtensionRoot),
skeletonWebExtension,
]);
let bookmarklet = mergeTrees([
mv(mergeTrees([tree, emberDebugs.bookmarklet]), webExtensionRoot),
skeletonBookmarklet,
]);
packageJson.previousEmberVersionsSupported.forEach(function (version) {
version = version.replace(/\./g, '-');
if (env === 'production') {
const prevDist = `dist_prev/${env}`;
bookmarklet = mergeTrees([
mv(`${prevDist}/bookmarklet/panes-${version}`, `panes-${version}`),
bookmarklet,
]);
firefox = mergeTrees([
mv(`${prevDist}/firefox/panes-${version}`, `panes-${version}`),
firefox,
]);
chrome = mergeTrees([
mv(`${prevDist}/chrome/panes-${version}`, `panes-${version}`),
chrome,
]);
} else {
const file = writeFile(
'index.html',
'This Ember version is not supported in development environment.'
);
const emberDebugFile = writeFile('ember_debug.js', 'void(0);');
chrome = mergeTrees([mv(file, `panes-${version}`), chrome]);
firefox = mergeTrees([mv(file, `panes-${version}`), firefox]);
bookmarklet = mergeTrees([
mv(file, `panes-${version}`),
mv(emberDebugFile, `panes-${version}`),
bookmarklet,
]);
}
});
// Pass the current dist to the Ember Inspector app.
// EMBER DIST
const dists = {
chrome,
firefox,
bookmarklet,
websocket: mergeTrees([tree, emberDebugs.websocket]),
basic: mergeTrees([tree, emberDebugs.basic]),
};
Object.keys(dists).forEach(function (key) {
dists[key] = replace(dists[key], {
files: ['**/*.js'],
patterns: [
{
match: /{{EMBER_DIST}}/g,
replacement: key,
},
],
});
});
// Add {{ remote-port }} to the head
// so that the websocket addon can replace it.
dists.websocket = replace(dists.websocket, {
files: ['index.html'],
patterns: [
{
match: /<head>/,
replacement: '<head>\n{{ remote-port }}\n',
},
],
});
let output;
if (env === 'test') {
// `ember test` expects the index.html file to be in the
// output directory.
// Change base tag for running tests in development env.
dists.basic = replace(dists.basic, {
files: ['tests/index.html'],
patterns: [
{
match: /<base.*\/>/,
replacement: '',
},
],
});
output = mergeTrees([dists.basic, dists.chrome]);
} else {
dists.testing = mergeTrees([dists.basic, dists.chrome]);
output = mergeTrees([
mv(dists.bookmarklet, 'bookmarklet'),
mv(dists.firefox, 'firefox'),
mv(dists.chrome, 'chrome'),
mv(dists.websocket, 'websocket'),
mv(dists.testing, 'testing'),
]);
}
return output;
};