/
compat-app.ts
360 lines (317 loc) · 11.4 KB
/
compat-app.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
import { Tree } from 'broccoli-plugin';
import mergeTrees from 'broccoli-merge-trees';
import {
Stage,
PackageCache,
OutputPaths,
BuildStage,
Asset,
EmberAsset,
AppAdapter,
AppBuilder,
EmberENV,
Package,
TemplateCompilerPlugins,
Resolver,
TemplateCompiler,
AddonPackage,
} from '@embroider/core';
import V1InstanceCache from './v1-instance-cache';
import V1App from './v1-app';
import walkSync from 'walk-sync';
import { join } from 'path';
import { JSDOM } from 'jsdom';
import { V1Config } from './v1-config';
import { statSync, readdirSync } from 'fs';
import Options, { optionsWithDefaults } from './options';
import CompatResolver from './resolver';
import { activePackageRules, PackageRules, expandModuleRules } from './dependency-rules';
import flatMap from 'lodash/flatMap';
import { Memoize } from 'typescript-memoize';
import flatten from 'lodash/flatten';
import { sync as resolveSync } from 'resolve';
interface TreeNames {
appJS: Tree;
htmlTree: Tree;
publicTree: Tree;
configTree: Tree;
}
// This runs at broccoli-pipeline-construction time, whereas our actual
// CompatAppAdapter instance only becomes available during tree-building
// time.
function setup(legacyEmberAppInstance: object, options: Required<Options>) {
let oldPackage = V1InstanceCache.forApp(legacyEmberAppInstance, options).app;
let { appJS } = oldPackage.processAppJS();
let htmlTree = oldPackage.htmlTree;
let publicTree = oldPackage.publicTree;
let configTree = oldPackage.config;
if (options.extraPublicTrees.length > 0) {
publicTree = mergeTrees([publicTree, ...options.extraPublicTrees].filter(Boolean));
}
let inTrees = {
appJS,
htmlTree,
publicTree,
configTree,
};
let instantiate = async (root: string, appSrcDir: string, packageCache: PackageCache) => {
let appPackage = packageCache.getApp(appSrcDir);
let adapter = new CompatAppAdapter(
root,
appPackage,
options,
oldPackage,
configTree,
packageCache.get(join(root, 'node_modules', '@embroider', 'synthesized-vendor')),
packageCache.get(join(root, 'node_modules', '@embroider', 'synthesized-styles'))
);
return new AppBuilder<TreeNames>(root, appPackage, adapter, options);
};
return { inTrees, instantiate };
}
class CompatAppAdapter implements AppAdapter<TreeNames> {
constructor(
private root: string,
private appPackage: Package,
private options: Required<Options>,
private oldPackage: V1App,
private configTree: V1Config,
private synthVendor: Package,
private synthStyles: Package
) {}
appJSSrcDir(treePaths: OutputPaths<TreeNames>) {
return treePaths.appJS;
}
assets(treePaths: OutputPaths<TreeNames>): Asset[] {
let assets: Asset[] = [];
// Everything in our traditional public tree is an on-disk asset
if (treePaths.publicTree) {
walkSync
.entries(treePaths.publicTree, {
directories: false,
})
.forEach(entry => {
assets.push({
kind: 'on-disk',
relativePath: entry.relativePath,
sourcePath: entry.fullPath,
mtime: (entry.mtime as unknown) as number, // https://github.com/joliss/node-walk-sync/pull/38
size: entry.size,
});
});
}
for (let asset of this.emberEntrypoints(treePaths.htmlTree)) {
assets.push(asset);
}
return assets;
}
@Memoize()
get allActiveAddons(): AddonPackage[] {
// todo: filter by addon-provided hook
let shouldInclude = (dep: Package) => dep.isEmberPackage();
let result = this.appPackage.findDescendants(shouldInclude) as AddonPackage[];
let extras = [this.synthVendor, this.synthStyles].filter(shouldInclude) as AddonPackage[];
let extraDescendants = flatMap(extras, dep => dep.findDescendants(shouldInclude)) as AddonPackage[];
result = [...result, ...extras, ...extraDescendants];
return result;
}
@Memoize()
get directActiveAddons(): AddonPackage[] {
// todo: filter by addon-provided hook
let shouldInclude = (dep: Package) => dep.isEmberPackage();
return this.appPackage.dependencies.filter(shouldInclude) as AddonPackage[];
}
@Memoize()
resolvableExtensions(): string[] {
// webpack's default is ['.wasm', '.mjs', '.js', '.json']. Keeping that
// subset in that order is sensible, since many third-party libraries will
// expect it to work that way.
let extensions = ['.wasm', '.mjs', '.js', '.json', '.hbs'];
// for now, this is hard-coded. If we see ember-cli-typescript, ts files are
// resolvable. Once we implement a preprocessor-registration build hook,
// this logic can be pushed down first into `@embroider/compat` (which can
// generate the appropriate hooks when it upcompiles ember-cli-typescript),
// and then later into ember-cli-typescript itself (which can ship a v2
// version with the hook).
//
// Typescript is a slightly weird example of a preprocessor because it gets
// implemented in babel, so all we realy need to do is make the extension
// resolvable and there's no other "loader" or anything to apply.
if (this.directActiveAddons.find(pkg => pkg.name === 'ember-cli-typescript')) {
extensions.unshift('.ts');
}
return extensions;
}
private *emberEntrypoints(htmlTreePath: string): IterableIterator<Asset> {
let classicEntrypoints = [
{ entrypoint: 'index.html', includeTests: false },
{ entrypoint: 'tests/index.html', includeTests: true },
];
if (!this.oldPackage.shouldBuildTests) {
classicEntrypoints.pop();
}
for (let { entrypoint, includeTests } of classicEntrypoints) {
let sourcePath = join(htmlTreePath, entrypoint);
let stats = statSync(sourcePath);
let asset: EmberAsset = {
kind: 'ember',
relativePath: entrypoint,
includeTests,
sourcePath,
mtime: stats.mtime.getTime(),
size: stats.size,
prepare: (dom: JSDOM) => {
let scripts = [...dom.window.document.querySelectorAll('script')];
let styles = [...dom.window.document.querySelectorAll('link[rel="stylesheet"]')] as HTMLLinkElement[];
return {
javascript: definitelyReplace(dom, this.oldPackage.findAppScript(scripts), 'app javascript', entrypoint),
styles: definitelyReplace(dom, this.oldPackage.findAppStyles(styles), 'app styles', entrypoint),
implicitScripts: definitelyReplace(
dom,
this.oldPackage.findVendorScript(scripts),
'vendor javascript',
entrypoint
),
implicitStyles: definitelyReplace(
dom,
this.oldPackage.findVendorStyles(styles),
'vendor styles',
entrypoint
),
testJavascript: maybeReplace(dom, this.oldPackage.findTestScript(scripts)),
implicitTestScripts: maybeReplace(dom, this.oldPackage.findTestSupportScript(scripts)),
implicitTestStyles: maybeReplace(dom, this.oldPackage.findTestSupportStyles(styles)),
};
},
};
yield asset;
}
}
autoRun(): boolean {
return this.oldPackage.autoRun;
}
mainModule(): string {
return this.oldPackage.isModuleUnification ? 'src/main' : 'app';
}
mainModuleConfig(): unknown {
return this.configTree.readConfig().APP;
}
emberENV(): EmberENV {
return this.configTree.readConfig().EmberENV;
}
modulePrefix(): string {
return this.configTree.readConfig().modulePrefix;
}
podModulePrefix(): string | undefined {
return this.configTree.readConfig().podModulePrefix;
}
rootURL(): string {
return this.configTree.readConfig().rootURL;
}
templateCompilerPath(): string {
return 'ember-source/vendor/ember/ember-template-compiler';
}
strictV2Format() {
return false;
}
@Memoize()
private activeRules() {
return activePackageRules(this.options.packageRules.concat(defaultAddonPackageRules()), [
this.appPackage,
...this.allActiveAddons.filter(p => p.meta['auto-upgraded']),
]);
}
@Memoize()
templateResolver(): Resolver {
return new CompatResolver({
root: this.root,
modulePrefix: this.modulePrefix(),
podModulePrefix: this.podModulePrefix(),
options: this.options,
activePackageRules: this.activeRules(),
resolvableExtensions: this.resolvableExtensions(),
});
}
// unlike `templateResolver`, this one brings its own simple TemplateCompiler
// along so it's capable of parsing component snippets in people's module
// rules.
@Memoize()
private internalTemplateResolver(): CompatResolver {
let resolver = new CompatResolver({
root: this.root,
modulePrefix: this.modulePrefix(),
options: this.options,
activePackageRules: this.activeRules(),
resolvableExtensions: this.resolvableExtensions(),
});
// It's ok that this isn't a fully configured template compiler. We're only
// using it to parse component snippets out of rules.
resolver.astTransformer(
new TemplateCompiler({
compilerPath: resolveSync(this.templateCompilerPath(), { basedir: this.root }),
EmberENV: {},
plugins: {},
})
);
return resolver;
}
extraImports() {
let output: { absPath: string; target: string; runtimeName?: string }[][] = [];
for (let rule of this.activeRules()) {
if (rule.addonModules) {
for (let [filename, moduleRules] of Object.entries(rule.addonModules)) {
for (let root of rule.roots) {
let absPath = join(root, filename);
output.push(expandModuleRules(absPath, moduleRules, this.internalTemplateResolver()));
}
}
}
if (rule.appModules) {
for (let [filename, moduleRules] of Object.entries(rule.appModules)) {
let absPath = join(this.root, filename);
output.push(expandModuleRules(absPath, moduleRules, this.internalTemplateResolver()));
}
}
}
return flatten(output);
}
htmlbarsPlugins(): TemplateCompilerPlugins {
return this.oldPackage.htmlbarsPlugins;
}
babelMajorVersion() {
return this.oldPackage.babelMajorVersion();
}
babelConfig() {
return this.oldPackage.babelConfig();
}
}
export default class CompatApp extends BuildStage<TreeNames> {
constructor(legacyEmberAppInstance: object, addons: Stage, options?: Options) {
let { inTrees, instantiate } = setup(legacyEmberAppInstance, optionsWithDefaults(options));
super(addons, inTrees, '@embroider/compat/app', instantiate);
}
}
function maybeReplace(dom: JSDOM, element: Element | undefined): Node | undefined {
if (element) {
return definitelyReplace(dom, element, '', '');
}
}
function definitelyReplace(dom: JSDOM, element: Element | undefined, description: string, file: string): Node {
if (!element) {
throw new Error(`could not find ${description} in ${file}`);
}
let placeholder = dom.window.document.createTextNode('');
element.replaceWith(placeholder);
return placeholder;
}
function defaultAddonPackageRules(): PackageRules[] {
return readdirSync(join(__dirname, 'addon-dependency-rules'))
.map(filename => {
if (filename.endsWith('.js')) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(join(__dirname, 'addon-dependency-rules', filename)).default;
}
})
.filter(Boolean)
.reduce((a, b) => a.concat(b), []);
}