/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v5.107.0
lib/json/JsonParser.js
82 строки
2 KB
Alexander Akait
refactor: move dll related code to dll directory (#20868)
24 апр 2026, 20:50
Не верифицирован
24 апр 2026, 20:50
235a160
Код
Авторство
О чём код?
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ "use strict"; const Parser = require("../Parser"); const JsonExportsDependency = require("../dependencies/JsonExportsDependency"); const parseJson = require("../util/parseJson"); const JsonData = require("./JsonData"); /** @typedef {import("../../declarations/WebpackOptions").JsonParserOptions} JsonParserOptions */ /** @typedef {import("../Module").BuildInfo} BuildInfo */ /** @typedef {import("../Module").BuildMeta} BuildMeta */ /** @typedef {import("../Parser").ParserState} ParserState */ /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */ /** @typedef {import("../util/fs").JsonValue} JsonValue */ /** @typedef {(input: string) => Buffer | JsonValue} ParseFn */ /** * Defines the function returning type used by this module. * @template T * @typedef {import("../util/memoize").FunctionReturning<T>} FunctionReturning */ class JsonParser extends Parser { /** * Creates an instance of JsonParser. * @param {JsonParserOptions} options parser options */ constructor(options = {}) { super(); /** @type {JsonParserOptions} */ this.options = options; } /** * Parses the provided source and updates the parser state. * @param {string | Buffer | PreparsedAst} source the source to parse * @param {ParserState} state the parser state * @returns {ParserState} the parser state */ parse(source, state) { if (Buffer.isBuffer(source)) { source = source.toString("utf8"); } const parseFn = typeof this.options.parse === "function" ? this.options.parse : parseJson; /** @type {Buffer | JsonValue | undefined} */ const data = typeof source === "object" ? source : parseFn(source[0] === "\uFEFF" ? source.slice(1) : source); const jsonData = new JsonData(/** @type {Buffer | JsonValue} */ (data)); const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo); buildInfo.jsonData = jsonData; buildInfo.strict = true; const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta); buildMeta.exportsType = "default"; buildMeta.defaultObject = typeof data === "object" ? this.options.namedExports === false ? false : this.options.namedExports === true ? "redirect" : "redirect-warn" : false; state.module.addDependency( new JsonExportsDependency( jsonData, /** @type {number} */ (this.options.exportsDepth) ) ); return state; } } module.exports = JsonParser;