UNPKG

3.15 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const { runtimeToString, RuntimeSpecMap } = require("./util/runtime");
9
10/** @typedef {import("webpack-sources").Source} Source */
11/** @typedef {import("./Module")} Module */
12/** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
13/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
14
15class CodeGenerationResults {
16 constructor() {
17 /** @type {Map<Module, RuntimeSpecMap<CodeGenerationResult>>} */
18 this.map = new Map();
19 }
20
21 /**
22 * @param {Module} module the module
23 * @param {RuntimeSpec} runtime runtime(s)
24 * @returns {CodeGenerationResult} the CodeGenerationResult
25 */
26 get(module, runtime) {
27 const entry = this.map.get(module);
28 if (entry === undefined) {
29 throw new Error(
30 `No code generation entry for ${module.identifier()} (existing entries: ${Array.from(
31 this.map.keys(),
32 m => m.identifier()
33 ).join(", ")})`
34 );
35 }
36 if (runtime === undefined) {
37 const results = new Set(entry.values());
38 if (results.size !== 1) {
39 throw new Error(
40 `No unique code generation entry for unspecified runtime for ${module.identifier()} (existing runtimes: ${Array.from(
41 entry.keys(),
42 r => runtimeToString(r)
43 ).join(", ")}).
44Caller might not support runtime-dependent code generation (opt-out via optimization.usedExports: "global").`
45 );
46 }
47 return results.values().next().value;
48 } else {
49 const result = entry.get(runtime);
50 if (result === undefined) {
51 throw new Error(
52 `No code generation entry for runtime ${runtimeToString(
53 runtime
54 )} for ${module.identifier()} (existing runtimes: ${Array.from(
55 entry.keys(),
56 r => runtimeToString(r)
57 ).join(", ")})`
58 );
59 }
60 return result;
61 }
62 }
63
64 /**
65 * @param {Module} module the module
66 * @param {RuntimeSpec} runtime runtime(s)
67 * @param {string} sourceType the source type
68 * @returns {Source} a source
69 */
70 getSource(module, runtime, sourceType) {
71 return this.get(module, runtime).sources.get(sourceType);
72 }
73
74 /**
75 * @param {Module} module the module
76 * @param {RuntimeSpec} runtime runtime(s)
77 * @returns {ReadonlySet<string>} runtime requirements
78 */
79 getRuntimeRequirements(module, runtime) {
80 return this.get(module, runtime).runtimeRequirements;
81 }
82
83 /**
84 * @param {Module} module the module
85 * @param {RuntimeSpec} runtime runtime(s)
86 * @param {string} key data key
87 * @returns {any} data generated by code generation
88 */
89 getData(module, runtime, key) {
90 const data = this.get(module, runtime).data;
91 return data === undefined ? undefined : data.get(key);
92 }
93
94 /**
95 * @param {Module} module the module
96 * @param {RuntimeSpec} runtime runtime(s)
97 * @param {CodeGenerationResult} result result from module
98 * @returns {void}
99 */
100 add(module, runtime, result) {
101 const map = this.map.get(module);
102 if (map !== undefined) {
103 map.set(runtime, result);
104 } else {
105 const newMap = new RuntimeSpecMap();
106 newMap.set(runtime, result);
107 this.map.set(module, newMap);
108 }
109 }
110}
111
112module.exports = CodeGenerationResults;