/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** *ImplementationofaCommonJSmoduleloaderforworkers. * *Use: *// in the .js file loaded by the constructor of the worker *importScripts("resource://gre/modules/workers/require.js"); *letmodule=require("resource://gre/modules/worker/myModule.js"); * *// in myModule.js *// Load dependencies *letSimpleTest=require("resource://gre/modules/workers/SimpleTest.js"); *letLogger=require("resource://gre/modules/workers/Logger.js"); * *// Define things that will not be exported *letsomeValue=// ... * *// Export symbols *exports.foo=// ... *exports.bar=// ... * * *Note#1: *Properties|fileName|and|stack|oferrorstriggeredfromamodule *containfilenamesthatdonotcorrespondtohuman-readablemodulepaths. *Humanreadersshouldratheruseproperties|moduleName|and|moduleStack|. * *Note#2: *Byoppositiontosomeothermoduleloaderimplementations,thismodule *loaderdoesnotenforceseparationofglobalobjects.Consequently,if *amodulemodifiesaglobalobject(e.g.|String.prototype|),allother *modulesinthesameworkermaybeaffected.
*/
/* global require */ /* exported require */
(function (exports) { "use strict";
if (exports.require) { // Avoid double-imports return;
}
// Simple implementation of |require|
let require = (function () { /** *MappingfromtheabsolutemoduleURItotheexportsobjectforthatmodule. * *@type{Map<string,object>}
*/
let modules = new Map();
/** *Importamodule * *@param{string}baseURLTheURLofthemodulesfromwhichweloadanewmodule. *ThiswillbenullforthefirstloadedmoduleandsoexpectanabsoluteURIinpath. *Notethatthisfirstparameterisboundbefore`require`methodispassedoutside *ofthismodule.Sothattypicalcallsiteswillonlypaththesecond`path`parameter. *@param{string}pathThepathtothemodule. *@return{*}Anobjectcontainingthepropertiesexportedbythemodule.
*/ returnfunction require(baseURL, path) {
let startTime = ChromeUtils.now(); if (typeof path != "string") { thrownew TypeError( "The argument to require() must be a string got " + path
);
}
// Resolve relative paths if ((path.startsWith("./") || path.startsWith("../")) && baseURL) {
path = new URL(path, baseURL).href;
}
if (!path.includes("://")) { thrownew TypeError( "The argument to require() must be a string uri, got " + path
);
} // Automatically add ".js" if there is no extension
let uri; if (path.lastIndexOf(".") <= path.lastIndexOf("/")) {
uri = path + ".js";
} else {
uri = path;
}
// Exports provided by the module
let exports = Object.create(null);
// Identification of the module
let module = {
id: path,
uri,
exports,
};
// Make module available immediately // (necessary in case of circular dependencies) if (modules.has(uri)) { return modules.get(uri).exports;
}
modules.set(uri, module);
try { // Load source of module, synchronously
let xhr = new XMLHttpRequest();
xhr.open("GET", uri, false);
xhr.responseType = "text";
xhr.send();
let source = xhr.responseText; if (source == "") { // There doesn't seem to be a better way to detect that the file couldn't be found thrownew Error("Could not find module " + path);
} // Use `Function` to leave this scope, use `eval` to start the line // number from 1 that is observed by `source` and the error message // thrown from the module, and also use `arguments` for accessing // `source` and `uri` to avoid polluting the module's environment.
let code = newFunction( "exports", "require", "module",
`eval(arguments[3] + "\\n//# sourceURL=" + arguments[4] + "\\n")`
);
code(exports, require.bind(null, path), module, source, uri);
} catch (ex) { // Module loading has failed, exports should not be made available // after all.
modules.delete(uri); throw ex;
} finally {
ChromeUtils.addProfilerMarker("require", startTime, path);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.