diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /tools/esmify/is-esmified.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/esmify/is-esmified.js')
-rw-r--r-- | tools/esmify/is-esmified.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/tools/esmify/is-esmified.js b/tools/esmify/is-esmified.js new file mode 100644 index 0000000000..872b4cd877 --- /dev/null +++ b/tools/esmify/is-esmified.js @@ -0,0 +1,81 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +// A utility to check if given JSM is already ESM-ified. + +/* eslint-env node */ + +const fs = require("fs"); +const _path = require("path"); +const { esmifyExtension } = require(_path.resolve(__dirname, "./utils.js")); + +let json_map; +if (process.env.ESMIFY_MAP_JSON) { + json_map = _path.resolve(process.env.ESMIFY_MAP_JSON); +} else { + json_map = _path.resolve(__dirname, "./map.json"); +} +const uri_map = JSON.parse(fs.readFileSync(json_map)); +const esm_uri_map = generateESMURIMap(uri_map); + +function generateESMURIMap(jsm_map) { + const esm_map = {}; + + for (let [uri, jsms] of Object.entries(jsm_map)) { + if (typeof jsms === "string") { + jsms = [jsms]; + } + esm_map[esmifyExtension(uri)] = jsms.map(esmifyExtension); + } + + return esm_map; +} + +function isESMifiedSlow(resourceURI) { + if (!(resourceURI in uri_map)) { + console.log(`WARNING: Unknown module: ${resourceURI}`); + return { result: false, jsms: [] }; + } + + let jsms = uri_map[resourceURI]; + if (typeof jsms === "string") { + jsms = [jsms]; + } + + const prefix = "../../"; + for (const jsm of jsms) { + if (fs.existsSync(prefix + jsm)) { + return { result: false, jsms }; + } + const esm = esmifyExtension(jsm); + if (!fs.existsSync(prefix + esm)) { + return { result: false, jsms }; + } + } + + return { result: true, jsms }; +} + +const isESMified_memo = {}; +function isESMified(resourceURI, files) { + if (!(resourceURI in isESMified_memo)) { + isESMified_memo[resourceURI] = isESMifiedSlow(resourceURI); + } + + for (const jsm of isESMified_memo[resourceURI].jsms) { + files.push(esmifyExtension(jsm)); + } + + return isESMified_memo[resourceURI].result; +} + +function getESMFiles(resourceURI) { + if (resourceURI in esm_uri_map) { + return esm_uri_map[resourceURI]; + } + return []; +} + +exports.isESMified = isESMified; +exports.getESMFiles = getESMFiles; |