collapseFrames.js (1501B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 function collapseLastFrames(frames) { 6 const index = frames.findIndex(frame => 7 frame.location.source.url?.match(/webpack\/bootstrap/i) 8 ); 9 10 if (index == -1) { 11 return { newFrames: frames, lastGroup: [] }; 12 } 13 14 const newFrames = frames.slice(0, index); 15 const lastGroup = frames.slice(index); 16 return { newFrames, lastGroup }; 17 } 18 19 export function collapseFrames(frames) { 20 // We collapse groups of one so that user frames 21 // are not in a group of one 22 function addGroupToList(group, list) { 23 if (!group) { 24 return list; 25 } 26 27 if (group.length > 1) { 28 list.push(group); 29 } else { 30 list = list.concat(group); 31 } 32 33 return list; 34 } 35 const { newFrames, lastGroup } = collapseLastFrames(frames); 36 frames = newFrames; 37 let items = []; 38 let currentGroup = null; 39 let prevItem = null; 40 for (const frame of frames) { 41 const prevLibrary = prevItem?.library; 42 43 if (!currentGroup) { 44 currentGroup = [frame]; 45 } else if (prevLibrary && prevLibrary == frame.library) { 46 currentGroup.push(frame); 47 } else { 48 items = addGroupToList(currentGroup, items); 49 currentGroup = [frame]; 50 } 51 52 prevItem = frame; 53 } 54 55 items = addGroupToList(currentGroup, items); 56 items = addGroupToList(lastGroup, items); 57 return items; 58 }