Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update system-extension-locate.js #1592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/system-extension-locate.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ addStealExtension(function addLocateProtocol(loader) {
* @param {String} moduleName The module to run through normalize and locate.
* @return {Promise} A promise to resolve when the address is found.
*/
var normalizeAndLocate = function(moduleName, parentName){
var loader = this;
let normalizeAndLocate = function(moduleName, parentName){
let loader = this;
return Promise.resolve(loader.normalize(moduleName, parentName))
.then(function(name){
return loader.locate({name: name, metadata: {}});
}).then(function(address){
var outAddress = address;
let outAddress = address;
if(address.substr(address.length - 3) === ".js") {
outAddress = address.substr(0, address.length - 3);
}
return outAddress;
});
};

var relative = function(base, path){
var uriParts = path.split("/"),
let relative = function(base, path){
let uriParts = path.split("/"),
baseParts = base.split("/"),
result = [];

Expand All @@ -39,9 +39,9 @@ addStealExtension(function addLocateProtocol(loader) {
return result.join("") + uriParts.join("/");
};

var schemePattern = /(locate):\/\/([a-z0-9/._@-]*)/ig,
let schemePattern = /(locate):\/\/([a-z0-9/._@-]*)/ig,
parsePathSchemes = function(source, parent) {
var locations = [];
let locations = [];
source.replace(schemePattern, function(whole, scheme, path, index){
locations.push({
start: index,
Expand All @@ -55,38 +55,38 @@ addStealExtension(function addLocateProtocol(loader) {
return locations;
};

var _translate = loader.translate;
let _translate = loader.translate;
loader.translate = function(load){
var loader = this;
let loader = this;

// This only applies to plugin resources.
if(!load.metadata.plugin) {
return _translate.call(this, load);
}

// Use the translator if this file path scheme is supported by the plugin
var locateSupport = load.metadata.plugin.locateScheme;
let locateSupport = load.metadata.plugin.locateScheme;
if(!locateSupport) {
return _translate.call(this, load);
}

// Parse array of module names
var locations = parsePathSchemes(load.source, load.address);
let locations = parsePathSchemes(load.source, load.address);

// no locations found
if(!locations.length) {
return _translate.call(this, load);
}

// normalize and locate all of the modules found and then replace those instances in the source.
var promises = [];
for(var i = 0, len = locations.length; i < len; i++) {
let promises = [];
for(let i = 0, len = locations.length; i < len; i++) {
promises.push(
normalizeAndLocate.call(this, locations[i].name, load.name)
);
}
return Promise.all(promises).then(function(addresses){
for(var i = locations.length - 1; i >= 0; i--) {
for(let i = locations.length - 1; i >= 0; i--) {
load.source = load.source.substr(0, locations[i].start)
+ locations[i].postLocate(addresses[i])
+ load.source.substr(locations[i].end, load.source.length);
Expand Down