-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
index.js
576 lines (458 loc) · 17.1 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
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
'use strict';
const VersionChecker = require('ember-cli-version-checker');
const clone = require('clone');
const path = require('path');
const semver = require('semver');
const defaultShouldIncludeHelpers = require('./lib/default-should-include-helpers');
const findApp = require('./lib/find-app');
const APP_BABEL_RUNTIME_VERSION = new WeakMap();
let count = 0;
module.exports = {
name: 'ember-cli-babel',
configKey: 'ember-cli-babel',
init() {
this._super.init && this._super.init.apply(this, arguments);
let checker = new VersionChecker(this);
let dep = checker.for('ember-cli', 'npm');
if (dep.lt('2.13.0')) {
throw new Error(`ember-cli-babel@7 (used by ${this._parentName()} at ${this.parent.root}) cannot be used by ember-cli versions older than 2.13, you used ${dep.version}`);
}
},
buildBabelOptions(_config) {
let config = _config || this._getAddonOptions();
return this._getBabelOptions(config);
},
_debugTree() {
if (!this._cachedDebugTree) {
this._cachedDebugTree = require('broccoli-debug').buildDebugCallback(`ember-cli-babel:${this._parentName()}`);
}
return this._cachedDebugTree.apply(null, arguments);
},
transpileTree(inputTree, config) {
let description = `000${++count}`.slice(-3);
let postDebugTree = this._debugTree(inputTree, `${description}:input`);
let options = this.buildBabelOptions(config);
let output;
if (this._shouldDoNothing(options)) {
output = postDebugTree;
} else {
let BabelTranspiler = require('broccoli-babel-transpiler');
output = new BabelTranspiler(postDebugTree, options);
}
return this._debugTree(output, `${description}:output`);
},
setupPreprocessorRegistry(type, registry) {
registry.add('js', {
name: 'ember-cli-babel',
ext: this._getExtensions(this._getAddonOptions()),
toTree: (tree) => this.transpileTree(tree)
});
},
_shouldIncludePolyfill() {
let addonOptions = this._getAddonOptions();
let customOptions = addonOptions['ember-cli-babel'];
if (customOptions && 'includePolyfill' in customOptions) {
return customOptions.includePolyfill === true;
} else {
return false;
}
},
_importPolyfill(app) {
let polyfillPath = 'vendor/babel-polyfill/polyfill.js';
if (this.import) { // support for ember-cli >= 2.7
this.import(polyfillPath, { prepend: true });
} else if (app.import) { // support ember-cli < 2.7
app.import(polyfillPath, { prepend: true });
} else {
// eslint-disable-next-line no-console
console.warn('Please run: ember install ember-cli-import-polyfill');
}
},
_shouldIncludeHelpers(options) {
let appOptions = this._getAppOptions();
let customOptions = appOptions['ember-cli-babel'];
let shouldIncludeHelpers = false;
if (!this._shouldCompileModules(options)) {
// we cannot use external helpers if we are not transpiling modules
return false;
} else if (customOptions && 'includeExternalHelpers' in customOptions) {
shouldIncludeHelpers = customOptions.includeExternalHelpers === true;
} else {
// Check the project to see if we should include helpers based on heuristics.
shouldIncludeHelpers = defaultShouldIncludeHelpers(this.project);
}
let appEmberCliBabelPackage = this.project.addons.find(a => a.name === 'ember-cli-babel').pkg;
let appEmberCliBabelVersion = appEmberCliBabelPackage && appEmberCliBabelPackage.version;
if (appEmberCliBabelVersion && semver.gte(appEmberCliBabelVersion, '7.3.0-beta.1')) {
return shouldIncludeHelpers;
} else if (shouldIncludeHelpers) {
this.project.ui.writeWarnLine(
`${this._parentName()} attempted to include external babel helpers to make your build size smaller, but your root app's ember-cli-babel version is not high enough. Please update ember-cli-babel to v7.3.0-beta.1 or later.`
);
}
return false;
},
_getHelperVersion() {
if (!APP_BABEL_RUNTIME_VERSION.has(this.project)) {
let checker = new VersionChecker(this.project);
APP_BABEL_RUNTIME_VERSION.set(this.project, checker.for('@babel/runtime', 'npm').version);
}
return APP_BABEL_RUNTIME_VERSION.get(this.project);
},
_getHelpersPlugin() {
return [
[
require.resolve('@babel/plugin-transform-runtime'),
{
version: this._getHelperVersion(),
regenerator: false,
useESModules: true
}
]
]
},
treeForAddon() {
// Helpers are a global config, so only the root application should bother
// generating and including the file.
let isRootBabel = this.parent === this.project;
let shouldIncludeHelpers = isRootBabel && this._shouldIncludeHelpers(this._getAppOptions());
if (!shouldIncludeHelpers) { return; }
const path = require('path');
const Funnel = require('broccoli-funnel');
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
const babelHelpersPath = path.dirname(require.resolve('@babel/runtime/package.json'));
let babelHelpersTree = new Funnel(new UnwatchedDir(babelHelpersPath), {
srcDir: 'helpers/esm',
destDir: '@babel/runtime/helpers/esm'
});
return this.transpileTree(babelHelpersTree, {
'ember-cli-babel': {
// prevents the helpers from being double transpiled, and including themselves
disablePresetEnv: true
}
});
},
treeForVendor() {
if (!this._shouldIncludePolyfill()) return;
const Funnel = require('broccoli-funnel');
const UnwatchedDir = require('broccoli-source').UnwatchedDir;
// Find babel-core's browser polyfill and use its directory as our vendor tree
let polyfillDir = path.dirname(require.resolve('@babel/polyfill/dist/polyfill'));
let polyfillTree = new Funnel(new UnwatchedDir(polyfillDir), {
destDir: 'babel-polyfill'
});
return polyfillTree;
},
included: function(app) {
this._super.included.apply(this, arguments);
this.app = app;
if (this._shouldIncludePolyfill()) {
this._importPolyfill(app);
}
},
isPluginRequired(pluginName) {
let targets = this._getTargets();
// if no targets are setup, assume that all plugins are required
if (!targets) { return true; }
const isPluginRequired = require('@babel/preset-env').isPluginRequired;
const pluginList = require('@babel/preset-env/data/plugins');
return isPluginRequired(targets, pluginList[pluginName]);
},
_getAddonOptions() {
let parentOptions = this.parent && this.parent.options;
let appOptions = this.app && this.app.options;
if (parentOptions) {
let customAddonOptions = parentOptions['ember-cli-babel'];
if (customAddonOptions && 'includeExternalHelpers' in customAddonOptions) {
throw new Error('includeExternalHelpers is not supported in addon configurations, it is an app-wide configuration option');
}
}
return parentOptions || appOptions || {};
},
_getAppOptions() {
let app = findApp(this);
return (app && app.options) || {};
},
_parentName() {
let parentName;
if (this.parent) {
if (typeof this.parent.name === 'function') {
parentName = this.parent.name();
} else {
parentName = this.parent.name;
}
}
return parentName;
},
_getAddonProvidedConfig(addonOptions) {
let options = clone(addonOptions.babel || {});
let plugins = options.plugins || [];
let postTransformPlugins = options.postTransformPlugins || [];
return {
options,
plugins,
postTransformPlugins
};
},
_getExtensions(config) {
let emberCLIBabelConfig = config['ember-cli-babel'] || {};
return emberCLIBabelConfig.extensions || ['js'];
},
_getBabelOptions(config) {
let addonProvidedConfig = this._getAddonProvidedConfig(config);
let shouldCompileModules = this._shouldCompileModules(config);
let shouldIncludeHelpers = this._shouldIncludeHelpers(config);
let shouldIncludeDecoratorPlugins = this._shouldIncludeDecoratorPlugins(config);
let emberCLIBabelConfig = config['ember-cli-babel'];
let shouldRunPresetEnv = true;
let providedAnnotation;
let throwUnlessParallelizable;
if (emberCLIBabelConfig) {
providedAnnotation = emberCLIBabelConfig.annotation;
shouldRunPresetEnv = !emberCLIBabelConfig.disablePresetEnv;
throwUnlessParallelizable = emberCLIBabelConfig.throwUnlessParallelizable;
}
let sourceMaps = false;
if (config.babel && 'sourceMaps' in config.babel) {
sourceMaps = config.babel.sourceMaps;
}
let filterExtensions = this._getExtensions(config);
let options = {
annotation: providedAnnotation || `Babel: ${this._parentName()}`,
sourceMaps,
throwUnlessParallelizable,
filterExtensions
};
let userPlugins = addonProvidedConfig.plugins;
let userPostTransformPlugins = addonProvidedConfig.postTransformPlugins;
if (shouldIncludeDecoratorPlugins) {
userPlugins = this._addDecoratorPlugins(userPlugins.slice(), addonProvidedConfig.options);
}
options.plugins = [].concat(
shouldIncludeHelpers && this._getHelpersPlugin(),
userPlugins,
this._getDebugMacroPlugins(config),
this._getEmberModulesAPIPolyfill(config),
shouldCompileModules && this._getModulesPlugin(),
userPostTransformPlugins
).filter(Boolean);
options.presets = [
shouldRunPresetEnv && this._getPresetEnv(addonProvidedConfig),
].filter(Boolean);
if (shouldCompileModules) {
options.moduleIds = true;
options.getModuleId = require('./lib/relative-module-paths').getRelativeModulePath;
}
options.highlightCode = this._shouldHighlightCode();
options.babelrc = false;
return options;
},
_shouldIncludeDecoratorPlugins(config) {
let customOptions = config['ember-cli-babel'] || {};
return customOptions.disableDecoratorTransforms !== true;
},
_addDecoratorPlugins(plugins, options) {
const { hasPlugin, addPlugin } = require('ember-cli-babel-plugin-helpers');
if (hasPlugin(plugins, '@babel/plugin-proposal-decorators')) {
if (this.parent === this.project) {
this.project.ui.writeWarnLine(`${
this._parentName()
} has added the decorators plugin to its build, but ember-cli-babel provides these by default now! You can remove the transforms, or the addon that provided them, such as @ember-decorators/babel-transforms. Ember supports the stage 1 decorator spec and transforms, so if you were using stage 2, you'll need to ensure that your decorators are compatible, or convert them to stage 1.`);
}
} else {
addPlugin(
plugins,
[require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
{
before: ['@babel/plugin-proposal-class-properties', '@babel/plugin-transform-typescript']
}
);
}
if (hasPlugin(plugins, '@babel/plugin-proposal-class-properties')) {
if (this.parent === this.project) {
this.project.ui.writeWarnLine(`${
this._parentName()
} has added the class-properties plugin to its build, but ember-cli-babel provides these by default now! You can remove the transforms, or the addon that provided them, such as @ember-decorators/babel-transforms.`);
}
} else {
addPlugin(
plugins,
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: options.loose || false }],
{
after: ['@babel/plugin-proposal-decorators'],
before: ['@babel/plugin-transform-typescript']
}
);
}
if (hasPlugin(plugins, 'babel-plugin-filter-imports')) {
let checker = new VersionChecker(this.parent).for('babel-plugin-filter-imports', 'npm');
if (checker.lt('3.0.0')) {
addPlugin(
plugins,
require.resolve('./lib/dedupe-internal-decorators-plugin'),
{
after: ['babel-plugin-filter-imports']
}
);
}
}
return plugins;
},
_getDebugMacroPlugins(config) {
let addonOptions = config['ember-cli-babel'] || {};
if (addonOptions.disableDebugTooling) {
return;
}
const isProduction = process.env.EMBER_ENV === 'production';
const isDebug = !isProduction;
return [
[
require.resolve('babel-plugin-debug-macros'),
{
flags: [
{
source: '@glimmer/env',
flags: { DEBUG: isDebug, CI: !!process.env.CI },
},
],
externalizeHelpers: {
global: 'Ember',
},
debugTools: {
isDebug,
source: '@ember/debug',
assertPredicateIndex: 1,
},
},
'@ember/debug stripping',
],
[
require.resolve('babel-plugin-debug-macros'),
{
// deprecated import path https://github.com/emberjs/ember.js/pull/17926#issuecomment-484987305
externalizeHelpers: {
global: 'Ember',
},
debugTools: {
isDebug,
source: '@ember/application/deprecations',
assertPredicateIndex: 1,
},
},
'@ember/application/deprecations stripping',
],
];
},
_getEmberModulesAPIPolyfill(config) {
let addonOptions = config['ember-cli-babel'] || {};
if (addonOptions.disableEmberModulesAPIPolyfill) { return; }
if (this._emberVersionRequiresModulesAPIPolyfill()) {
const blacklist = this._getEmberModulesAPIBlacklist();
return [[require.resolve('babel-plugin-ember-modules-api-polyfill'), { blacklist }]];
}
},
_getPresetEnv(config) {
let options = config.options;
let targets = this.project && this.project.targets;
let presetOptions = Object.assign({}, options, {
modules: false,
targets
});
// delete any properties added to `options.babel` that
// are invalid for @babel/preset-env
delete presetOptions.sourceMaps;
delete presetOptions.plugins;
delete presetOptions.postTransformPlugins;
return [require.resolve('@babel/preset-env'), presetOptions];
},
_getTargets() {
let targets = this.project && this.project.targets;
let parser = require('@babel/preset-env/lib/targets-parser').default;
if (typeof targets === 'object' && targets !== null) {
return parser(targets);
} else {
return targets;
}
},
_getModulesPlugin() {
const resolvePath = require('./lib/relative-module-paths').resolveRelativeModulePath;
return [
[require.resolve('babel-plugin-module-resolver'), { resolvePath }],
[require.resolve('@babel/plugin-transform-modules-amd'), { noInterop: true }],
];
},
/*
* Used to discover if the addon's current configuration will compile modules
* or not.
*
* @public
* @method shouldCompileModules
*/
shouldCompileModules() {
return this._shouldCompileModules(this._getAddonOptions());
},
// will use any provided configuration
_shouldCompileModules(options) {
let addonOptions = options['ember-cli-babel'];
if (addonOptions && 'compileModules' in addonOptions) {
return addonOptions.compileModules;
} else {
return semver.gt(this.project.emberCLIVersion(), '2.12.0-alpha.1');
}
},
_emberVersionRequiresModulesAPIPolyfill() {
// once a version of Ember ships with the
// emberjs/rfcs#176 modules natively this will
// be updated to detect that and return false
return true;
},
_getEmberModulesAPIBlacklist() {
const blacklist = {
'@ember/debug': ['assert', 'deprecate', 'warn'],
'@ember/application/deprecations': ['deprecate'],
};
if (this._shouldBlacklistEmberString()) {
blacklist['@ember/string'] = [
'fmt', 'loc', 'w',
'decamelize', 'dasherize', 'camelize',
'classify', 'underscore', 'capitalize',
'setStrings', 'getStrings', 'getString'
];
}
if (this._shouldBlacklistJQuery()) {
blacklist['jquery'] = ['default'];
}
return blacklist;
},
_isProjectName(dependency) {
return this.project.name && this.project.name() === dependency;
},
_isTransitiveDependency(dependency) {
return (
!(dependency in this.parent.dependencies()) &&
!(dependency in this.project.dependencies())
)
},
_shouldBlacklistEmberString() {
let packageName = '@ember/string';
if (this._isProjectName(packageName)) { return true; }
if (this._isTransitiveDependency(packageName)) { return false; }
let checker = new VersionChecker(this.parent).for(packageName, 'npm');
return checker.exists();
},
_shouldBlacklistJQuery() {
let packageName = '@ember/jquery';
if (this._isProjectName(packageName)) { return true; }
if (this._isTransitiveDependency(packageName)) { return false; }
let checker = new VersionChecker(this.parent).for(packageName, 'npm');
return checker.gte('0.6.0');
},
_shouldHighlightCode() {
let checker = new VersionChecker(this.parent).for('broccoli-middleware', 'npm');
return checker.gte('2.1.0');
},
// detect if running babel would do nothing... and do nothing instead
_shouldDoNothing(options) {
return !options.sourceMaps && !options.plugins.length;
}
};