Skip to content

Instantly share code, notes, and snippets.

@mydea
Created November 25, 2020 07:26
Show Gist options
  • Save mydea/d890f3b83a4bf25b3c3a4f41f6a47e42 to your computer and use it in GitHub Desktop.
Save mydea/d890f3b83a4bf25b3c3a4f41f6a47e42 to your computer and use it in GitHub Desktop.
An extended strict resolver that works with pod routes/controllers/templates and nested co-located components.
import Resolver from 'ember-strict-resolver';
interface ModuleDefinition {
prefix: string;
type: string;
name: string;
}
export default class CustomStrictResolver extends Resolver {
legacyMappings: { [key: string]: string } = {
'service:userSession': 'service:user-session',
'service:powerCalendar': 'service:power-calendar',
'service:apiStatus': 'service:api-status',
// ... all other multi-word services
};
moduleNameMappings: { [key: string]: string | undefined } = {
'template:components/basic-dropdown':
'fabscale-app/templates/components/basic-dropdown',
'template:components/basic-dropdown-trigger':
'fabscale-app/templates/components/basic-dropdown-trigger',
'template:components/basic-dropdown-content':
'fabscale-app/templates/components/basic-dropdown-content',
'template:components/power-calendar':
'fabscale-app/templates/components/power-calendar',
'template:components/power-calendar/days':
'fabscale-app/templates/components/power-calendar/days',
'template:components/power-calendar-range':
'fabscale-app/templates/components/power-calendar-range',
'template:components/power-calendar-range/days':
'fabscale-app/templates/components/power-calendar-range/days',
'template:components/-dynamic-element': undefined,
};
resolve(fullName: string) {
return super.resolve(this._getName(fullName));
}
normalize(fullName: string) {
return super.normalize(this._getName(fullName));
}
_getName(fullName: string) {
return this.legacyMappings[fullName] || fullName;
}
moduleNameForFullName(fullName: string) {
// Custom mappings
if (this.moduleNameMappings.hasOwnProperty(fullName)) {
return this.moduleNameMappings[fullName];
}
let { prefix, type, name } = this.parseFullName(fullName);
name = name.replace(/\./g, '/');
// Abilities - custom pluralization...
if (type === 'ability') {
return `${prefix}/abilities/${name}`;
}
// Pods structure for routes & controllers
if (type === 'route' || type === 'controller') {
return `${prefix}/${name}/${type}`;
}
// Handle pod route templates & nested component templates (index)
if (type === 'template') {
return this._getModuleNameForTemplate(fullName, { prefix, name, type });
}
// Handle nested components (index)
if (type === 'component') {
return this._getModuleNameForComponent(fullName, { prefix, name, type });
}
return super.moduleNameForFullName(fullName);
}
_getModuleNameForTemplate(
fullName: string,
{ prefix, name }: ModuleDefinition
) {
// Pods structure for route templates
if (!name.startsWith('components/')) {
return `${prefix}/${name}/template`;
}
// Handle nested component templates (index)
// try colocation...
let moduleName = `${prefix}/${name}`;
if (this.has(moduleName)) {
return moduleName;
}
// try index
moduleName = `${prefix}/${name}/index`;
if (this.has(moduleName)) {
return moduleName;
}
return super.moduleNameForFullName(fullName);
}
_getModuleNameForComponent(
fullName: string,
{ prefix, name }: ModuleDefinition
) {
// try colocation...
let moduleName = `${prefix}/components/${name}`;
if (this.has(moduleName)) {
return moduleName;
}
// try index
moduleName = `${prefix}/components/${name}/index`;
if (this.has(moduleName)) {
return moduleName;
}
return super.moduleNameForFullName(fullName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment