Skip to content

Commit

Permalink
feat(all): new loader-plugin-based remplate loader
Browse files Browse the repository at this point in the history
This is a BREAKING CHANGE. The new default loader uses plugins to
System.js and Require.js which then delegate to the html import
capability. This allows Aurelia to use the module loader’s map/path
configuration for views, providing a more correct resolution
capability. The loader now returns a TemplateRegistryEntry instead of
just the raw template. This entry contains the template, its
dependencies and its id. It is expect that a view engine will also
provide the resources and factory at a later time. All requests for the
same template should return the same entry instance.

As a result of loader-based view loading, we have been able to remove
the baseUrl and baseViewUrl properties (which were horribly hacked
anyway).

Finally, this loader will expose itself as window.AureliaLoader for use
by the bootstrapper.
  • Loading branch information
EisenbergEffect committed Mar 2, 2015
1 parent 3018266 commit ab694b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,6 @@ This library is part of the [Aurelia](http://www.aurelia.io/) platform and conta

* [aurelia-metadata](https://github.com/aurelia/metadata)
* [aurelia-loader](https://github.com/aurelia/loader)
* [aurelia-path](https://github.com/aurelia/path)

## Used By

Expand Down
1 change: 0 additions & 1 deletion config.js
Expand Up @@ -11,7 +11,6 @@ System.config({
"map": {
"aurelia-loader": "github:aurelia/loader@0.3.5",
"aurelia-metadata": "github:aurelia/metadata@0.3.3",
"aurelia-path": "github:aurelia/path@0.4.5",
"github:aurelia/loader@0.3.5": {
"aurelia-html-template-element": "github:aurelia/html-template-element@0.1.3",
"core-js": "npm:core-js@0.4.10",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -26,8 +26,7 @@
},
"dependencies": {
"aurelia-loader": "github:aurelia/loader@^0.3.5",
"aurelia-metadata": "github:aurelia/metadata@^0.3.3",
"aurelia-path": "github:aurelia/path@^0.4.5"
"aurelia-metadata": "github:aurelia/metadata@^0.3.3"
}
},
"devDependencies": {
Expand Down
77 changes: 56 additions & 21 deletions src/index.js
@@ -1,16 +1,18 @@
import {Origin} from 'aurelia-metadata';
import {Loader} from 'aurelia-loader';
import {join} from 'aurelia-path';

if(!window.System || !window.System.import){
var sys = window.System = window.System || {};

sys.polyfilled = true;
sys.map = {};

sys['import'] = function(moduleId){
return new Promise((resolve, reject) => {
require([moduleId], resolve, reject);
});
};

sys.normalize = function(url){
return Promise.resolve(url);
};
Expand All @@ -25,7 +27,7 @@ function ensureOriginOnExports(executed, name){
}

Origin.set(target, new Origin(name, 'default'));

for (key in target) {
exportedValue = target[key];

Expand All @@ -37,32 +39,63 @@ function ensureOriginOnExports(executed, name){
return executed;
}

Loader.createDefaultLoader = function(){
return new DefaultLoader();
};

export class DefaultLoader extends Loader {
constructor(){
this.baseUrl = System.baseUrl;
this.baseViewUrl = System.baseViewUrl || System.baseUrl;
this.registry = {};
}
super();

this.moduleRegistry = {};
var that = this;

loadModule(id, baseUrl){
baseUrl = baseUrl === undefined ? this.baseUrl : baseUrl;
if(System.polyfilled){
define('html-import-template', [], {
load: function (name, req, onload, config) {
var entry = that.getOrCreateTemplateRegistryEntry(name),
address;

if(baseUrl && id.indexOf(baseUrl) !== 0){
id = join(baseUrl, id);
if(entry.templateIsLoaded){
onload(entry);
return;
}

address = req.toUrl(name);

that.importTemplate(address).then(template => {
entry.setTemplate(template);
onload(entry);
});
}
});
}else{
System.set('html-import-template', System.newModule({
fetch: function(load, fetch) {
var id = load.name.substring(0, load.name.indexOf('!'));
var entry = load.metadata.templateRegistryEntry = that.getOrCreateTemplateRegistryEntry(id);

if(entry.templateIsLoaded){
return '';
}

return that.importTemplate(load.address).then(template => {
entry.setTemplate(template);
return '';
});
},
instantiate:function(load) {
return load.metadata.templateRegistryEntry;
}
}));
}

}

loadModule(id){
return System.normalize(id).then(newId => {
var existing = this.registry[newId];
var existing = this.moduleRegistry[newId];
if(existing){
return existing;
}

return System.import(newId).then(m => {
this.registry[newId] = m;
this.moduleRegistry[newId] = m;
return ensureOriginOnExports(m, newId);
});
});
Expand All @@ -79,10 +112,12 @@ export class DefaultLoader extends Loader {
}

loadTemplate(url){
if(this.baseViewUrl && url.indexOf(this.baseViewUrl) !== 0){
url = join(this.baseViewUrl, url);
if(System.polyfilled){
return System.import('html-import-template!' + url);
}else{
return System.import(url + '!html-import-template');
}

return this.importTemplate(url);
}
}

window.AureliaLoader = DefaultLoader;

0 comments on commit ab694b1

Please sign in to comment.