From 23933f0b704afecaa4173c2ba07b5c3aa0851325 Mon Sep 17 00:00:00 2001 From: Travis Hoover Date: Thu, 22 Apr 2021 14:23:58 -0700 Subject: [PATCH 1/2] Allow compat adapter to expose isEnabled The use case is that if a compat adapter is found (either the built-in ones or custom ones) they will always be used. This method allows the compat adapters to opt out if say a newer version of a package fixes the issue. --- packages/compat/src/v1-addon.ts | 2 ++ packages/compat/src/v1-instance-cache.ts | 33 +++++++++++++++++------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/compat/src/v1-addon.ts b/packages/compat/src/v1-addon.ts index 75eae5ef8..73a53ee84 100644 --- a/packages/compat/src/v1-addon.ts +++ b/packages/compat/src/v1-addon.ts @@ -1009,6 +1009,8 @@ export interface V1AddonConstructor { packageCache: PackageCache, orderIdx: number ): V1Addon; + + isEnabled?(addonInstance: any): boolean; } class IntermediateBuild { diff --git a/packages/compat/src/v1-instance-cache.ts b/packages/compat/src/v1-instance-cache.ts index 244417867..74ba45bc2 100644 --- a/packages/compat/src/v1-instance-cache.ts +++ b/packages/compat/src/v1-instance-cache.ts @@ -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.isEnabled) { + return AdapterClass.isEnabled(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); From b4473c9838d0f03dfd4e12fd3bab8d530abc66c9 Mon Sep 17 00:00:00 2001 From: Travis Hoover Date: Mon, 26 Apr 2021 09:15:24 -0700 Subject: [PATCH 2/2] Use shouldApplyAdapter over isEnabled --- packages/compat/src/v1-addon.ts | 2 +- packages/compat/src/v1-instance-cache.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/compat/src/v1-addon.ts b/packages/compat/src/v1-addon.ts index 73a53ee84..d5bd59c10 100644 --- a/packages/compat/src/v1-addon.ts +++ b/packages/compat/src/v1-addon.ts @@ -1010,7 +1010,7 @@ export interface V1AddonConstructor { orderIdx: number ): V1Addon; - isEnabled?(addonInstance: any): boolean; + shouldApplyAdapter?(addonInstance: any): boolean; } class IntermediateBuild { diff --git a/packages/compat/src/v1-instance-cache.ts b/packages/compat/src/v1-instance-cache.ts index 74ba45bc2..170cd7a1c 100644 --- a/packages/compat/src/v1-instance-cache.ts +++ b/packages/compat/src/v1-instance-cache.ts @@ -65,8 +65,8 @@ export default class V1InstanceCache { return V1Addon; } - if (AdapterClass.isEnabled) { - return AdapterClass.isEnabled(addonInstance) ? AdapterClass : V1Addon; + if (AdapterClass.shouldApplyAdapter) { + return AdapterClass.shouldApplyAdapter(addonInstance) ? AdapterClass : V1Addon; } return AdapterClass;