From 35dcda2d4cb92f5974fd31ce2a9c28bab50e2df0 Mon Sep 17 00:00:00 2001 From: guybedford Date: Mon, 22 Jun 2015 22:39:47 +0200 Subject: [PATCH] plugin normalize hook support (#542) --- lib/plugins.js | 27 ++++++++++++++++++--------- test/test.js | 7 +++++++ test/tests/normalize-plugin.js | 6 ++++++ 3 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 test/tests/normalize-plugin.js diff --git a/lib/plugins.js b/lib/plugins.js index c4428cb0..db6d32ed 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -37,15 +37,24 @@ return argumentName + '!' + pluginName; } else { - return Promise.all([ - loader.normalize(argumentName, parentName), - loader.normalize(pluginName, parentName) - ]) - .then(function(normalized) { - argumentName = normalized[0]; - if (defaultExtension && argumentName.substr(argumentName.length - 3, 3) == '.js') - argumentName = argumentName.substr(0, argumentName.length - 3); - return argumentName + '!' + normalized[1]; + return loader.normalize(pluginName, parentName) + .then(function(_pluginName) { + pluginName = _pluginName; + var pluginLoader = loader.pluginLoader || loader; + return pluginLoader['import'](pluginName) + }) + .then(function(pluginModule) { + if (pluginModule.normalize) + return pluginModule.normalize(argumentName, parentName); + return loader.normalize(argumentName, parentName) + .then(function(normalized) { + if (defaultExtension && argumentName.substr(argumentName.length - 3, 3) == '.js') + return argumentName.substr(0, argumentName.length - 3); + return normalized; + }); + }) + .then(function(argumentName) { + return argumentName + '!' + pluginName; }); } } diff --git a/test/test.js b/test/test.js index 20c20d9e..cd439676 100644 --- a/test/test.js +++ b/test/test.js @@ -420,6 +420,13 @@ asyncTest('Advanced compiler plugin', function() { }, err); }); +asyncTest('Plugin normalize hook', function() { + System['import']('normalize-test!tests/normalize-plugin.js').then(function(m) { + ok(m.name == 'normalize-test'); + start(); + }, err); +}); + asyncTest('Plugin as a dependency', function() { System.map['css'] = 'tests/css.js'; System['import']('tests/cjs-loading-plugin.js').then(function(m) { diff --git a/test/tests/normalize-plugin.js b/test/tests/normalize-plugin.js new file mode 100644 index 00000000..fac55b69 --- /dev/null +++ b/test/tests/normalize-plugin.js @@ -0,0 +1,6 @@ +exports.normalize = function(name, parentName) { + return name; +}; +exports.fetch = function(load) { + return 'exports.name = "' + load.name.split('!')[0] + '";'; +} \ No newline at end of file