Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[addon-shim]: Narrowed down broccoli trees for optimized file watching #1901

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/addon-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
"dependencies": {
"@embroider/shared-internals": "workspace:^",
"broccoli-funnel": "^3.0.8",
"common-ancestor-path": "^1.0.1",
"semver": "^7.3.8"
},
"devDependencies": {
"@types/common-ancestor-path": "^1.0.2",
"@types/semver": "^7.3.6",
"broccoli-node-api": "^1.7.0",
"typescript": "^5.1.6",
Expand Down
89 changes: 45 additions & 44 deletions packages/addon-shim/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { resolve, relative, isAbsolute } from 'path';
import { readFileSync } from 'fs';
import {
AddonMeta,
AddonInstance,
isDeepAddonInstance,
AddonMeta,
PackageInfo,
isDeepAddonInstance,
} from '@embroider/shared-internals';
import buildFunnel from 'broccoli-funnel';
import type { Node } from 'broccoli-node-api';
import commonAncestorPath from 'common-ancestor-path';
import { readFileSync } from 'fs';
import { dirname, isAbsolute, join, normalize, relative, resolve } from 'path';
import { satisfies } from 'semver';

export interface ShimOptions {
Expand All @@ -29,15 +29,46 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {

let meta = addonMeta(pkg);
let disabled = false;
const rootTrees = new WeakMap<AddonInstance, Node>();

function rootTree(addonInstance: AddonInstance): Node {
let tree = rootTrees.get(addonInstance);
if (!tree) {
tree = addonInstance.treeGenerator(directory);
rootTrees.set(addonInstance, tree);
function treeFor(
addonInstance: AddonInstance,
resourceMap: Record<string, string>,
// default expectation is for resourceMap to map from interior to exterior, swap if needed
swapInteriorExterior = false
) {
const absoluteInteriorPaths = Object[
swapInteriorExterior ? 'values' : 'keys'
](resourceMap).map((internalPath) => join(directory, internalPath));

if (absoluteInteriorPaths.length === 0) {
return;
}
return tree;

const ancestorPath =
commonAncestorPath(...absoluteInteriorPaths.map(dirname)) ?? directory;
const ancestorPathRel = relative(directory, ancestorPath);
const ancestorTree = addonInstance.treeGenerator(ancestorPath);
const relativeInteriorPaths = absoluteInteriorPaths.map((absPath) =>
relative(ancestorPath, absPath)
);

return buildFunnel(ancestorTree, {
files: relativeInteriorPaths,
getDestinationPath(relativePath: string): string {
for (let [a, b] of Object.entries(resourceMap)) {
const interiorName = swapInteriorExterior ? b : a;
const exteriorName = swapInteriorExterior ? a : b;
if (join(ancestorPathRel, relativePath) === normalize(interiorName)) {
return exteriorName;
}
}
throw new Error(
`bug in addonV1Shim, no match for ${relativePath} in ${JSON.stringify(
resourceMap
)}`
);
},
});
}

return {
Expand Down Expand Up @@ -72,22 +103,7 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {
}
let maybeAppJS = meta['app-js'];
if (maybeAppJS) {
const appJS = maybeAppJS;
return buildFunnel(rootTree(this), {
files: Object.values(appJS),
getDestinationPath(relativePath: string): string {
for (let [exteriorName, interiorName] of Object.entries(appJS)) {
if (relativePath === interiorName) {
return exteriorName;
}
}
throw new Error(
`bug in addonV1Shim, no match for ${relativePath} in ${JSON.stringify(
appJS
)}`
);
},
});
return treeFor(this, maybeAppJS, true);
}
},

Expand All @@ -104,22 +120,7 @@ export function addonV1Shim(directory: string, options: ShimOptions = {}) {
}
let maybeAssets = meta['public-assets'];
if (maybeAssets) {
const assets = maybeAssets;
return buildFunnel(rootTree(this), {
files: Object.keys(assets),
getDestinationPath(relativePath: string): string {
for (let [interiorName, exteriorName] of Object.entries(assets)) {
if (relativePath === interiorName) {
return exteriorName;
}
}
throw new Error(
`bug in addonV1Shim, no match for ${relativePath} in ${JSON.stringify(
assets
)}`
);
},
});
return treeFor(this, maybeAssets);
}
},

Expand Down
Loading
Loading