source: imaps-frontend/node_modules/webpack/lib/json/JsonParser.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Parser = require("../Parser");
9const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
10const memoize = require("../util/memoize");
11const JsonData = require("./JsonData");
12
13/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
14/** @typedef {import("../Module").BuildInfo} BuildInfo */
15/** @typedef {import("../Module").BuildMeta} BuildMeta */
16/** @typedef {import("../Parser").ParserState} ParserState */
17/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
18/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
19
20const getParseJson = memoize(() => require("json-parse-even-better-errors"));
21
22class JsonParser extends Parser {
23 /**
24 * @param {JsonModulesPluginParserOptions} options parser options
25 */
26 constructor(options) {
27 super();
28 this.options = options || {};
29 }
30
31 /**
32 * @param {string | Buffer | PreparsedAst} source the source to parse
33 * @param {ParserState} state the parser state
34 * @returns {ParserState} the parser state
35 */
36 parse(source, state) {
37 if (Buffer.isBuffer(source)) {
38 source = source.toString("utf-8");
39 }
40
41 /** @type {NonNullable<JsonModulesPluginParserOptions["parse"]>} */
42 const parseFn =
43 typeof this.options.parse === "function"
44 ? this.options.parse
45 : getParseJson();
46 /** @type {Buffer | RawJsonData | undefined} */
47 let data;
48 try {
49 data =
50 typeof source === "object"
51 ? source
52 : parseFn(source[0] === "\uFEFF" ? source.slice(1) : source);
53 } catch (err) {
54 throw new Error(
55 `Cannot parse JSON: ${/** @type {Error} */ (err).message}`
56 );
57 }
58 const jsonData = new JsonData(/** @type {Buffer | RawJsonData} */ (data));
59 const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo);
60 buildInfo.jsonData = jsonData;
61 buildInfo.strict = true;
62 const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
63 buildMeta.exportsType = "default";
64 buildMeta.defaultObject =
65 typeof data === "object" ? "redirect-warn" : false;
66 state.module.addDependency(new JsonExportsDependency(jsonData));
67 return state;
68 }
69}
70
71module.exports = JsonParser;
Note: See TracBrowser for help on using the repository browser.