mirror of https://github.com/webpack/webpack.git
Merge pull request #13725 from webpack/bugfix/serialize-json-as-buffer
serialize json data as buffer and decode on demand
This commit is contained in:
commit
49c0864551
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||||
|
Author Tobias Koppers @sokra
|
||||||
|
*/
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const { register } = require("../util/serialization");
|
||||||
|
|
||||||
|
class JsonData {
|
||||||
|
constructor(data) {
|
||||||
|
this._buffer = undefined;
|
||||||
|
this._data = undefined;
|
||||||
|
if (Buffer.isBuffer(data)) {
|
||||||
|
this._buffer = data;
|
||||||
|
} else {
|
||||||
|
this._data = data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get() {
|
||||||
|
if (this._data === undefined && this._buffer !== undefined) {
|
||||||
|
this._data = JSON.parse(this._buffer.toString());
|
||||||
|
}
|
||||||
|
return this._data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
register(JsonData, "webpack/lib/json/JsonData", null, {
|
||||||
|
serialize(obj, { write }) {
|
||||||
|
if (obj._buffer === undefined && obj._data !== undefined) {
|
||||||
|
obj._buffer = Buffer.from(JSON.stringify(obj._data));
|
||||||
|
}
|
||||||
|
write(obj._buffer);
|
||||||
|
},
|
||||||
|
deserialize({ read }) {
|
||||||
|
return new JsonData(read());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = JsonData;
|
||||||
|
|
@ -116,7 +116,10 @@ class JsonGenerator extends Generator {
|
||||||
* @returns {number} estimate size of the module
|
* @returns {number} estimate size of the module
|
||||||
*/
|
*/
|
||||||
getSize(module, type) {
|
getSize(module, type) {
|
||||||
let data = module.buildInfo && module.buildInfo.jsonData;
|
let data =
|
||||||
|
module.buildInfo &&
|
||||||
|
module.buildInfo.jsonData &&
|
||||||
|
module.buildInfo.jsonData.get();
|
||||||
if (!data) return 0;
|
if (!data) return 0;
|
||||||
return stringifySafe(data).length + 10;
|
return stringifySafe(data).length + 10;
|
||||||
}
|
}
|
||||||
|
|
@ -145,7 +148,10 @@ class JsonGenerator extends Generator {
|
||||||
concatenationScope
|
concatenationScope
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
const data = module.buildInfo && module.buildInfo.jsonData;
|
const data =
|
||||||
|
module.buildInfo &&
|
||||||
|
module.buildInfo.jsonData &&
|
||||||
|
module.buildInfo.jsonData.get();
|
||||||
if (data === undefined) {
|
if (data === undefined) {
|
||||||
return new RawSource(
|
return new RawSource(
|
||||||
runtimeTemplate.missingModuleStatement({
|
runtimeTemplate.missingModuleStatement({
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
const parseJson = require("json-parse-better-errors");
|
const parseJson = require("json-parse-better-errors");
|
||||||
const Parser = require("../Parser");
|
const Parser = require("../Parser");
|
||||||
const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
|
const JsonExportsDependency = require("../dependencies/JsonExportsDependency");
|
||||||
|
const JsonData = require("./JsonData");
|
||||||
|
|
||||||
/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
|
/** @typedef {import("../../declarations/plugins/JsonModulesPluginParser").JsonModulesPluginParserOptions} JsonModulesPluginParserOptions */
|
||||||
/** @typedef {import("../Parser").ParserState} ParserState */
|
/** @typedef {import("../Parser").ParserState} ParserState */
|
||||||
|
|
@ -41,7 +42,7 @@ class JsonParser extends Parser {
|
||||||
? source
|
? source
|
||||||
: parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
|
: parseFn(source[0] === "\ufeff" ? source.slice(1) : source);
|
||||||
|
|
||||||
state.module.buildInfo.jsonData = data;
|
state.module.buildInfo.jsonData = new JsonData(data);
|
||||||
state.module.buildInfo.strict = true;
|
state.module.buildInfo.strict = true;
|
||||||
state.module.buildMeta.exportsType = "default";
|
state.module.buildMeta.exportsType = "default";
|
||||||
state.module.buildMeta.defaultObject =
|
state.module.buildMeta.defaultObject =
|
||||||
|
|
|
||||||
|
|
@ -156,6 +156,7 @@ module.exports = {
|
||||||
require("../dependencies/WebpackIsIncludedDependency"),
|
require("../dependencies/WebpackIsIncludedDependency"),
|
||||||
"dependencies/WorkerDependency": () =>
|
"dependencies/WorkerDependency": () =>
|
||||||
require("../dependencies/WorkerDependency"),
|
require("../dependencies/WorkerDependency"),
|
||||||
|
"json/JsonData": () => require("../json/JsonData"),
|
||||||
"optimize/ConcatenatedModule": () =>
|
"optimize/ConcatenatedModule": () =>
|
||||||
require("../optimize/ConcatenatedModule"),
|
require("../optimize/ConcatenatedModule"),
|
||||||
DelegatedModule: () => require("../DelegatedModule"),
|
DelegatedModule: () => require("../DelegatedModule"),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue