Skip to content

Commit

Permalink
fix(plugin): add feature api and prepare for jspm beta
Browse files Browse the repository at this point in the history
This is a BREAKING CHANGE. The plugin api should now only be used for
external 3rd party plugins. If you have a folder inside your own app
that you want to load. You now use the feature api. Simply place an
index.js in that folder and provide the folder name to the feature api.
Make sure your index.js has an exported configure function.
  • Loading branch information
EisenbergEffect committed Aug 3, 2015
1 parent f76baf0 commit 5f0b7cf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/aurelia.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,16 @@ export class Aurelia {
*/
globalizeResources(resources:string|string[]):Aurelia{
var toAdd = Array.isArray(resources) ? resources : arguments,
i, ii, resource, pluginPath = this.currentPluginId || '', path,
internalPlugin = pluginPath.startsWith('./');
i, ii, resource, path,
resourcesRelativeTo = this.resourcesRelativeTo || '';

for(i = 0, ii = toAdd.length; i < ii; ++i){
resource = toAdd[i];
if(typeof resource != 'string'){
throw new Error(`Invalid resource path [${resource}]. Resources must be specified as relative module IDs.`);
}

path = internalPlugin
? relativeToFile(resource, pluginPath)
: join(pluginPath, resource);

path = join(resourcesRelativeTo, resource);
this.resourcesToLoad[path] = this.resourcesToLoad[path];
}

Expand Down
36 changes: 26 additions & 10 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var logger = TheLogManager.getLogger('aurelia');

function loadPlugin(aurelia, loader, info){
logger.debug(`Loading plugin ${info.moduleId}.`);
aurelia.currentPluginId = (info.moduleId.endsWith('.js') || info.moduleId.endsWith('.ts')) ? info.moduleId.substring(0, info.moduleId.length - 3) : info.moduleId;
aurelia.resourcesRelativeTo = info.resourcesRelativeTo;

return loader.loadModule(info.moduleId).then(m => {
if('configure' in m){
return Promise.resolve(m.configure(aurelia, info.config || {})).then(() => {
aurelia.currentPluginId = null;
aurelia.resourcesRelativeTo = null;
logger.debug(`Configured plugin ${info.moduleId}.`);
});
}else{
aurelia.currentPluginId = null;
aurelia.resourcesRelativeTo = null;
logger.debug(`Loaded plugin ${info.moduleId}.`);
}
});
Expand All @@ -38,19 +38,35 @@ export class Plugins {
}

/**
* Configures a plugin before Aurelia starts.
* Configures an internal feature plugin before Aurelia starts.
*
* @method feature
* @param {string} plugin The folder for the internal plugin to configure (expects an index.js in that folder).
* @param {config} config The configuration for the specified plugin.
* @return {Plugins} Returns the current Plugins instance.
*/
feature(plugin:string, config:any):Plugins{
plugin = plugin.endsWith('.js') || plugin.endsWith('.ts') ? plugin.substring(0, plugin.length - 3) : plugin;
return this.plugin({ moduleId: plugin + '/index', resourcesRelativeTo: plugin, config: config || {} });
}

/**
* Configures an external, 3rd party plugin before Aurelia starts.
*
* @method plugin
* @param {moduleId} moduleId The ID of the module to configure.
* @param {config} config The configuration for the specified module.
* @param {string} plugin The ID of the 3rd party plugin to configure.
* @param {config} config The configuration for the specified plugin.
* @return {Plugins} Returns the current Plugins instance.
*/
plugin(moduleId:string, config:any):Plugins{
var plugin = {moduleId:moduleId, config:config || {}};
plugin(plugin:string, config:any):Plugins{
if(typeof(plugin) === 'string'){
plugin = plugin.endsWith('.js') || plugin.endsWith('.ts') ? plugin.substring(0, plugin.length - 3) : plugin;
return this.plugin({ moduleId: plugin, resourcesRelativeTo: plugin, config: config || {} });
}

if(this.processed){
if (this.processed) {
loadPlugin(this.aurelia, this.aurelia.loader, plugin);
}else{
} else {
this.info.push(plugin);
}

Expand Down
4 changes: 2 additions & 2 deletions test/aurelia.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('aurelia', () => {
expect('./someResource' in aurelia.resourcesToLoad).toEqual(true);
});

it('globalizeResources will make relative to currentPluginId if set in aurelia', () => {
aurelia.currentPluginId = './plugin/index';
it('globalizeResources will make relative to resourcesRelativeTo if set in aurelia', () => {
aurelia.resourcesRelativeTo = './plugin';
expect(aurelia.globalizeResources('./someResource')).toBe(aurelia);
expect('plugin/someResource' in aurelia.resourcesToLoad).toEqual(true);
});
Expand Down
4 changes: 2 additions & 2 deletions test/plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ describe('the plugin loader', () => {
plugins._process();
expect(plugins.processed).toBeTruthy();
plugins.plugin("plugin");
expect(aureliaMock.currentPluginId).toBe("plugin");
expect(aureliaMock.resourcesRelativeTo).toBe("plugin");
//There is no promise to hook onto here so the best option is to do an instant timeout
setTimeout(() => {
expect(aureliaMock.loader.loadModule).toHaveBeenCalledWith("plugin");
expect(aureliaMock.currentPluginId).toBeNull();
expect(aureliaMock.resourcesRelativeTo).toBeNull();
done();
});
});
Expand Down

0 comments on commit 5f0b7cf

Please sign in to comment.