Skip to content

Commit

Permalink
Merge pull request #1482 from embroider-build/cleanups
Browse files Browse the repository at this point in the history
Cleanup & refactor after 1435
  • Loading branch information
ef4 committed Jun 28, 2023
2 parents dff78b0 + ffc4db0 commit 7d1b7f0
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 753 deletions.
216 changes: 103 additions & 113 deletions packages/compat/src/compat-app-builder.ts

Large diffs are not rendered by default.

32 changes: 23 additions & 9 deletions packages/compat/src/sync-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,50 @@ import { resolve } from 'path';
import { copySync, mkdirpSync, removeSync, rmdirSync, unlinkSync } from 'fs-extra';

// mirrors the changes in the src dir to the dest dir, while tracking the
// current set of files present.
// current set of files present. If dest is undefined, it only tracks the set of
// files without mirroring the changes to anywhere
export class SyncDir {
private prev: FSTree = new FSTree();
readonly files: Set<string> = new Set();

constructor(private src: string, private dest: string) {}
constructor(private src: string, private dest: string | undefined) {}

update(): void {
let next = new FSTree({
entries: walkSync.entries(this.src),
});
for (let [operation, relativePath] of this.prev.calculatePatch(next)) {
let outputPath = resolve(this.dest, relativePath);
let outputPath: string | undefined;
if (this.dest) {
outputPath = resolve(this.dest, relativePath);
}
switch (operation) {
case 'unlink':
unlinkSync(outputPath);
if (outputPath) {
unlinkSync(outputPath);
}
this.files.delete(relativePath);
break;
case 'rmdir':
rmdirSync(outputPath);
if (outputPath) {
rmdirSync(outputPath);
}
break;
case 'mkdir':
mkdirpSync(outputPath);
if (outputPath) {
mkdirpSync(outputPath);
}
break;
case 'change':
removeSync(outputPath);
copySync(resolve(this.src, relativePath), outputPath, { dereference: true });
if (outputPath) {
removeSync(outputPath);
copySync(resolve(this.src, relativePath), outputPath, { dereference: true });
}
break;
case 'create':
copySync(resolve(this.src, relativePath), outputPath, { dereference: true });
if (outputPath) {
copySync(resolve(this.src, relativePath), outputPath, { dereference: true });
}
this.files.add(relativePath);
break;
default:
Expand Down
197 changes: 0 additions & 197 deletions packages/core/src/app-differ.ts

This file was deleted.

79 changes: 64 additions & 15 deletions packages/core/src/app-files.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { sep } from 'path';
import { Package, AddonPackage } from '@embroider/shared-internals';
import type AppDiffer from './app-differ';

export interface RouteFiles {
route?: string;
Expand All @@ -17,15 +16,55 @@ export class AppFiles {
private perRoute: RouteFiles;
readonly otherAppFiles: ReadonlyArray<string>;
readonly isFastbootOnly: Map<string, boolean>;

constructor(appDiffer: AppDiffer, resolvableExtensions: RegExp, podModulePrefix?: string) {
readonly fastbootFiles: { [appName: string]: { localFilename: string; shadowedFilename: string | undefined } };

constructor(
readonly engine: Engine,
appFiles: Set<string>,
fastbootFiles: Set<string>,
resolvableExtensions: RegExp,
podModulePrefix?: string
) {
let tests: string[] = [];
let components: string[] = [];
let helpers: string[] = [];
let modifiers: string[] = [];
let otherAppFiles: string[] = [];
this.perRoute = { children: new Map() };
for (let relativePath of appDiffer.files.keys()) {

let combinedFiles = new Set<string>();
let combinedNonFastbootFiles = new Set<string>();
let isFastbootOnly = new Map<string, boolean>();

for (let f of appFiles) {
combinedFiles.add(f);
combinedNonFastbootFiles.add(f);
}
for (let f of fastbootFiles) {
combinedFiles.add(f);
}

for (let addon of engine.addons) {
let appJS = addon.meta['app-js'];
if (appJS) {
for (let filename of Object.keys(appJS)) {
filename = filename.replace(/^\.\//, '');
combinedFiles.add(filename);
combinedNonFastbootFiles.add(filename);
}
}

let fastbootJS = addon.meta['fastboot-js'];
if (fastbootJS) {
for (let filename of Object.keys(fastbootJS)) {
filename = filename.replace(/^\.\//, '');
combinedFiles.add(filename);
}
}
}

for (let relativePath of combinedFiles) {
isFastbootOnly.set(relativePath, !combinedNonFastbootFiles.has(relativePath));
relativePath = relativePath.split(sep).join('/');
if (!resolvableExtensions.test(relativePath)) {
continue;
Expand Down Expand Up @@ -82,7 +121,25 @@ export class AppFiles {
this.helpers = helpers;
this.modifiers = modifiers;
this.otherAppFiles = otherAppFiles;
this.isFastbootOnly = appDiffer.isFastbootOnly;
this.isFastbootOnly = isFastbootOnly;

// this deliberately only describes the app's fastboot files. Not the full
// merge from all the addons. This is because they need different handling
// in the module resolver -- addon fastboot files can always be a
// fallbackResolve, because if the app happens to define the same name
// (whether fastboot-specific or just browser) that wins over the addon.
// Whereas if the app itself defines a fastbot-specific version of a file,
// that must take precedence over the *normal* resolution, and must be
// implemented in beforeResolve.
this.fastbootFiles = Object.fromEntries(
[...fastbootFiles].map(name => [
`./${name}`,
{
localFilename: `./_fastboot_/${name}`,
shadowedFilename: appFiles.has(name) ? `./${name}` : undefined,
},
])
);
}

private handleClassicRouteFile(relativePath: string): boolean {
Expand Down Expand Up @@ -142,25 +199,17 @@ export class AppFiles {
}
}

export interface EngineSummary {
export interface Engine {
// the engine's own package
package: Package;
// the set of active addons in the engine
addons: Set<AddonPackage>;
// the parent engine, if any
parent: EngineSummary | undefined;
parent: Engine | undefined;
// where the engine's own V2 code comes from
sourcePath: string;
// where the engine gets built into, combining its own code with all its
// addons
destPath: string;
// runtime name for the engine's own module namespace
modulePrefix: string;
// this is destPath but relative to the app itself
appRelativePath: string;
}

export interface Engine extends EngineSummary {
appFiles: AppFiles;
fastbootFiles: { [appName: string]: { localFilename: string; shadowedFilename: string | undefined } };
}
5 changes: 0 additions & 5 deletions packages/core/src/engine-mangler.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export { default as toBroccoliPlugin } from './to-broccoli-plugin';
export { default as WaitForTrees, OutputPaths } from './wait-for-trees';
export { compile as jsHandlebarsCompile } from './js-handlebars';
export { todo, unsupported, warn, debug, expectWarning, throwOnWarnings } from './messages';
export { mangledEngineRoot } from './engine-mangler';
export {
Resolver,
Options as ResolverOptions,
Expand Down
Loading

0 comments on commit 7d1b7f0

Please sign in to comment.