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

Allow compat adapter's to expose shouldApplyAdapter #772

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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/compat/src/v1-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ export interface V1AddonConstructor {
packageCache: PackageCache,
orderIdx: number
): V1Addon;

shouldApplyAdapter?(addonInstance: any): boolean;
}

class IntermediateBuild {
Expand Down
33 changes: 24 additions & 9 deletions packages/compat/src/v1-instance-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,38 @@ export default class V1InstanceCache {
});
}

private adapterClass(packageName: string): V1AddonConstructor {
private adapterClass(addonInstance: any): V1AddonConstructor {
let packageName = addonInstance.pkg.name;
// if the user registered something (including "null", which allows
// disabling the built-in adapters), that takes precedence.
if (this.options.compatAdapters.has(packageName)) {
return this.options.compatAdapters.get(packageName) || V1Addon;
let AdapterClass = this.options.compatAdapters.get(packageName);

if (AdapterClass === null) {
return V1Addon;
}

if (!AdapterClass) {
let path = `${__dirname}/compat-adapters/${packageName}.js`;
if (pathExistsSync(path)) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
AdapterClass = require(path).default;
}
}
let path = `${__dirname}/compat-adapters/${packageName}.js`;
if (pathExistsSync(path)) {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(path).default;

if (!AdapterClass) {
return V1Addon;
}
return V1Addon;

if (AdapterClass.shouldApplyAdapter) {
return AdapterClass.shouldApplyAdapter(addonInstance) ? AdapterClass : V1Addon;
}

return AdapterClass;
}

private addAddon(addonInstance: any) {
this.orderIdx += 1;
let Klass = this.adapterClass(addonInstance.pkg.name);
let Klass = this.adapterClass(addonInstance);
let v1Addon = new Klass(addonInstance, this.options, this.app, this.packageCache, this.orderIdx);
let pkgs = getOrCreate(this.addons, v1Addon.root, () => []);
pkgs.push(v1Addon);
Expand Down