Skip to content

Commit 5f0b7cf

Browse files
fix(plugin): add feature api and prepare for jspm beta
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.
1 parent f76baf0 commit 5f0b7cf

File tree

4 files changed

+33
-20
lines changed

4 files changed

+33
-20
lines changed

src/aurelia.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,16 @@ export class Aurelia {
149149
*/
150150
globalizeResources(resources:string|string[]):Aurelia{
151151
var toAdd = Array.isArray(resources) ? resources : arguments,
152-
i, ii, resource, pluginPath = this.currentPluginId || '', path,
153-
internalPlugin = pluginPath.startsWith('./');
152+
i, ii, resource, path,
153+
resourcesRelativeTo = this.resourcesRelativeTo || '';
154154

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

161-
path = internalPlugin
162-
? relativeToFile(resource, pluginPath)
163-
: join(pluginPath, resource);
164-
161+
path = join(resourcesRelativeTo, resource);
165162
this.resourcesToLoad[path] = this.resourcesToLoad[path];
166163
}
167164

src/plugins.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ var logger = TheLogManager.getLogger('aurelia');
66

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

1111
return loader.loadModule(info.moduleId).then(m => {
1212
if('configure' in m){
1313
return Promise.resolve(m.configure(aurelia, info.config || {})).then(() => {
14-
aurelia.currentPluginId = null;
14+
aurelia.resourcesRelativeTo = null;
1515
logger.debug(`Configured plugin ${info.moduleId}.`);
1616
});
1717
}else{
18-
aurelia.currentPluginId = null;
18+
aurelia.resourcesRelativeTo = null;
1919
logger.debug(`Loaded plugin ${info.moduleId}.`);
2020
}
2121
});
@@ -38,19 +38,35 @@ export class Plugins {
3838
}
3939

4040
/**
41-
* Configures a plugin before Aurelia starts.
41+
* Configures an internal feature plugin before Aurelia starts.
42+
*
43+
* @method feature
44+
* @param {string} plugin The folder for the internal plugin to configure (expects an index.js in that folder).
45+
* @param {config} config The configuration for the specified plugin.
46+
* @return {Plugins} Returns the current Plugins instance.
47+
*/
48+
feature(plugin:string, config:any):Plugins{
49+
plugin = plugin.endsWith('.js') || plugin.endsWith('.ts') ? plugin.substring(0, plugin.length - 3) : plugin;
50+
return this.plugin({ moduleId: plugin + '/index', resourcesRelativeTo: plugin, config: config || {} });
51+
}
52+
53+
/**
54+
* Configures an external, 3rd party plugin before Aurelia starts.
4255
*
4356
* @method plugin
44-
* @param {moduleId} moduleId The ID of the module to configure.
45-
* @param {config} config The configuration for the specified module.
57+
* @param {string} plugin The ID of the 3rd party plugin to configure.
58+
* @param {config} config The configuration for the specified plugin.
4659
* @return {Plugins} Returns the current Plugins instance.
4760
*/
48-
plugin(moduleId:string, config:any):Plugins{
49-
var plugin = {moduleId:moduleId, config:config || {}};
61+
plugin(plugin:string, config:any):Plugins{
62+
if(typeof(plugin) === 'string'){
63+
plugin = plugin.endsWith('.js') || plugin.endsWith('.ts') ? plugin.substring(0, plugin.length - 3) : plugin;
64+
return this.plugin({ moduleId: plugin, resourcesRelativeTo: plugin, config: config || {} });
65+
}
5066

51-
if(this.processed){
67+
if (this.processed) {
5268
loadPlugin(this.aurelia, this.aurelia.loader, plugin);
53-
}else{
69+
} else {
5470
this.info.push(plugin);
5571
}
5672

test/aurelia.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ describe('aurelia', () => {
7474
expect('./someResource' in aurelia.resourcesToLoad).toEqual(true);
7575
});
7676

77-
it('globalizeResources will make relative to currentPluginId if set in aurelia', () => {
78-
aurelia.currentPluginId = './plugin/index';
77+
it('globalizeResources will make relative to resourcesRelativeTo if set in aurelia', () => {
78+
aurelia.resourcesRelativeTo = './plugin';
7979
expect(aurelia.globalizeResources('./someResource')).toBe(aurelia);
8080
expect('plugin/someResource' in aurelia.resourcesToLoad).toEqual(true);
8181
});

test/plugin.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ describe('the plugin loader', () => {
110110
plugins._process();
111111
expect(plugins.processed).toBeTruthy();
112112
plugins.plugin("plugin");
113-
expect(aureliaMock.currentPluginId).toBe("plugin");
113+
expect(aureliaMock.resourcesRelativeTo).toBe("plugin");
114114
//There is no promise to hook onto here so the best option is to do an instant timeout
115115
setTimeout(() => {
116116
expect(aureliaMock.loader.loadModule).toHaveBeenCalledWith("plugin");
117-
expect(aureliaMock.currentPluginId).toBeNull();
117+
expect(aureliaMock.resourcesRelativeTo).toBeNull();
118118
done();
119119
});
120120
});

0 commit comments

Comments
 (0)