/* 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/. */ "use strict";
const IGNORE = [
/\.git/,
/\.hg/,
/node_modules/,
/^obj.*/,
/test262/, // eslint-plugin-mozilla isn't part of Gecko/Firefox code, but runs tests // simulating imports that we don't need to define in the paths.
/eslint-plugin-mozilla/,
]; constIMPORT =
/(?<!@)(\bimport |import\(|require\(|\.(importESModule|defineESModuleGetters?|declareLazy|defineLazy)\()[^;]+/gm; // TypeScript imports have no `;` so cannot be included in the IMPORT regular // expression. Therefore we have a separate expression to handle the TypeScript // specific imports which will be within comments. const TYPESCRIPT_IMPORT =
/\/\*\*?\s*@import\s.*?\s+from\s+["'][^"']+["']\s*\*\//gm; const URI = /("|')((resource|chrome|moz-src):\/\/[\w\d\/_.-]+\.m?js)\1/gm;
function ignore(filePath) { return IGNORE.some(re => filePath.match(re));
}
// Scan the root dir for *.js and *.mjs files, recursivelly. function scan(root, dir, files) { for (let file of fs.readdirSync(`${root}/${dir}`, { withFileTypes: true })) { if (file.isDirectory() && !ignore(dir + file.name)) {
scan(root, `${dir}${file.name}/`, files);
} elseif (file.name.match(/\.(x?html|m?js)$/)) {
files.push(dir + file.name);
}
}
}
// Emit path mapping for all found module URIs. function emitPaths(files, uris, modules, relativeBasePath) { /** @type {Record<string, string[]>} */
let paths = {}; for (let uri of [...uris].sort()) { // Fixed URIs need to go at the end, with their own URIs if (uri in fixed) { continue;
}
let parts = uri.split("/");
let filePath = "";
let matches;
// Check for a substitution .d.ts file from processed/generated sources.
let sub = parts.at(-1).replace(/\.(m)?js$/, ".d.$1ts"); if (fs.existsSync(`${__dirname}/../@types/subs/${sub}`)) {
paths[uri] = [`${relativeBasePath}/tools/@types/subs/${sub}`]; continue;
}
// Starting with just the file name, add each path part going backwards. do {
filePath = "/" + parts.pop() + filePath;
matches = files.filter(f => f.endsWith(filePath)); // While multiple files match, add parts used for filtering.
} while (matches.length > 1 && parts.length);
// Unique match is almost certainy correct. if (matches.length === 1) {
paths[uri] = [`${relativeBasePath}/${matches[0]}`];
} else { // URI matched more than one, or failed to match any file.
console.warn("[WARN]", uri);
modules.delete(uri);
}
}
// Emit type mapping for all modules imported via lazy getters. function emitLazy(modules) {
let lines = [HEADER];
lines.push("export interface Modules {"); for (let uri of [...modules].sort()) {
lines.push(` "${uri}": typeofimport("${uri}"),`);
}
lines.push("}\n"); return lines.join("\n");
}
function main(root_dir, paths_json, lib_lazy) {
let files = [];
scan(root_dir, "", files);
let uris = new Set();
let modules = new Set();
for (let file of files) {
let src = fs.readFileSync(`${root_dir}/${file}`, "utf-8");
for (let [exp, , method] of [
...src.matchAll(IMPORT),
...src.matchAll(TYPESCRIPT_IMPORT),
]) { for (let [, , uri, proto] of exp.matchAll(URI)) { if (proto !== "moz-src") {
uris.add(uri);
} if (method?.match(/importESModule|ModuleGetter|Lazy/)) {
modules.add(uri);
}
}
}
}
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.