diff --git a/docs/creating-plugins.md b/docs/creating-plugins.md index 47b82225..6b0e110b 100644 --- a/docs/creating-plugins.md +++ b/docs/creating-plugins.md @@ -17,7 +17,7 @@ As of SystemJS 0.20, instantiation plugins are modules with a `load` method reso plugin.js ```javascript -export function load (key) { +export function load (url) { return Promise.resolve({ some: 'value' }); @@ -35,7 +35,15 @@ Within this hook, plugins can fetch `key` as a source text, or do any other cust is provided, an empty module is used for the plugin module value. Within the `load` hook `this` is set to the loader itself. It is also possible to call `loader.register(key, deps, declare)` to instantiate the module -in the plugin load hook. +in the plugin load hook. Because plugins are referenced via syntax (`resource!plugin`) it is necessary to use the corresponding `registerKey` argument here: + +```javascript +export function load (url, registerKey) { + this.register(registerKey, deps, function (_export, _context) { + // ... System.register format ... + }); +} +``` > For SystemJS 0.19 and below, instantiation plugins must use the `instantiate` hook API form from the compiler plugins section below. @@ -45,11 +53,11 @@ A CSS loading plugin can be written: css.js: ```javascript -exports.load = function(key) { +exports.load = function (url) { return new Promise(function (resolve, reject) { var link = document.createElement('link'); link.rel = 'stylesheet'; - link.href = key; + link.href = url; link.onload = resolve; document.head.appendChild(link); diff --git a/src/instantiate.js b/src/instantiate.js index 59dcdca1..b86f2c8c 100644 --- a/src/instantiate.js +++ b/src/instantiate.js @@ -42,7 +42,7 @@ export function instantiate (key, metadata, processAnonRegister) { .then(function () { // modern plugin = load hook if (metadata.pluginModule && typeof metadata.pluginModule.load === 'function') - return runPluginLoad(loader, key, metadata, metadata.pluginKey, metadata.pluginModule); + return runPluginLoad(loader, metadata.pluginArgument, key, metadata, metadata.pluginKey, metadata.pluginModule); return runFetchPipeline(loader, key, metadata, processAnonRegister, config.wasm); }) @@ -95,10 +95,10 @@ function protectedCreateNamespace (bindings) { return new ModuleNamespace(bindings); } -function runPluginLoad (loader, key, metadata, pluginKey, pluginModule) { +function runPluginLoad (loader, key, registerKey, metadata, pluginKey, pluginModule) { return Promise.resolve() .then(function () { - return pluginModule.load.call(loader, key); + return pluginModule.load.call(loader, key, registerKey); }) .then(function (pluginResult) { if (pluginResult === undefined) @@ -501,7 +501,7 @@ function transpile (loader, source, key, metadata) { return loader.import.call(loader, loader.transpiler) .then(function (transpiler) { if (typeof transpiler.load === 'function') - return runPluginLoad(loader, key, metadata, loader.transpiler, transpiler); + return runPluginLoad(loader, key, key, metadata, loader.transpiler, transpiler); if (transpiler.__useDefault) transpiler = transpiler.default; diff --git a/test/test.js b/test/test.js index 9b4b5164..990fcffe 100644 --- a/test/test.js +++ b/test/test.js @@ -530,8 +530,8 @@ suite('SystemJS Standard Tests', function() { test('Instantiate plugin register', function () { System.set('instantiate-plugin-register', System.newModule({ - load: function (key) { - this.register(key, [], function (_export) { + load: function (key, fullKey) { + this.register(fullKey, [], function (_export) { return function () { _export('some', 'thing'); };