Skip to content

Commit

Permalink
Implement MU sources and namespaces with expandLocalLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Mar 5, 2018
1 parent ecc4748 commit 338680a
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 200 deletions.
4 changes: 2 additions & 2 deletions mu-trees/addon/resolvers/fallback/index.js
Expand Up @@ -9,8 +9,8 @@ export default Resolver.extend({
namespace: { modulePrefix: this.config.app.name }
}, options));
},
resolve(name, referrer, targetNamespace) {
let result = this._super(name, referrer, targetNamespace);
resolve(name) {
let result = this._super(name);
return result || this._fallback.resolve(this._fallback.normalize(name));
}
});
160 changes: 99 additions & 61 deletions mu-trees/addon/resolvers/glimmer-wrapper/index.js
Expand Up @@ -10,6 +10,60 @@ function slasherize(dotted) {

const TEMPLATE_TO_PARTIAL = /^template:(.*\/)?_([\w-]+)/;

function isAbsoluteSpecifier(specifier) {
return specifier.indexOf(':/') !== -1;
}

function cleanupEmberSpecifier(specifier, source, _namespace) {
let [type, name] = specifier.split(':');
if (!name) {
return [specifier, null];
}

if (type === 'component' && name) {
specifier = `${type}:${name}`;
} else if (type === 'service') {
/* Services may be camelCased */
specifier = `service:${dasherize(name)}`;
} else if (type === 'route') {
/* Routes may have.dot.paths */
specifier = `route:${slasherize(name)}`;
} else if (type === 'controller') {
/* Controllers may have.dot.paths */
specifier = `controller:${slasherize(name)}`;
} else if (type === 'template') {
if (name && name.indexOf('components/') === 0) {
let sliced = name.slice(11);
specifier = `template:${sliced}`;
} else {
/*
* Ember partials are looked up as templates. Here we replace the template
* resolution with a partial resolute when appropriate. Try to keep this
* code as "pay-go" as possible.
*/
let match = TEMPLATE_TO_PARTIAL.exec(specifier);
if (match) {
let namespace = match[1] || '';
let name = match[2];

specifier = `partial:${namespace}${name}`;
} else {
if (source) {
throw new Error(`Cannot look up a route template ${specifier} with a source`);
}
/*
* Templates for routes must be looked up with a source. They may
* have dots.in.paths
*/
specifier = `template`;
source = `route:/${_namespace}/routes/${slasherize(name)}`;
}
}
}

return [specifier, source];
}

/*
* Wrap the @glimmer/resolver in Ember's resolver API. Although
* this code extends from the DefaultResolver, it should never
Expand All @@ -30,74 +84,58 @@ const Resolver = DefaultResolver.extend({

normalize: null,

resolve(lookupString, referrer, targetNamespace) {
let rootName = targetNamespace ||this._configRootName;

let [type, name] = lookupString.split(':');

/*
* Ember components require their lookupString to be massaged. Make this
* as "pay-go" as possible.
*/
if (referrer) {
// make absolute
let parts = referrer.split(':src/ui/');
referrer = `${parts[0]}:/${rootName}/${parts[1]}`;
referrer = referrer.split('/template.hbs')[0];
} else if (targetNamespace) {
// This is only required because:
// https://github.com/glimmerjs/glimmer-di/issues/45
referrer = `${type}:/${rootName}/`;
expandLocalLookup(specifier, source, namespace) {
if (isAbsoluteSpecifier(specifier)) {
return specifier; // specifier is absolute
}

if (name) {
if (type === 'component' && name) {
lookupString = `${type}:${name}`;
} else if (type === 'service') {
/* Services may be camelCased */
lookupString = `service:${dasherize(name)}`;
} else if (type === 'route') {
/* Routes may have.dot.paths */
lookupString = `route:${slasherize(name)}`;
} else if (type === 'controller') {
/* Controllers may have.dot.paths */
lookupString = `controller:${slasherize(name)}`;
} else if (type === 'template') {
if (name && name.indexOf('components/') === 0) {
let sliced = name.slice(11);
lookupString = `template:${sliced}`;
} else {
/*
* Ember partials are looked up as templates. Here we replace the template
* resolution with a partial resolute when appropriate. Try to keep this
* code as "pay-go" as possible.
*/
let match = TEMPLATE_TO_PARTIAL.exec(lookupString);
if (match) {
let namespace = match[1] || '';
let name = match[2];

lookupString = `partial:${namespace}${name}`;
} else {
if (referrer) {
throw new Error(`Cannot look up a route template ${lookupString} with a referrer`);
}
/*
* Templates for routes must be looked up with a referrer. They may
* have dots.in.paths
*/
lookupString = `template`;
referrer = `route:/${rootName}/routes/${slasherize(name)}`;
}
}
if (source || namespace) {
let rootName = namespace || this._configRootName;

let [type, name] = specifier.split(':');

/*
* Ember components require their lookupString to be massaged. Make this
* as "pay-go" as possible.
*/
if (namespace) {
// This is only required because:
// https://github.com/glimmerjs/glimmer-di/issues/45
source = `${type}:/${rootName}/`;
} else if (source) {
// make absolute
let parts = source.split(':src/ui/');
source = `${parts[0]}:/${rootName}/${parts[1]}`;
source = source.split('/template.hbs')[0];
}

let [_specifier, _source] = cleanupEmberSpecifier(specifier, source, rootName);

let absoluteSpecifier = this._glimmerResolver.identify(_specifier, _source);

if (absoluteSpecifier) {
return absoluteSpecifier;
}

absoluteSpecifier = this._glimmerResolver.identify(_specifier);

if (absoluteSpecifier) {
return specifier;
}
}

return this._resolve(lookupString, referrer);
return specifier;
},

_resolve(lookupString, referrer) {
return this._glimmerResolver.resolve(lookupString, referrer);
resolve(specifier) {
let source = null;
if (!isAbsoluteSpecifier(specifier)) {
let [_specifier, _source] = cleanupEmberSpecifier(specifier, source, this._configRootName);
specifier = _specifier;
source = _source;
}

return this._glimmerResolver.resolve(specifier, source);
}

});
Expand Down

0 comments on commit 338680a

Please sign in to comment.