-
Notifications
You must be signed in to change notification settings - Fork 142
/
v1-addon.ts
899 lines (819 loc) · 30.1 KB
/
v1-addon.ts
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
import { Memoize } from 'typescript-memoize';
import { dirname, isAbsolute, join, relative } from 'path';
import { sync as pkgUpSync } from 'pkg-up';
import { existsSync, pathExistsSync } from 'fs-extra';
import Funnel, { Options as FunnelOptions } from 'broccoli-funnel';
import { UnwatchedDir, WatchedDir } from 'broccoli-source';
import RewritePackageJSON from './rewrite-package-json';
import { todo, unsupported } from '@embroider/core/src/messages';
import { Tree } from 'broccoli-plugin';
import mergeTrees from 'broccoli-merge-trees';
import semver from 'semver';
import rewriteAddonTree from './rewrite-addon-tree';
import { mergeWithAppend } from './merges';
import { AddonMeta, TemplateCompiler, debug, PackageCache, Resolver } from '@embroider/core';
import Options from './options';
import walkSync from 'walk-sync';
import ObserveTree from './observe-tree';
import { Options as HTMLBarsOptions } from 'ember-cli-htmlbars';
import { isEmbroiderMacrosPlugin } from '@embroider/macros';
import { TransformOptions, PluginItem } from '@babel/core';
import V1App from './v1-app';
import modulesCompat from './modules-compat';
import writeFile from 'broccoli-file-creator';
import SynthesizeTemplateOnlyComponents from './synthesize-template-only-components';
import { isEmberAutoImportDynamic } from './detect-ember-auto-import';
import { ResolvedDep } from '@embroider/core/src/resolver';
const stockTreeNames = Object.freeze([
'addon',
'addon-styles',
'styles',
'addon-test-support',
'test-support',
'app',
'public',
'vendor',
// 'addon-templates' and 'templates are trees too, but they live inside
// 'addon' and 'app' and we handle them there.
]);
const dynamicTreeHooks = Object.freeze([
'treeFor',
'treeForAddon',
'treeForAddonTemplates',
'treeForAddonTestSupport',
'treeForApp',
'treeForPublic',
'treeForStyles',
'treeForTemplates',
'treeForTestSupport',
'treeForVendor',
]);
const appPublicationDir = '_app_';
const fastbootPublicationDir = '_fastboot_';
/**
* Creating a interface here just to keep the Resolver's structure as it is.
*/
interface ResolverParams {
root: string;
modulePrefix: string;
}
class V1AddonCompatResolver implements Resolver {
params: ResolverParams;
constructor(params: ResolverParams) {
this.params = params;
}
astTransformer(_templateCompiler: TemplateCompiler): unknown {
return;
}
dependenciesOf(_moduleName: string): ResolvedDep[] {
return [];
}
absPathToRuntimeName(moduleName: string) {
if (isAbsolute(moduleName)) {
return moduleName;
}
return join(this.params.modulePrefix, moduleName);
}
}
// This controls and types the interface between our new world and the classic
// v1 addon instance.
export default class V1Addon {
constructor(
protected addonInstance: any,
protected addonOptions: Required<Options>,
private app: V1App,
private packageCache: PackageCache,
private orderIdx: number
) {
if (addonInstance.registry) {
this.updateRegistry(addonInstance.registry);
}
}
// this is only defined when there are custom AST transforms that need it
@Memoize()
private get templateCompiler(): TemplateCompiler | undefined {
let htmlbars = this.addonInstance.addons.find((a: any) => a.name === 'ember-cli-htmlbars');
if (htmlbars) {
let options = htmlbars.htmlbarsOptions() as HTMLBarsOptions;
if (options.plugins && options.plugins.ast) {
// our macros don't run here in stage1
options.plugins.ast = options.plugins.ast.filter((p: any) => !isEmbroiderMacrosPlugin(p));
if (options.plugins.ast.length > 0) {
return new TemplateCompiler({
compilerPath: options.templateCompilerPath,
EmberENV: {},
plugins: options.plugins,
resolver: this.templateResolver(),
});
}
}
}
}
@Memoize()
templateResolver(): Resolver {
return new V1AddonCompatResolver({
root: this.app.root,
modulePrefix: this.moduleName,
});
}
private updateRegistry(registry: any) {
// auto-import gets disabled because we support it natively
registry.remove('js', 'ember-auto-import-analyzer');
// here we're replacing the stock template compiler with our own. Ours
// leaves hbs files as hbs, not js. The only transformation it is supposed
// to do is applying any custom AST transforms and reserializing the results
// back to HBS.
//
// Even when no AST transforms are registered, we'll still need to register
// a no-op transform here to avoid an exception coming out of ember-cli like
// "Addon templates were detected, but there are no template compilers
// registered".
registry.remove('template', 'ember-cli-htmlbars');
registry.add('template', {
name: 'embroider-addon-templates',
ext: 'hbs',
_addon: this,
toTree(this: { _addon: V1Addon }, tree: Tree): Tree {
if (this._addon.templateCompiler) {
return this._addon.templateCompiler.applyTransformsToTree(tree);
} else {
// when there are no custom AST transforms, we don't need to do
// anything at all.
return tree;
}
},
});
if (this.needsCustomBabel()) {
// there is customized babel behavior needed, so we will leave
// ember-cli-babel in place, but modify its config so it doesn't do the
// things we don't want to do in stage1.
this.updateBabelConfig();
} else {
// no custom babel behavior, so we don't run the ember-cli-babel
// preprocessor at all. We still need to register a no-op preprocessor to
// prevent ember-cli from emitting a deprecation warning.
registry.remove('js', 'ember-cli-babel');
registry.add('js', {
name: 'embroider-babel-noop',
ext: 'js',
toTree(tree: Tree) {
return tree;
},
});
}
}
// we need to run custom inline hbs preprocessing if there are custom hbs
// plugins and there are inline hbs templates
private needsInlineHBS(): boolean {
if (!this.templateCompiler) {
// no custom transforms
return false;
}
if (this.addonInstance.addons.find((a: any) => a.name === 'ember-cli-htmlbars-inline-precompile')) {
// the older inline template compiler is present
return true;
}
if (
this.addonInstance.addons.find(
(a: any) =>
a.name === 'ember-cli-htmlbars' && semver.satisfies(semver.coerce(a.pkg.version) || a.pkg.version, '>4.0.0')
)
) {
// a version of ember-cli-htmlbars that natively supports inline hbs is present
return true;
}
return false;
}
private needsCustomBabel() {
let babelConfig = this.options.babel as TransformOptions | undefined;
if (babelConfig && babelConfig.plugins && babelConfig.plugins.length > 0) {
// this addon has custom babel plugins, so we need to run them here in
// stage1
return true;
}
// even if there are no custom babel plugins, if we need to do any
// preprocessing of inline handlebars templates we still need to run the
// custom babel.
return this.needsInlineHBS();
}
get name(): string {
return this.packageJSON.name;
}
protected get packageJSON() {
return this.addonInstance.pkg;
}
@Memoize()
get root(): string {
// addonInstance.root gets modified by a customized "main" or
// "ember-addon.main" in package.json. We want the real package root here
// (the place where package.json lives).
return dirname(pkgUpSync(this.addonInstance.root)!);
}
@Memoize()
private get mainModule() {
// eslint-disable-next-line @typescript-eslint/no-require-imports
const mod = require(this.addonInstance.constructor._meta_.modulePath);
if (typeof mod === 'function') {
return mod.prototype;
} else {
return mod;
}
}
protected get options() {
if (!this.addonInstance.options) {
this.addonInstance.options = {};
return this.addonInstance.options;
}
// some addons (like ember-cli-inline-content) assign the *app's* options
// onto their own this.options. Which means they (accidentally or on
// purpose), always get the app's babel config, and it means when we try
// to modify the addon's babel config we're accidentally modifying the
// app's too.
//
// So here we do copying to ensure that we can modify the babel config
// without altering anybody else. We're not doing cloneDeep because that
// pulls on our lazy MacrosConfig if it appears in any babel configs here,
// whereas we want to leave it unevaluated until babel actually uses it.
let addonOptions =
typeof this.addonInstance.options == 'function' ? this.addonInstance.options() : this.addonInstance.options;
let options = Object.assign({}, addonOptions);
if (options.babel) {
options.babel = Object.assign({}, options.babel);
if (options.babel.plugins) {
options.babel.plugins = options.babel.plugins.slice();
}
}
if (options['ember-cli-babel']) {
options['ember-cli-babel'] = Object.assign({}, options['ember-cli-babel']);
}
if (typeof this.addonInstance.options == 'function') {
this.addonInstance.options = () => options;
} else {
this.addonInstance.options = options;
}
return options;
}
protected customizes(...treeNames: string[]) {
return Boolean(
treeNames.find(treeName => {
return (
// customized hook exists in actual code exported from their index.js
this.mainModule[treeName] ||
// addon instance doesn't match its own prototype
(this.addonInstance.__proto__ && this.addonInstance[treeName] !== this.addonInstance.__proto__[treeName])
);
})
);
}
@Memoize()
private hasStockTree(treeName: string) {
// we need to use this.addonInstance.root instead of this.root here because
// we're looking for the classic location of the stock tree, and that
// location is influenced by a customized ember-addon.main in package.json,
// which is reflected in addonInstance.root.
return (
this.addonInstance.treePaths && existsSync(join(this.addonInstance.root, this.addonInstance.treePaths[treeName]))
);
}
hasAnyTrees(): boolean {
return Boolean(stockTreeNames.find(name => this.hasStockTree(name))) || this.customizes(...dynamicTreeHooks);
}
// we keep all these here to ensure that we always apply the same options to
// the same tree, so that our cache doesn't need to worry about varying
// options.
private stockTreeFunnelOptions(treeName: string): FunnelOptions | undefined {
switch (treeName) {
case 'addon':
return {
exclude: ['styles/**'],
};
case 'styles':
return {
destDir: '_app_styles_',
};
case 'addon-test-support':
return {
destDir: 'test-support',
};
case 'app':
return {
exclude: ['styles/**'],
destDir: appPublicationDir,
};
case 'public':
return {
destDir: `public/${this.moduleName}`,
};
case 'vendor':
return {
destDir: 'vendor',
};
}
}
protected stockTree(treeName: string) {
return this.throughTreeCache(treeName, 'stock', () => {
// adjust from the legacy "root" to our real root, because our rootTree
// uses our real root but the stock trees are defined in terms of the
// legacy root
let srcDir = relative(this.root, join(this.addonInstance.root, this.addonInstance.treePaths[treeName]));
let opts = Object.assign({ srcDir }, this.stockTreeFunnelOptions(treeName));
return new Funnel(this.rootTree, opts);
})!;
}
@Memoize()
protected get rootTree() {
if (this.packageCache.get(this.root).mayRebuild) {
return new WatchedDir(this.root);
} else {
return new UnwatchedDir(this.root);
}
}
@Memoize()
private get moduleName(): string {
if (typeof this.addonInstance.moduleName === 'function') {
return this.addonInstance.moduleName();
}
return this.addonInstance.name;
}
// applies preprocessors to JS and HBS
private transpile(tree: Tree) {
tree = this.addonInstance.preprocessJs(tree, '/', this.moduleName, {
registry: this.addonInstance.registry,
});
if (this.addonInstance.shouldCompileTemplates() && this.addonInstance.registry.load('template').length > 0) {
tree = this.app.preprocessRegistry.preprocessTemplates(tree, {
registry: this.addonInstance.registry,
});
}
return tree;
}
@Memoize()
protected updateBabelConfig() {
let packageOptions = this.options;
let emberCLIBabelInstance = this.addonInstance.addons.find((a: any) => a.name === 'ember-cli-babel');
let version;
if (emberCLIBabelInstance) {
version = emberCLIBabelInstance.pkg.version;
}
if (!packageOptions['ember-cli-babel']) {
packageOptions['ember-cli-babel'] = {};
}
if (!packageOptions.babel) {
packageOptions.babel = {};
}
let babelConfig = packageOptions.babel as TransformOptions;
Object.assign(packageOptions['ember-cli-babel'], {
compileModules: false,
disablePresetEnv: true,
disableDebugTooling: true,
disableEmberModulesAPIPolyfill: true,
});
if (version && semver.satisfies(semver.coerce(version) || version, '^5')) {
unsupported(`${this.name} is using babel 5. Not installing our custom plugin.`);
return;
}
if (!babelConfig.plugins) {
babelConfig.plugins = [];
} else {
let hadAutoImport = Boolean(babelConfig.plugins.find(isEmberAutoImportDynamic));
babelConfig.plugins = babelConfig.plugins.filter(babelPluginAllowedInStage1);
if (hadAutoImport) {
// if we removed ember-auto-import's dynamic import() plugin, the code
// may use import() syntax and we need to re-add it to the parser.
if (version && semver.satisfies(semver.coerce(version) || version, '^6')) {
babelConfig.plugins.push(require.resolve('babel-plugin-syntax-dynamic-import'));
} else {
babelConfig.plugins.push(require.resolve('@babel/plugin-syntax-dynamic-import'));
}
}
}
if (this.templateCompiler) {
babelConfig.plugins.push(this.templateCompiler.inlineTransformsBabelPlugin());
}
}
get v2Tree(): Tree {
return this.throughTreeCache('addon', 'v2Tree', () => mergeTrees(this.v2Trees, { overwrite: true }));
}
// this is split out so that compatibility shims can override it to add more
// things to the package metadata.
protected get packageMeta(): Partial<AddonMeta> {
let built = this.build();
return mergeWithAppend(built.staticMeta, ...built.dynamicMeta.map(d => d()));
}
@Memoize()
protected get v2Trees() {
let { trees } = this.build();
return trees;
}
protected throughTreeCache(name: string, category: string, fn: () => Tree): Tree;
protected throughTreeCache(name: string, category: string, fn: () => Tree | undefined): Tree | undefined {
let cacheKey;
if (typeof this.addonInstance.cacheKeyForTree === 'function') {
cacheKey = this.addonInstance.cacheKeyForTree(name);
if (cacheKey) {
cacheKey = cacheKey + category;
let cachedTree = this.app.addonTreeCache.get(cacheKey);
if (cachedTree) {
debug('cache hit %s %s %s', this.name, name, category);
return cachedTree;
}
}
}
debug('cache miss %s %s %s', this.name, name, category);
let tree = fn();
if (tree && cacheKey) {
this.app.addonTreeCache.set(cacheKey, tree);
}
return tree;
}
protected invokeOriginalTreeFor(
name: string,
{ neuterPreprocessors } = { neuterPreprocessors: false }
): Tree | undefined {
return this.throughTreeCache(name, 'original', () => {
let original;
try {
if (neuterPreprocessors) {
original = this.addonInstance.preprocessJs;
this.addonInstance.preprocessJs = function(tree: Tree) {
return tree;
};
}
return this.addonInstance._treeFor(name);
} finally {
if (neuterPreprocessors) {
this.addonInstance.preprocessJs = original;
}
}
});
}
protected treeForAddon(built: IntermediateBuild): Tree | undefined {
// the extra isEngine condition is because ember-engines injects a
// treeForAddon method into each engine addon that we really don't need or
// want to run. Unfortunately there's not a more localized place to patch it
// out, partly because ember-engines also uses a bogus inheritance strategy
// (instead of providing a prototype that engine addons can extend it
// patches things into their instance directly).
if (this.customizes('treeForAddon', 'treeForAddonTemplates') && !this.isEngine()) {
let tree = this.invokeOriginalTreeFor('addon', { neuterPreprocessors: true });
if (tree) {
tree = modulesCompat(tree);
// this captures addons that are trying to escape their own package's
// namespace
let result = rewriteAddonTree(this.transpile(tree), this.name, this.moduleName);
built.dynamicMeta.push(result.getMeta);
return result.tree;
}
} else if (this.hasStockTree('addon')) {
return this.transpile(this.stockTree('addon'));
}
}
protected addonStylesTree(): Tree | undefined {
if (this.customizes('treeForAddonStyles')) {
return this.invokeOriginalTreeFor('addon-styles');
} else if (this.hasStockTree('addon-styles')) {
return this.addonInstance.compileStyles(this.stockTree('addon-styles'));
}
}
protected treeForTestSupport(): Tree | undefined {
if (this.customizes('treeForTestSupport')) {
todo(`${this.name} has customized the test support tree`);
} else if (this.hasStockTree('test-support')) {
// this one doesn't go through transpile yet because it gets handled as
// part of the consuming app. For example, imports should be relative to
// the consuming app, not our own package.
return new Funnel(this.stockTree('test-support'), {
destDir: `${appPublicationDir}/tests`,
});
}
}
private buildTreeForAddon(built: IntermediateBuild) {
let tree = this.treeForAddon(built);
if (!tree) {
return;
}
let templateOnlyComponents: Tree = new SynthesizeTemplateOnlyComponents(tree, ['components']);
if (!this.addonOptions.staticAddonTrees) {
let filenames: string[] = [];
let templateOnlyComponentNames: string[] = [];
tree = new ObserveTree(tree, outputDir => {
filenames = walkSync(outputDir, { globs: ['**/*.js', '**/*.hbs'] })
.map(f => `./${f.replace(/\.js$/i, '')}`)
.filter(notColocatedTemplate);
});
templateOnlyComponents = new ObserveTree(templateOnlyComponents, outputDir => {
templateOnlyComponentNames = walkSync(outputDir, { globs: ['**/*.js'] }).map(
f => `./${f.replace(/\.js$/i, '')}`
);
});
built.dynamicMeta.push(() => ({
'implicit-modules': filenames.concat(templateOnlyComponentNames),
}));
}
built.trees.push(tree);
built.trees.push(templateOnlyComponents);
}
private buildAddonStyles(built: IntermediateBuild) {
let addonStylesTree = this.addonStylesTree();
if (addonStylesTree) {
let discoveredFiles: string[] = [];
let tree = new ObserveTree(addonStylesTree, outputPath => {
discoveredFiles = walkSync(outputPath, { globs: ['**/*.css'], directories: false });
});
built.trees.push(tree);
built.dynamicMeta.push(() => {
return {
'implicit-styles': discoveredFiles.map(f => `./${f}`),
};
});
}
}
private buildTreeForStyles(built: IntermediateBuild) {
let tree;
if (this.customizes('treeForStyles')) {
// the user's tree returns their own styles with no "app/styles" wrapping
// around, which is actually what we want
tree = this.invokeOriginalTreeFor('styles');
if (tree) {
tree = new Funnel(tree, {
destDir: '_app_styles_',
getDestinationPath(path) {
return path.replace(/^app\/styles\//, '');
},
});
}
} else if (this.hasStockTree('styles')) {
tree = this.stockTree('styles');
}
if (tree) {
built.trees.push(tree);
}
}
private buildAddonTestSupport(built: IntermediateBuild) {
let addonTestSupportTree;
if (this.customizes('treeForAddonTestSupport')) {
let original = this.invokeOriginalTreeFor('addon-test-support', { neuterPreprocessors: true });
if (original) {
let { tree, getMeta } = rewriteAddonTree(original, this.name, this.moduleName);
addonTestSupportTree = this.transpile(tree);
built.dynamicMeta.push(getMeta);
}
} else if (this.hasStockTree('addon-test-support')) {
addonTestSupportTree = this.transpile(this.stockTree('addon-test-support'));
}
if (addonTestSupportTree) {
if (!this.addonOptions.staticAddonTestSupportTrees) {
let filenames: string[] = [];
addonTestSupportTree = new ObserveTree(addonTestSupportTree, outputPath => {
filenames = walkSync(outputPath, { globs: ['**/*.js', '**/*.hbs'] }).map(f => `./${f.replace(/.js$/i, '')}`);
});
built.dynamicMeta.push(() => ({
'implicit-test-modules': filenames,
}));
}
built.trees.push(addonTestSupportTree);
}
}
private maybeSetDirectoryMeta(built: IntermediateBuild, tree: Tree, localDir: string, key: keyof AddonMeta): Tree {
// unforunately Funnel doesn't create destDir if its input exists but is
// empty. And we want to only put the app-js key in package.json if
// there's really a directory for it to point to. So we need to monitor
// the output and use dynamicMeta.
let dirExists = false;
built.dynamicMeta.push(() => {
if (dirExists) {
return { [key]: localDir };
} else {
return {};
}
});
return new ObserveTree(tree, (outputPath: string) => {
dirExists = pathExistsSync(join(outputPath, localDir));
});
}
private buildTestSupport(built: IntermediateBuild) {
let tree = this.treeForTestSupport();
if (tree) {
tree = this.maybeSetDirectoryMeta(built, tree, appPublicationDir, 'app-js');
built.trees.push(tree);
}
}
private buildTreeForApp(built: IntermediateBuild) {
let appTree;
if (this.customizes('treeForApp', 'treeForTemplates')) {
let original = this.invokeOriginalTreeFor('app');
if (original) {
appTree = new Funnel(original, {
destDir: appPublicationDir,
});
}
} else if (this.hasStockTree('app')) {
appTree = this.stockTree('app');
}
if (appTree) {
// this one doesn't go through transpile yet because it gets handled as
// part of the consuming app.
appTree = this.maybeSetDirectoryMeta(built, appTree, appPublicationDir, 'app-js');
built.trees.push(appTree);
}
if (
typeof this.addonInstance.isDevelopingAddon === 'function' &&
this.addonInstance.isDevelopingAddon() &&
this.addonInstance.hintingEnabled()
) {
let hintTree = this.addonInstance.jshintAddonTree();
if (hintTree) {
hintTree = this.maybeSetDirectoryMeta(
built,
new Funnel(hintTree, { destDir: appPublicationDir }),
appPublicationDir,
'app-js'
);
built.trees.push(hintTree);
}
}
}
private buildTreeForFastBoot(built: IntermediateBuild) {
let tree;
if (this.customizes('treeForFastBoot')) {
// Arguably, it would be more correct to always create the new Funnel,
// because the fastboot directory could be created *after* our build starts.
// But that would result in hundreds of additional trees, even though the
// vast majority of addons aren't changing and don't have fastboot
// directories. So I'm pretty comfortable with the optimization. It means
// that an addon author who creates a brand new fastboot directory in a v1
// packages will need to restart their build. (Really we hope new addons
// will be authored in v2 instead soon anyway, and they won't need the
// concept of "fastboot directory" because they can use the macro system to
// conditionally import some things only in fastboot.)
if (pathExistsSync(join(this.root, 'fastboot'))) {
tree = new Funnel(this.rootTree, { srcDir: 'fastboot' });
}
tree = this.addonInstance.treeForFastBoot(tree);
if (tree) {
tree = new Funnel(tree, { destDir: fastbootPublicationDir });
}
} else {
if (pathExistsSync(join(this.root, 'fastboot'))) {
tree = new Funnel(this.rootTree, { srcDir: 'fastboot', destDir: fastbootPublicationDir });
}
}
if (tree) {
// this one doesn't go through transpile yet because it gets handled as
// part of the consuming app.
tree = this.maybeSetDirectoryMeta(built, tree, fastbootPublicationDir, 'fastboot-js');
built.trees.push(tree);
}
}
private buildPublicTree(built: IntermediateBuild) {
let publicTree;
// the extra isEngine condition is here because ember-engines injects a
// customized treeForPublic into every engine addon. We don't want or need
// it to run.
if (this.customizes('treeForPublic') && !this.isEngine()) {
let original = this.invokeOriginalTreeFor('public');
if (original) {
publicTree = new Funnel(original, {
destDir: 'public',
});
}
} else if (this.hasStockTree('public')) {
publicTree = this.stockTree('public');
}
if (publicTree) {
let publicAssets: { [filename: string]: string } = {};
publicTree = new ObserveTree(publicTree, (outputPath: string) => {
publicAssets = {};
for (let filename of walkSync(join(outputPath, 'public'))) {
if (!filename.endsWith('/')) {
publicAssets[`public/${filename}`] = filename;
}
}
});
built.trees.push(publicTree);
built.dynamicMeta.push(() => ({ 'public-assets': publicAssets }));
}
}
private buildVendorTree(built: IntermediateBuild) {
if (this.customizes('treeForVendor')) {
// We don't have any particular opinions about the structure inside
// vendor, so even when it's customized we can just use the customized
// one.
let tree = this.invokeOriginalTreeFor('vendor');
if (tree) {
built.trees.push(
new Funnel(tree, {
destDir: 'vendor',
})
);
}
} else if (this.hasStockTree('vendor')) {
built.trees.push(this.stockTree('vendor'));
}
}
private isEngine() {
return typeof this.addonInstance.getEngineConfigContents === 'function';
}
private buildEngineConfig(built: IntermediateBuild) {
if (!this.isEngine()) {
return;
}
// this addon is an engine, so it needs its own config/environment.js.
// ember-engines always emits a separate inline (not-meta-tag) config for
// fastboot, so we mimic that behavior here.
//
// getEngineConfigContents is an arbitrary customizable module, so we can't
// easily rewrite it to live inside our conditional, so it's safer in a
// separate module.
built.trees.push(writeFile('config/_environment_browser_.js', this.addonInstance.getEngineConfigContents()));
built.trees.push(
writeFile(
'config/environment.js',
`
import { macroCondition, getGlobalConfig, importSync } from '@embroider/macros';
let config;
if (macroCondition(getGlobalConfig().fastboot?.isRunning)){
config = ${JSON.stringify(this.addonInstance.engineConfig(this.app.env, {}), null, 2)};
} else {
config = importSync('./_environment_browser_.js').default;
}
export default config;
`
)
);
}
private buildPackageJSON(built: IntermediateBuild) {
let packageJSONRewriter = new RewritePackageJSON(
this.rootTree,
() => this.packageMeta,
this.packageCache.get(this.root).packageJSON
);
built.trees.push(packageJSONRewriter);
}
@Memoize()
private build(): IntermediateBuild {
let built = new IntermediateBuild();
built.staticMeta['order-index'] = this.orderIdx;
if (this.options.lazyLoading && this.options.lazyLoading.enabled) {
built.staticMeta['lazy-engine'] = true;
}
if (this.moduleName !== this.name) {
built.staticMeta['renamed-packages'] = {
[this.moduleName]: this.name,
};
}
if (this.customizes('treeFor')) {
unsupported(`${this.name} has customized treeFor`);
}
this.buildTreeForAddon(built);
this.buildAddonStyles(built);
this.buildTreeForStyles(built);
this.buildAddonTestSupport(built);
this.buildTestSupport(built);
this.buildTreeForApp(built);
this.buildTreeForFastBoot(built);
this.buildPublicTree(built);
this.buildVendorTree(built);
this.buildEngineConfig(built);
this.buildPackageJSON(built);
return built;
}
}
export interface V1AddonConstructor {
new (
addonInstance: any,
options: Required<Options>,
app: V1App,
packageCache: PackageCache,
orderIdx: number
): V1Addon;
}
class IntermediateBuild {
trees: Tree[] = [];
staticMeta: { [metaField: string]: any } = {};
dynamicMeta: (() => Partial<AddonMeta>)[] = [];
}
function babelPluginAllowedInStage1(plugin: PluginItem) {
if (isEmbroiderMacrosPlugin(plugin)) {
// the point of @embroider/macros is that it's allowed to stay in v2
// addon publication format, so it doesn't need to run here in stage1.
// We always run it in stage3.
return false;
}
if (TemplateCompiler.isInlinePrecompilePlugin(plugin)) {
// Similarly, the inline precompile plugin must not run in stage1. We
// want all templates uncompiled. Instead, we will be adding our own
// plugin that only runs custom AST transforms inside inline
// templates.
return false;
}
if (isEmberAutoImportDynamic(plugin)) {
// We replace ember-auto-import's implementation of dynamic import(), so we
// need to stop its plugin from rewriting those.
return false;
}
return true;
}
function notColocatedTemplate(path: string) {
return !/^\.\/components\/.*\.hbs$/.test(path);
}