feat: initial commit
This commit is contained in:
74
node_modules/webpack/lib/json/JsonData.js
generated
vendored
Normal file
74
node_modules/webpack/lib/json/JsonData.js
generated
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
const { register } = require("../util/serialization");
|
||||
|
||||
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
||||
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
||||
/** @typedef {import("../util/Hash")} Hash */
|
||||
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
|
||||
|
||||
class JsonData {
|
||||
/**
|
||||
* @param {Buffer | RawJsonData} data JSON data
|
||||
*/
|
||||
constructor(data) {
|
||||
/** @type {Buffer | undefined} */
|
||||
this._buffer = undefined;
|
||||
/** @type {RawJsonData | undefined} */
|
||||
this._data = undefined;
|
||||
if (Buffer.isBuffer(data)) {
|
||||
this._buffer = data;
|
||||
} else {
|
||||
this._data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {RawJsonData|undefined} Raw JSON data
|
||||
*/
|
||||
get() {
|
||||
if (this._data === undefined && this._buffer !== undefined) {
|
||||
this._data = JSON.parse(this._buffer.toString());
|
||||
}
|
||||
return this._data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Hash} hash hash to be updated
|
||||
* @returns {void} the updated hash
|
||||
*/
|
||||
updateHash(hash) {
|
||||
if (this._buffer === undefined && this._data !== undefined) {
|
||||
this._buffer = Buffer.from(JSON.stringify(this._data));
|
||||
}
|
||||
|
||||
if (this._buffer) hash.update(this._buffer);
|
||||
}
|
||||
}
|
||||
|
||||
register(JsonData, "webpack/lib/json/JsonData", null, {
|
||||
/**
|
||||
* @param {JsonData} obj JSONData object
|
||||
* @param {ObjectSerializerContext} context context
|
||||
*/
|
||||
serialize(obj, { write }) {
|
||||
if (obj._buffer === undefined && obj._data !== undefined) {
|
||||
obj._buffer = Buffer.from(JSON.stringify(obj._data));
|
||||
}
|
||||
write(obj._buffer);
|
||||
},
|
||||
/**
|
||||
* @param {ObjectDeserializerContext} context context
|
||||
* @returns {JsonData} deserialized JSON data
|
||||
*/
|
||||
deserialize({ read }) {
|
||||
return new JsonData(read());
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = JsonData;
|
205
node_modules/webpack/lib/json/JsonGenerator.js
generated
vendored
Normal file
205
node_modules/webpack/lib/json/JsonGenerator.js
generated
vendored
Normal file
@ -0,0 +1,205 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
const { RawSource } = require("webpack-sources");
|
||||
const ConcatenationScope = require("../ConcatenationScope");
|
||||
const { UsageState } = require("../ExportsInfo");
|
||||
const Generator = require("../Generator");
|
||||
const RuntimeGlobals = require("../RuntimeGlobals");
|
||||
|
||||
/** @typedef {import("webpack-sources").Source} Source */
|
||||
/** @typedef {import("../ExportsInfo")} ExportsInfo */
|
||||
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
||||
/** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
||||
/** @typedef {import("../NormalModule")} NormalModule */
|
||||
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
||||
/** @typedef {import("./JsonData")} JsonData */
|
||||
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
|
||||
|
||||
/**
|
||||
* @param {RawJsonData} data Raw JSON data
|
||||
* @returns {undefined|string} stringified data
|
||||
*/
|
||||
const stringifySafe = data => {
|
||||
const stringified = JSON.stringify(data);
|
||||
if (!stringified) {
|
||||
return undefined; // Invalid JSON
|
||||
}
|
||||
|
||||
return stringified.replace(/\u2028|\u2029/g, str =>
|
||||
str === "\u2029" ? "\\u2029" : "\\u2028"
|
||||
); // invalid in JavaScript but valid JSON
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {RawJsonData} data Raw JSON data (always an object or array)
|
||||
* @param {ExportsInfo} exportsInfo exports info
|
||||
* @param {RuntimeSpec} runtime the runtime
|
||||
* @returns {RawJsonData} reduced data
|
||||
*/
|
||||
const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
|
||||
if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused)
|
||||
return data;
|
||||
const isArray = Array.isArray(data);
|
||||
/** @type {RawJsonData} */
|
||||
const reducedData = isArray ? [] : {};
|
||||
for (const key of Object.keys(data)) {
|
||||
const exportInfo = exportsInfo.getReadOnlyExportInfo(key);
|
||||
const used = exportInfo.getUsed(runtime);
|
||||
if (used === UsageState.Unused) continue;
|
||||
|
||||
/** @type {RawJsonData} */
|
||||
let value;
|
||||
if (used === UsageState.OnlyPropertiesUsed && exportInfo.exportsInfo) {
|
||||
value = createObjectForExportsInfo(
|
||||
data[key],
|
||||
exportInfo.exportsInfo,
|
||||
runtime
|
||||
);
|
||||
} else {
|
||||
value = data[key];
|
||||
}
|
||||
|
||||
const name = /** @type {string} */ (exportInfo.getUsedName(key, runtime));
|
||||
/** @type {Record<string, RawJsonData>} */ (reducedData)[name] = value;
|
||||
}
|
||||
if (isArray) {
|
||||
let arrayLengthWhenUsed =
|
||||
exportsInfo.getReadOnlyExportInfo("length").getUsed(runtime) !==
|
||||
UsageState.Unused
|
||||
? data.length
|
||||
: undefined;
|
||||
|
||||
let sizeObjectMinusArray = 0;
|
||||
for (let i = 0; i < reducedData.length; i++) {
|
||||
if (reducedData[i] === undefined) {
|
||||
sizeObjectMinusArray -= 2;
|
||||
} else {
|
||||
sizeObjectMinusArray += `${i}`.length + 3;
|
||||
}
|
||||
}
|
||||
if (arrayLengthWhenUsed !== undefined) {
|
||||
sizeObjectMinusArray +=
|
||||
`${arrayLengthWhenUsed}`.length +
|
||||
8 -
|
||||
(arrayLengthWhenUsed - reducedData.length) * 2;
|
||||
}
|
||||
if (sizeObjectMinusArray < 0)
|
||||
return Object.assign(
|
||||
arrayLengthWhenUsed === undefined
|
||||
? {}
|
||||
: { length: arrayLengthWhenUsed },
|
||||
reducedData
|
||||
);
|
||||
/** @type {number} */
|
||||
const generatedLength =
|
||||
arrayLengthWhenUsed !== undefined
|
||||
? Math.max(arrayLengthWhenUsed, reducedData.length)
|
||||
: reducedData.length;
|
||||
for (let i = 0; i < generatedLength; i++) {
|
||||
if (reducedData[i] === undefined) {
|
||||
reducedData[i] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return reducedData;
|
||||
};
|
||||
|
||||
const TYPES = new Set(["javascript"]);
|
||||
|
||||
class JsonGenerator extends Generator {
|
||||
/**
|
||||
* @param {NormalModule} module fresh module
|
||||
* @returns {Set<string>} available types (do not mutate)
|
||||
*/
|
||||
getTypes(module) {
|
||||
return TYPES;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NormalModule} module the module
|
||||
* @param {string=} type source type
|
||||
* @returns {number} estimate size of the module
|
||||
*/
|
||||
getSize(module, type) {
|
||||
/** @type {RawJsonData | undefined} */
|
||||
const data =
|
||||
module.buildInfo &&
|
||||
module.buildInfo.jsonData &&
|
||||
module.buildInfo.jsonData.get();
|
||||
if (!data) return 0;
|
||||
return /** @type {string} */ (stringifySafe(data)).length + 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
getConcatenationBailoutReason(module, context) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {NormalModule} module module for which the code should be generated
|
||||
* @param {GenerateContext} generateContext context for generate
|
||||
* @returns {Source} generated code
|
||||
*/
|
||||
generate(
|
||||
module,
|
||||
{
|
||||
moduleGraph,
|
||||
runtimeTemplate,
|
||||
runtimeRequirements,
|
||||
runtime,
|
||||
concatenationScope
|
||||
}
|
||||
) {
|
||||
/** @type {RawJsonData | undefined} */
|
||||
const data =
|
||||
module.buildInfo &&
|
||||
module.buildInfo.jsonData &&
|
||||
module.buildInfo.jsonData.get();
|
||||
if (data === undefined) {
|
||||
return new RawSource(
|
||||
runtimeTemplate.missingModuleStatement({
|
||||
request: module.rawRequest
|
||||
})
|
||||
);
|
||||
}
|
||||
const exportsInfo = moduleGraph.getExportsInfo(module);
|
||||
/** @type {RawJsonData} */
|
||||
let finalJson =
|
||||
typeof data === "object" &&
|
||||
data &&
|
||||
exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused
|
||||
? createObjectForExportsInfo(data, exportsInfo, runtime)
|
||||
: data;
|
||||
// Use JSON because JSON.parse() is much faster than JavaScript evaluation
|
||||
const jsonStr = /** @type {string} */ (stringifySafe(finalJson));
|
||||
const jsonExpr =
|
||||
jsonStr.length > 20 && typeof finalJson === "object"
|
||||
? `/*#__PURE__*/JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
|
||||
: jsonStr;
|
||||
/** @type {string} */
|
||||
let content;
|
||||
if (concatenationScope) {
|
||||
content = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
||||
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
||||
} = ${jsonExpr};`;
|
||||
concatenationScope.registerNamespaceExport(
|
||||
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
||||
);
|
||||
} else {
|
||||
runtimeRequirements.add(RuntimeGlobals.module);
|
||||
content = `${module.moduleArgument}.exports = ${jsonExpr};`;
|
||||
}
|
||||
return new RawSource(content);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = JsonGenerator;
|
59
node_modules/webpack/lib/json/JsonModulesPlugin.js
generated
vendored
Normal file
59
node_modules/webpack/lib/json/JsonModulesPlugin.js
generated
vendored
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
const { JSON_MODULE_TYPE } = require("../ModuleTypeConstants");
|
||||
const createSchemaValidation = require("../util/create-schema-validation");
|
||||
const JsonGenerator = require("./JsonGenerator");
|
||||
const JsonParser = require("./JsonParser");
|
||||
|
||||
/** @typedef {import("../Compiler")} Compiler */
|
||||
/** @typedef {Record<string, any>} RawJsonData */
|
||||
|
||||
const validate = createSchemaValidation(
|
||||
require("../../schemas/plugins/JsonModulesPluginParser.check.js"),
|
||||
() => require("../../schemas/plugins/JsonModulesPluginParser.json"),
|
||||
{
|
||||
name: "Json Modules Plugin",
|
||||
baseDataPath: "parser"
|
||||
}
|
||||
);
|
||||
|
||||
const PLUGIN_NAME = "JsonModulesPlugin";
|
||||
|
||||
/**
|
||||
* The JsonModulesPlugin is the entrypoint plugin for the json modules feature.
|
||||
* It adds the json module type to the compiler and registers the json parser and generator.
|
||||
*/
|
||||
class JsonModulesPlugin {
|
||||
/**
|
||||
* Apply the plugin
|
||||
* @param {Compiler} compiler the compiler instance
|
||||
* @returns {void}
|
||||
*
|
||||
*/
|
||||
apply(compiler) {
|
||||
compiler.hooks.compilation.tap(
|
||||
PLUGIN_NAME,
|
||||
(compilation, { normalModuleFactory }) => {
|
||||
normalModuleFactory.hooks.createParser
|
||||
.for(JSON_MODULE_TYPE)
|
||||
.tap(PLUGIN_NAME, parserOptions => {
|
||||
validate(parserOptions);
|
||||
|
||||
return new JsonParser(parserOptions);
|
||||
});
|
||||
normalModuleFactory.hooks.createGenerator
|
||||
.for(JSON_MODULE_TYPE)
|
||||
.tap(PLUGIN_NAME, () => {
|
||||
return new JsonGenerator();
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = JsonModulesPlugin;
|
69
node_modules/webpack/lib/json/JsonParser.js
generated
vendored
Normal file
69
node_modules/webpack/lib/json/JsonParser.js
generated
vendored
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
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 memoize = require("../util/memoize");
|
||||
const JsonData = require("./JsonData");
|
||||
|
||||
/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
|
||||
/** @typedef {import("../Module").BuildInfo} BuildInfo */
|
||||
/** @typedef {import("../Module").BuildMeta} BuildMeta */
|
||||
/** @typedef {import("../Parser").ParserState} ParserState */
|
||||
/** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
|
||||
/** @typedef {import("./JsonModulesPlugin").RawJsonData} RawJsonData */
|
||||
|
||||
const getParseJson = memoize(() => require("json-parse-even-better-errors"));
|
||||
|
||||
class JsonParser extends Parser {
|
||||
/**
|
||||
* @param {JsonModulesPluginParserOptions} options parser options
|
||||
*/
|
||||
constructor(options) {
|
||||
super();
|
||||
this.options = options || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @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("utf-8");
|
||||
}
|
||||
|
||||
/** @type {NonNullable<JsonModulesPluginParserOptions["parse"]>} */
|
||||
const parseFn =
|
||||
typeof this.options.parse === "function"
|
||||
? this.options.parse
|
||||
: getParseJson();
|
||||
/** @type {Buffer | RawJsonData | undefined} */
|
||||
let data;
|
||||
try {
|
||||
data =
|
||||
typeof source === "object"
|
||||
? source
|
||||
: parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot parse JSON: ${/** @type {Error} */ (e).message}`);
|
||||
}
|
||||
const jsonData = new JsonData(/** @type {Buffer | RawJsonData} */ (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" ? "redirect-warn" : false;
|
||||
state.module.addDependency(new JsonExportsDependency(jsonData));
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = JsonParser;
|
Reference in New Issue
Block a user