| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Parser = require("../Parser");
|
|---|
| 9 | const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
|
|---|
| 10 | const parseJson = require("../util/parseJson");
|
|---|
| 11 | const JsonData = require("./JsonData");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("../../declarations/WebpackOptions").JsonParserOptions} JsonParserOptions */
|
|---|
| 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("../util/fs").JsonValue} JsonValue */
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {(input: string) => Buffer | JsonValue} ParseFn */
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Defines the function returning type used by this module.
|
|---|
| 24 | * @template T
|
|---|
| 25 | * @typedef {import("../util/memoize").FunctionReturning<T>} FunctionReturning
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | class JsonParser extends Parser {
|
|---|
| 29 | /**
|
|---|
| 30 | * Creates an instance of JsonParser.
|
|---|
| 31 | * @param {JsonParserOptions} options parser options
|
|---|
| 32 | */
|
|---|
| 33 | constructor(options = {}) {
|
|---|
| 34 | super();
|
|---|
| 35 | /** @type {JsonParserOptions} */
|
|---|
| 36 | this.options = options;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Parses the provided source and updates the parser state.
|
|---|
| 41 | * @param {string | Buffer | PreparsedAst} source the source to parse
|
|---|
| 42 | * @param {ParserState} state the parser state
|
|---|
| 43 | * @returns {ParserState} the parser state
|
|---|
| 44 | */
|
|---|
| 45 | parse(source, state) {
|
|---|
| 46 | if (Buffer.isBuffer(source)) {
|
|---|
| 47 | source = source.toString("utf8");
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | const parseFn =
|
|---|
| 51 | typeof this.options.parse === "function" ? this.options.parse : parseJson;
|
|---|
| 52 | /** @type {Buffer | JsonValue | undefined} */
|
|---|
| 53 | const data =
|
|---|
| 54 | typeof source === "object"
|
|---|
| 55 | ? source
|
|---|
| 56 | : parseFn(source[0] === "\uFEFF" ? source.slice(1) : source);
|
|---|
| 57 | const jsonData = new JsonData(/** @type {Buffer | JsonValue} */ (data));
|
|---|
| 58 | const buildInfo = /** @type {BuildInfo} */ (state.module.buildInfo);
|
|---|
| 59 | buildInfo.jsonData = jsonData;
|
|---|
| 60 | buildInfo.strict = true;
|
|---|
| 61 | const buildMeta = /** @type {BuildMeta} */ (state.module.buildMeta);
|
|---|
| 62 | buildMeta.exportsType = "default";
|
|---|
| 63 | buildMeta.defaultObject =
|
|---|
| 64 | typeof data === "object"
|
|---|
| 65 | ? this.options.namedExports === false
|
|---|
| 66 | ? false
|
|---|
| 67 | : this.options.namedExports === true
|
|---|
| 68 | ? "redirect"
|
|---|
| 69 | : "redirect-warn"
|
|---|
| 70 | : false;
|
|---|
| 71 | state.module.addDependency(
|
|---|
| 72 | new JsonExportsDependency(
|
|---|
| 73 | jsonData,
|
|---|
| 74 | /** @type {number} */
|
|---|
| 75 | (this.options.exportsDepth)
|
|---|
| 76 | )
|
|---|
| 77 | );
|
|---|
| 78 | return state;
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | module.exports = JsonParser;
|
|---|