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 { RawSource } = require("webpack-sources");
|
---|
9 | const ConcatenationScope = require("../ConcatenationScope");
|
---|
10 | const { UsageState } = require("../ExportsInfo");
|
---|
11 | const Generator = require("../Generator");
|
---|
12 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
13 |
|
---|
14 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
15 | /** @typedef {import("../ExportsInfo")} ExportsInfo */
|
---|
16 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
---|
17 | /** @typedef {import("../Module").ConcatenationBailoutReasonContext} ConcatenationBailoutReasonContext */
|
---|
18 | /** @typedef {import("../NormalModule")} NormalModule */
|
---|
19 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
20 |
|
---|
21 | const stringifySafe = data => {
|
---|
22 | const stringified = JSON.stringify(data);
|
---|
23 | if (!stringified) {
|
---|
24 | return undefined; // Invalid JSON
|
---|
25 | }
|
---|
26 |
|
---|
27 | return stringified.replace(/\u2028|\u2029/g, str =>
|
---|
28 | str === "\u2029" ? "\\u2029" : "\\u2028"
|
---|
29 | ); // invalid in JavaScript but valid JSON
|
---|
30 | };
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @param {Object} data data (always an object or array)
|
---|
34 | * @param {ExportsInfo} exportsInfo exports info
|
---|
35 | * @param {RuntimeSpec} runtime the runtime
|
---|
36 | * @returns {Object} reduced data
|
---|
37 | */
|
---|
38 | const createObjectForExportsInfo = (data, exportsInfo, runtime) => {
|
---|
39 | if (exportsInfo.otherExportsInfo.getUsed(runtime) !== UsageState.Unused)
|
---|
40 | return data;
|
---|
41 | const isArray = Array.isArray(data);
|
---|
42 | const reducedData = isArray ? [] : {};
|
---|
43 | for (const key of Object.keys(data)) {
|
---|
44 | const exportInfo = exportsInfo.getReadOnlyExportInfo(key);
|
---|
45 | const used = exportInfo.getUsed(runtime);
|
---|
46 | if (used === UsageState.Unused) continue;
|
---|
47 |
|
---|
48 | let value;
|
---|
49 | if (used === UsageState.OnlyPropertiesUsed && exportInfo.exportsInfo) {
|
---|
50 | value = createObjectForExportsInfo(
|
---|
51 | data[key],
|
---|
52 | exportInfo.exportsInfo,
|
---|
53 | runtime
|
---|
54 | );
|
---|
55 | } else {
|
---|
56 | value = data[key];
|
---|
57 | }
|
---|
58 | const name = exportInfo.getUsedName(key, runtime);
|
---|
59 | reducedData[name] = value;
|
---|
60 | }
|
---|
61 | if (isArray) {
|
---|
62 | let arrayLengthWhenUsed =
|
---|
63 | exportsInfo.getReadOnlyExportInfo("length").getUsed(runtime) !==
|
---|
64 | UsageState.Unused
|
---|
65 | ? data.length
|
---|
66 | : undefined;
|
---|
67 |
|
---|
68 | let sizeObjectMinusArray = 0;
|
---|
69 | for (let i = 0; i < reducedData.length; i++) {
|
---|
70 | if (reducedData[i] === undefined) {
|
---|
71 | sizeObjectMinusArray -= 2;
|
---|
72 | } else {
|
---|
73 | sizeObjectMinusArray += `${i}`.length + 3;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | if (arrayLengthWhenUsed !== undefined) {
|
---|
77 | sizeObjectMinusArray +=
|
---|
78 | `${arrayLengthWhenUsed}`.length +
|
---|
79 | 8 -
|
---|
80 | (arrayLengthWhenUsed - reducedData.length) * 2;
|
---|
81 | }
|
---|
82 | if (sizeObjectMinusArray < 0)
|
---|
83 | return Object.assign(
|
---|
84 | arrayLengthWhenUsed === undefined
|
---|
85 | ? {}
|
---|
86 | : { length: arrayLengthWhenUsed },
|
---|
87 | reducedData
|
---|
88 | );
|
---|
89 | const generatedLength =
|
---|
90 | arrayLengthWhenUsed !== undefined
|
---|
91 | ? Math.max(arrayLengthWhenUsed, reducedData.length)
|
---|
92 | : reducedData.length;
|
---|
93 | for (let i = 0; i < generatedLength; i++) {
|
---|
94 | if (reducedData[i] === undefined) {
|
---|
95 | reducedData[i] = 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | return reducedData;
|
---|
100 | };
|
---|
101 |
|
---|
102 | const TYPES = new Set(["javascript"]);
|
---|
103 |
|
---|
104 | class JsonGenerator extends Generator {
|
---|
105 | /**
|
---|
106 | * @param {NormalModule} module fresh module
|
---|
107 | * @returns {Set<string>} available types (do not mutate)
|
---|
108 | */
|
---|
109 | getTypes(module) {
|
---|
110 | return TYPES;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @param {NormalModule} module the module
|
---|
115 | * @param {string=} type source type
|
---|
116 | * @returns {number} estimate size of the module
|
---|
117 | */
|
---|
118 | getSize(module, type) {
|
---|
119 | let data =
|
---|
120 | module.buildInfo &&
|
---|
121 | module.buildInfo.jsonData &&
|
---|
122 | module.buildInfo.jsonData.get();
|
---|
123 | if (!data) return 0;
|
---|
124 | return stringifySafe(data).length + 10;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @param {NormalModule} module module for which the bailout reason should be determined
|
---|
129 | * @param {ConcatenationBailoutReasonContext} context context
|
---|
130 | * @returns {string | undefined} reason why this module can't be concatenated, undefined when it can be concatenated
|
---|
131 | */
|
---|
132 | getConcatenationBailoutReason(module, context) {
|
---|
133 | return undefined;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * @param {NormalModule} module module for which the code should be generated
|
---|
138 | * @param {GenerateContext} generateContext context for generate
|
---|
139 | * @returns {Source} generated code
|
---|
140 | */
|
---|
141 | generate(
|
---|
142 | module,
|
---|
143 | {
|
---|
144 | moduleGraph,
|
---|
145 | runtimeTemplate,
|
---|
146 | runtimeRequirements,
|
---|
147 | runtime,
|
---|
148 | concatenationScope
|
---|
149 | }
|
---|
150 | ) {
|
---|
151 | const data =
|
---|
152 | module.buildInfo &&
|
---|
153 | module.buildInfo.jsonData &&
|
---|
154 | module.buildInfo.jsonData.get();
|
---|
155 | if (data === undefined) {
|
---|
156 | return new RawSource(
|
---|
157 | runtimeTemplate.missingModuleStatement({
|
---|
158 | request: module.rawRequest
|
---|
159 | })
|
---|
160 | );
|
---|
161 | }
|
---|
162 | const exportsInfo = moduleGraph.getExportsInfo(module);
|
---|
163 | let finalJson =
|
---|
164 | typeof data === "object" &&
|
---|
165 | data &&
|
---|
166 | exportsInfo.otherExportsInfo.getUsed(runtime) === UsageState.Unused
|
---|
167 | ? createObjectForExportsInfo(data, exportsInfo, runtime)
|
---|
168 | : data;
|
---|
169 | // Use JSON because JSON.parse() is much faster than JavaScript evaluation
|
---|
170 | const jsonStr = stringifySafe(finalJson);
|
---|
171 | const jsonExpr =
|
---|
172 | jsonStr.length > 20 && typeof finalJson === "object"
|
---|
173 | ? `JSON.parse('${jsonStr.replace(/[\\']/g, "\\$&")}')`
|
---|
174 | : jsonStr;
|
---|
175 | let content;
|
---|
176 | if (concatenationScope) {
|
---|
177 | content = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
---|
178 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
---|
179 | } = ${jsonExpr};`;
|
---|
180 | concatenationScope.registerNamespaceExport(
|
---|
181 | ConcatenationScope.NAMESPACE_OBJECT_EXPORT
|
---|
182 | );
|
---|
183 | } else {
|
---|
184 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
185 | content = `${module.moduleArgument}.exports = ${jsonExpr};`;
|
---|
186 | }
|
---|
187 | return new RawSource(content);
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | module.exports = JsonGenerator;
|
---|