/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
lib/util/parseJson.js
43 строки
1 KB
Alexander Akait
perf: load webpack's own modules only when a build needs them (#21652)
10 авг 2026, 07:32
Не верифицирован
10 авг 2026, 07:32
8e28763
Код
Авторство
О чём код?
/* MIT License http://www.opensource.org/licenses/mit-license.php */ "use strict"; const memoize = require("./memoize"); const getJSONParseError = memoize(() => require("../errors/JSONParseError")); /** @typedef {import("../util/fs").JsonValue} JsonValue */ // Inspired by https://github.com/npm/json-parse-even-better-errors // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) // because the buffer-to-string conversion in `fs.readFileSync()` // translates it to FEFF, the UTF-16 BOM. /** * @param {string | Buffer} txt text * @returns {string} text without BOM */ const stripBOM = (txt) => String(txt).replace(/^\uFEFF/, ""); /** * @template [R=JsonValue] * @callback ParseJsonFn * @param {string} raw text * @param {(this: EXPECTED_ANY, key: string, value: EXPECTED_ANY) => EXPECTED_ANY=} reviver reviver * @returns {R} parsed JSON */ /** @type {ParseJsonFn} */ const parseJson = (raw, reviver) => { const txt = stripBOM(raw); try { return JSON.parse(txt, reviver); } catch (err) { throw new (getJSONParseError())(/** @type {Error} */ (err), raw, txt); } }; module.exports = parseJson;