|
4 | 4 |
|
5 | 5 | import { INJECTED_FILE } from '@dd/core/constants';
|
6 | 6 | import { isInjectionFile } from '@dd/core/helpers';
|
7 |
| -import type { |
8 |
| - BuildReport, |
9 |
| - SerializedEntry, |
10 |
| - File, |
11 |
| - GlobalContext, |
12 |
| - SerializedInput, |
13 |
| - SerializedBuildReport, |
14 |
| - SerializedOutput, |
15 |
| - Entry, |
16 |
| - Input, |
17 |
| - Output, |
18 |
| -} from '@dd/core/types'; |
| 7 | +import type { GlobalContext } from '@dd/core/types'; |
19 | 8 | import path from 'path';
|
20 | 9 |
|
21 | 10 | // Will match any last part of a path after a dot or slash and is a word character.
|
@@ -43,158 +32,6 @@ export const getType = (name: string): string => {
|
43 | 32 | return getExtension(cleanPath(name)) || 'unknown';
|
44 | 33 | };
|
45 | 34 |
|
46 |
| -// Returns an object that is safe to serialize to JSON. |
47 |
| -// Mostly useful for debugging and testing. |
48 |
| -export const serializeBuildReport = (report: BuildReport): SerializedBuildReport => { |
49 |
| - // Report is an object that self reference some of its values. |
50 |
| - // To make it JSON serializable, we need to remove the self references |
51 |
| - // and replace them with strings, we'll use "filepath" to still have them uniquely identifiable. |
52 |
| - const jsonReport: SerializedBuildReport = { |
53 |
| - bundler: report.bundler, |
54 |
| - errors: report.errors, |
55 |
| - warnings: report.warnings, |
56 |
| - logs: report.logs, |
57 |
| - start: report.start, |
58 |
| - end: report.end, |
59 |
| - duration: report.duration, |
60 |
| - writeDuration: report.writeDuration, |
61 |
| - entries: [], |
62 |
| - inputs: [], |
63 |
| - outputs: [], |
64 |
| - }; |
65 |
| - |
66 |
| - for (const entry of report.entries || []) { |
67 |
| - const newEntry: SerializedEntry = { ...entry, inputs: [], outputs: [] }; |
68 |
| - if (entry.inputs) { |
69 |
| - newEntry.inputs = entry.inputs.map((file: File) => file.filepath); |
70 |
| - } |
71 |
| - if (entry.outputs) { |
72 |
| - newEntry.outputs = entry.outputs.map((file: File) => file.filepath); |
73 |
| - } |
74 |
| - jsonReport.entries.push(newEntry); |
75 |
| - } |
76 |
| - |
77 |
| - for (const input of report.inputs || []) { |
78 |
| - const newInput: SerializedInput = { ...input, dependencies: [], dependents: [] }; |
79 |
| - if (input.dependencies) { |
80 |
| - for (const dependency of input.dependencies) { |
81 |
| - newInput.dependencies.push(dependency.filepath); |
82 |
| - } |
83 |
| - } |
84 |
| - if (input.dependents) { |
85 |
| - for (const dependent of input.dependents) { |
86 |
| - newInput.dependents.push(dependent.filepath); |
87 |
| - } |
88 |
| - } |
89 |
| - jsonReport.inputs.push(newInput); |
90 |
| - } |
91 |
| - |
92 |
| - for (const output of report.outputs || []) { |
93 |
| - const newOutput: SerializedOutput = { ...output, inputs: [] }; |
94 |
| - if (output.inputs) { |
95 |
| - newOutput.inputs = output.inputs.map((file: File) => file.filepath); |
96 |
| - } |
97 |
| - jsonReport.outputs.push(newOutput); |
98 |
| - } |
99 |
| - |
100 |
| - return jsonReport; |
101 |
| -}; |
102 |
| - |
103 |
| -// Returns an object that is unserialized from serializeBuildReport(). |
104 |
| -// Mostly useful for debugging and testing. |
105 |
| -export const unserializeBuildReport = (report: SerializedBuildReport): BuildReport => { |
106 |
| - const buildReport: BuildReport = { |
107 |
| - bundler: report.bundler, |
108 |
| - errors: report.errors, |
109 |
| - warnings: report.warnings, |
110 |
| - logs: report.logs, |
111 |
| - start: report.start, |
112 |
| - end: report.end, |
113 |
| - duration: report.duration, |
114 |
| - writeDuration: report.writeDuration, |
115 |
| - }; |
116 |
| - |
117 |
| - const reportInputs = report.inputs || []; |
118 |
| - const reportOutputs = report.outputs || []; |
119 |
| - |
120 |
| - const entries: Entry[] = []; |
121 |
| - |
122 |
| - // Prefill inputs and outputs as they are sometimes self-referencing themselves. |
123 |
| - const indexedInputs: Map<string, Input> = new Map(); |
124 |
| - const inputs: Input[] = reportInputs.map<Input>((input) => { |
125 |
| - const newInput: Input = { |
126 |
| - ...input, |
127 |
| - // Keep them empty for now, we'll fill them later. |
128 |
| - dependencies: new Set(), |
129 |
| - dependents: new Set(), |
130 |
| - }; |
131 |
| - indexedInputs.set(input.filepath, newInput); |
132 |
| - return newInput; |
133 |
| - }); |
134 |
| - |
135 |
| - const indexedOutputs: Map<string, Output> = new Map(); |
136 |
| - const outputs: Output[] = reportOutputs.map<Output>((output) => { |
137 |
| - const newOutput: Output = { ...output, inputs: [] }; |
138 |
| - indexedOutputs.set(output.filepath, newOutput); |
139 |
| - return newOutput; |
140 |
| - }); |
141 |
| - |
142 |
| - // Fill in the inputs' dependencies and dependents. |
143 |
| - for (const input of reportInputs) { |
144 |
| - const newInput: Input = indexedInputs.get(input.filepath)!; |
145 |
| - |
146 |
| - // Re-assign the dependencies and dependents to the actual objects. |
147 |
| - if (input.dependencies) { |
148 |
| - for (const dependency of input.dependencies) { |
149 |
| - const newDependency = indexedInputs.get(dependency)!; |
150 |
| - newInput.dependencies.add(newDependency); |
151 |
| - } |
152 |
| - } |
153 |
| - if (input.dependents) { |
154 |
| - for (const dependent of input.dependents) { |
155 |
| - const newDependent = indexedInputs.get(dependent)!; |
156 |
| - newInput.dependents.add(newDependent); |
157 |
| - } |
158 |
| - } |
159 |
| - } |
160 |
| - |
161 |
| - // Fill in the outputs' inputs. |
162 |
| - for (const output of reportOutputs) { |
163 |
| - const newOutput: Output = indexedOutputs.get(output.filepath)!; |
164 |
| - if (output.inputs) { |
165 |
| - // Re-assign the inputs to the actual objects. |
166 |
| - newOutput.inputs = output.inputs |
167 |
| - .map< |
168 |
| - // Can be either an input or an output (for sourcemaps). |
169 |
| - Input | Output | undefined |
170 |
| - >((filepath: string) => indexedInputs.get(filepath) || indexedOutputs.get(filepath)) |
171 |
| - .filter(Boolean) as (Input | Output)[]; |
172 |
| - } |
173 |
| - } |
174 |
| - |
175 |
| - for (const entry of report.entries || []) { |
176 |
| - const newEntry: Entry = { ...entry, inputs: [], outputs: [] }; |
177 |
| - if (entry.inputs) { |
178 |
| - newEntry.inputs = entry.inputs |
179 |
| - .map((filepath: string) => indexedInputs.get(filepath)) |
180 |
| - .filter(Boolean) as (Output | Input)[]; |
181 |
| - } |
182 |
| - if (entry.outputs) { |
183 |
| - newEntry.outputs = entry.outputs |
184 |
| - .map((filepath: string) => indexedOutputs.get(filepath)) |
185 |
| - .filter(Boolean) as Output[]; |
186 |
| - } |
187 |
| - entries.push(newEntry); |
188 |
| - } |
189 |
| - |
190 |
| - return { |
191 |
| - ...buildReport, |
192 |
| - entries, |
193 |
| - inputs, |
194 |
| - outputs, |
195 |
| - }; |
196 |
| -}; |
197 |
| - |
198 | 35 | const BUNDLER_SPECIFICS = ['unknown', 'commonjsHelpers.js', 'vite/preload-helper.js'];
|
199 | 36 | // Make list of paths unique, remove the current file and particularities.
|
200 | 37 | export const cleanReport = <T = string>(
|
|
0 commit comments