[79a0317] | 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("../ChunkGraph")} ChunkGraph */
|
---|
| 12 | /** @typedef {import("../Dependency").ExportSpec} ExportSpec */
|
---|
| 13 | /** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
|
---|
| 14 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 15 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 16 | /** @typedef {import("../json/JsonData")} JsonData */
|
---|
| 17 | /** @typedef {import("../json/JsonData").RawJsonData} RawJsonData */
|
---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 20 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * @param {RawJsonData} data data
|
---|
| 24 | * @returns {TODO} value
|
---|
| 25 | */
|
---|
| 26 | const getExportsFromData = data => {
|
---|
| 27 | if (data && typeof data === "object") {
|
---|
| 28 | if (Array.isArray(data)) {
|
---|
| 29 | return data.length < 100
|
---|
| 30 | ? data.map((item, idx) => ({
|
---|
| 31 | name: `${idx}`,
|
---|
| 32 | canMangle: true,
|
---|
| 33 | exports: getExportsFromData(item)
|
---|
| 34 | }))
|
---|
| 35 | : undefined;
|
---|
| 36 | }
|
---|
| 37 | const exports = [];
|
---|
| 38 | for (const key of Object.keys(data)) {
|
---|
| 39 | exports.push({
|
---|
| 40 | name: key,
|
---|
| 41 | canMangle: true,
|
---|
| 42 | exports: getExportsFromData(data[key])
|
---|
| 43 | });
|
---|
| 44 | }
|
---|
| 45 | return exports;
|
---|
| 46 | }
|
---|
| 47 | return undefined;
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | class JsonExportsDependency extends NullDependency {
|
---|
| 51 | /**
|
---|
| 52 | * @param {JsonData} data json data
|
---|
| 53 | */
|
---|
| 54 | constructor(data) {
|
---|
| 55 | super();
|
---|
| 56 | this.data = data;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | get type() {
|
---|
| 60 | return "json exports";
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | /**
|
---|
| 64 | * Returns the exported names
|
---|
| 65 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 66 | * @returns {ExportsSpec | undefined} export names
|
---|
| 67 | */
|
---|
| 68 | getExports(moduleGraph) {
|
---|
| 69 | return {
|
---|
| 70 | exports: getExportsFromData(
|
---|
| 71 | this.data && /** @type {RawJsonData} */ (this.data.get())
|
---|
| 72 | ),
|
---|
| 73 | dependencies: undefined
|
---|
| 74 | };
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Update the hash
|
---|
| 79 | * @param {Hash} hash hash to be updated
|
---|
| 80 | * @param {UpdateHashContext} context context
|
---|
| 81 | * @returns {void}
|
---|
| 82 | */
|
---|
| 83 | updateHash(hash, context) {
|
---|
| 84 | this.data.updateHash(hash);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * @param {ObjectSerializerContext} context context
|
---|
| 89 | */
|
---|
| 90 | serialize(context) {
|
---|
| 91 | const { write } = context;
|
---|
| 92 | write(this.data);
|
---|
| 93 | super.serialize(context);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /**
|
---|
| 97 | * @param {ObjectDeserializerContext} context context
|
---|
| 98 | */
|
---|
| 99 | deserialize(context) {
|
---|
| 100 | const { read } = context;
|
---|
| 101 | this.data = read();
|
---|
| 102 | super.deserialize(context);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | makeSerializable(
|
---|
| 107 | JsonExportsDependency,
|
---|
| 108 | "webpack/lib/dependencies/JsonExportsDependency"
|
---|
| 109 | );
|
---|
| 110 |
|
---|
| 111 | module.exports = JsonExportsDependency;
|
---|