| | |
| | | const ConcatenationScope = require("../ConcatenationScope"); |
| | | const { UsageState } = require("../ExportsInfo"); |
| | | const Generator = require("../Generator"); |
| | | const { JS_TYPES } = require("../ModuleSourceTypesConstants"); |
| | | const { JAVASCRIPT_TYPES } = require("../ModuleSourceTypeConstants"); |
| | | const RuntimeGlobals = require("../RuntimeGlobals"); |
| | | |
| | | /** @typedef {import("webpack-sources").Source} Source */ |
| | | /** @typedef {import("../../declarations/WebpackOptions").JsonGeneratorOptions} JsonGeneratorOptions */ |
| | | /** @typedef {import("../ExportsInfo")} ExportsInfo */ |
| | | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ |
| | | /** @typedef {import("../Generator").UpdateHashContext} UpdateHashContext */ |
| | | /** @typedef {import("../util/Hash")} Hash */ |
| | | /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */ |
| | | /** @typedef {import("../Module").SourceType} SourceType */ |
| | | /** @typedef {import("../Module").SourceTypes} SourceTypes */ |
| | | /** @typedef {import("../NormalModule")} NormalModule */ |
| | | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */ |
| | |
| | | /** @typedef {import("../util/fs").JsonValue} JsonValue */ |
| | | |
| | | /** |
| | | * Returns stringified data. |
| | | * @param {JsonValue} data Raw JSON data |
| | | * @returns {undefined|string} stringified data |
| | | * @returns {undefined | string} stringified data |
| | | */ |
| | | const stringifySafe = (data) => { |
| | | const stringified = JSON.stringify(data); |
| | |
| | | }; |
| | | |
| | | /** |
| | | * Creates an object for exports info. |
| | | * @param {JsonObject | JsonArray} data Raw JSON data (always an object or array) |
| | | * @param {ExportsInfo} exportsInfo exports info |
| | | * @param {RuntimeSpec} runtime the runtime |
| | |
| | | |
| | | class JsonGenerator extends Generator { |
| | | /** |
| | | * Creates an instance of JsonGenerator. |
| | | * @param {JsonGeneratorOptions} options options |
| | | */ |
| | | constructor(options) { |
| | | super(); |
| | | /** @type {JsonGeneratorOptions} */ |
| | | this.options = options; |
| | | } |
| | | |
| | | /** |
| | | * Returns the source types available for this module. |
| | | * @param {NormalModule} module fresh module |
| | | * @returns {SourceTypes} available types (do not mutate) |
| | | */ |
| | | getTypes(module) { |
| | | return JS_TYPES; |
| | | return JAVASCRIPT_TYPES; |
| | | } |
| | | |
| | | /** |
| | | * Returns the estimated size for the requested source type. |
| | | * @param {NormalModule} module the module |
| | | * @param {string=} type source type |
| | | * @param {SourceType=} type source type |
| | | * @returns {number} estimate size of the module |
| | | */ |
| | | getSize(module, type) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns the reason this module cannot be concatenated, when one exists. |
| | | * @param {NormalModule} module module for which the bailout reason should be determined |
| | | * @param {ConcatenationBailoutReasonContext} context context |
| | | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates generated code for this runtime module. |
| | | * @param {NormalModule} module module for which the code should be generated |
| | | * @param {GenerateContext} generateContext context for generate |
| | | * @returns {Source | null} generated code |
| | |
| | | } |
| | | |
| | | /** |
| | | * Generates fallback output for the provided error condition. |
| | | * @param {Error} error the error |
| | | * @param {NormalModule} module module for which the code should be generated |
| | | * @param {GenerateContext} generateContext context for generate |
| | |
| | | generateError(error, module, generateContext) { |
| | | return new RawSource(`throw new Error(${JSON.stringify(error.message)});`); |
| | | } |
| | | |
| | | /** |
| | | * Updates the hash with the data contributed by this instance. |
| | | * @param {Hash} hash hash that will be modified |
| | | * @param {UpdateHashContext} updateHashContext context for updating hash |
| | | */ |
| | | updateHash(hash, updateHashContext) { |
| | | if (this.options.JSONParse) { |
| | | hash.update("json-parse"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | module.exports = JsonGenerator; |