| 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 makeSerializable = require("../util/makeSerializable");
|
|---|
| 9 | const NullDependency = require("./NullDependency");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../Dependency").ExportSpec} ExportSpec */
|
|---|
| 12 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
|---|
| 13 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 14 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 15 | /** @typedef {import("../json/JsonData")} JsonData */
|
|---|
| 16 | /** @typedef {import("../json/JsonData").JsonValue} JsonValue */
|
|---|
| 17 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 19 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * Defines the get exports from data fn callback.
|
|---|
| 23 | * @callback GetExportsFromDataFn
|
|---|
| 24 | * @param {JsonValue} data raw json data
|
|---|
| 25 | * @param {number=} curDepth current depth
|
|---|
| 26 | * @returns {ExportSpec[] | null} export spec or nothing
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Gets exports with depth.
|
|---|
| 31 | * @param {number} exportsDepth exportsDepth
|
|---|
| 32 | * @returns {GetExportsFromDataFn} value
|
|---|
| 33 | */
|
|---|
| 34 | const getExportsWithDepth = (exportsDepth) =>
|
|---|
| 35 | /** @type {GetExportsFromDataFn} */
|
|---|
| 36 | function getExportsFromData(data, curDepth = 1) {
|
|---|
| 37 | if (curDepth > exportsDepth) {
|
|---|
| 38 | return null;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | if (data && typeof data === "object") {
|
|---|
| 42 | if (Array.isArray(data)) {
|
|---|
| 43 | return data.length < 100
|
|---|
| 44 | ? data.map((item, idx) => ({
|
|---|
| 45 | name: `${idx}`,
|
|---|
| 46 | canMangle: true,
|
|---|
| 47 | exports: getExportsFromData(item, curDepth + 1) || undefined
|
|---|
| 48 | }))
|
|---|
| 49 | : null;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /** @type {ExportSpec[]} */
|
|---|
| 53 | const exports = [];
|
|---|
| 54 |
|
|---|
| 55 | for (const key of Object.keys(data)) {
|
|---|
| 56 | exports.push({
|
|---|
| 57 | name: key,
|
|---|
| 58 | canMangle: true,
|
|---|
| 59 | exports:
|
|---|
| 60 | getExportsFromData(
|
|---|
| 61 | /** @type {JsonValue} */
|
|---|
| 62 | (data[key]),
|
|---|
| 63 | curDepth + 1
|
|---|
| 64 | ) || undefined
|
|---|
| 65 | });
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | return exports;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | return null;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | class JsonExportsDependency extends NullDependency {
|
|---|
| 75 | /**
|
|---|
| 76 | * Creates an instance of JsonExportsDependency.
|
|---|
| 77 | * @param {JsonData} data json data
|
|---|
| 78 | * @param {number} exportsDepth the depth of json exports to analyze
|
|---|
| 79 | */
|
|---|
| 80 | constructor(data, exportsDepth) {
|
|---|
| 81 | super();
|
|---|
| 82 | this.data = data;
|
|---|
| 83 | this.exportsDepth = exportsDepth;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | get type() {
|
|---|
| 87 | return "json exports";
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Returns the exported names
|
|---|
| 92 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 93 | * @returns {ExportsSpec | undefined} export names
|
|---|
| 94 | */
|
|---|
| 95 | getExports(moduleGraph) {
|
|---|
| 96 | return {
|
|---|
| 97 | exports: getExportsWithDepth(this.exportsDepth)(
|
|---|
| 98 | this.data && /** @type {JsonValue} */ (this.data.get())
|
|---|
| 99 | ),
|
|---|
| 100 | dependencies: undefined
|
|---|
| 101 | };
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Updates the hash with the data contributed by this instance.
|
|---|
| 106 | * @param {Hash} hash hash to be updated
|
|---|
| 107 | * @param {UpdateHashContext} context context
|
|---|
| 108 | * @returns {void}
|
|---|
| 109 | */
|
|---|
| 110 | updateHash(hash, context) {
|
|---|
| 111 | this.data.updateHash(hash);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * Serializes this instance into the provided serializer context.
|
|---|
| 116 | * @param {ObjectSerializerContext} context context
|
|---|
| 117 | */
|
|---|
| 118 | serialize(context) {
|
|---|
| 119 | const { write } = context;
|
|---|
| 120 | write(this.data);
|
|---|
| 121 | write(this.exportsDepth);
|
|---|
| 122 | super.serialize(context);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Restores this instance from the provided deserializer context.
|
|---|
| 127 | * @param {ObjectDeserializerContext} context context
|
|---|
| 128 | */
|
|---|
| 129 | deserialize(context) {
|
|---|
| 130 | const { read } = context;
|
|---|
| 131 | this.data = read();
|
|---|
| 132 | this.exportsDepth = read();
|
|---|
| 133 | super.deserialize(context);
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | makeSerializable(
|
|---|
| 138 | JsonExportsDependency,
|
|---|
| 139 | "webpack/lib/dependencies/JsonExportsDependency"
|
|---|
| 140 | );
|
|---|
| 141 |
|
|---|
| 142 | module.exports = JsonExportsDependency;
|
|---|