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