getLibraryFromUrl.js (2800B)
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 const libraryMap = [ 6 { 7 label: "Backbone", 8 pattern: /backbone/i, 9 }, 10 { 11 label: "Babel", 12 pattern: /node_modules\/@babel/i, 13 }, 14 { 15 label: "jQuery", 16 pattern: /jquery/i, 17 }, 18 { 19 label: "Preact", 20 pattern: /preact/i, 21 }, 22 { 23 label: "React", 24 pattern: 25 /(node_modules\/(?:react(-dom)?(-dev)?\/))|(react(-dom)?(-dev)?(\.[a-z]+)*\.js$)/, 26 }, 27 { 28 label: "Immutable", 29 pattern: /immutable/i, 30 }, 31 { 32 label: "Webpack", 33 pattern: /webpack\/bootstrap/i, 34 }, 35 { 36 label: "Express", 37 pattern: /node_modules\/express/, 38 }, 39 { 40 label: "Pug", 41 pattern: /node_modules\/pug/, 42 }, 43 { 44 label: "ExtJS", 45 pattern: /\/ext-all[\.\-]/, 46 }, 47 { 48 label: "MobX", 49 pattern: /mobx/i, 50 }, 51 { 52 label: "Underscore", 53 pattern: /underscore/i, 54 }, 55 { 56 label: "Lodash", 57 pattern: /lodash/i, 58 }, 59 { 60 label: "Ember", 61 pattern: /ember/i, 62 }, 63 { 64 label: "VueJS", 65 pattern: /vue(?:\.[a-z]+)*\.js/i, 66 }, 67 { 68 label: "RxJS", 69 pattern: /rxjs/i, 70 }, 71 { 72 label: "Angular", 73 pattern: /angular(?!.*\/app\/)/i, 74 contextPattern: /zone\.js/, 75 }, 76 { 77 label: "Redux", 78 pattern: /redux/i, 79 }, 80 { 81 label: "Dojo", 82 pattern: /dojo/i, 83 }, 84 { 85 label: "Marko", 86 pattern: /marko/i, 87 }, 88 { 89 label: "NuxtJS", 90 pattern: /[\._]nuxt/i, 91 }, 92 { 93 label: "Aframe", 94 pattern: /aframe/i, 95 }, 96 { 97 label: "NextJS", 98 pattern: /[\._]next/i, 99 }, 100 ]; 101 102 export function getLibraryFromUrl(frame, callStack = []) { 103 const frameUrl = frame.location.source.url; 104 105 // Let's first check if the frame match a defined pattern. 106 let match = libraryMap.find(o => o.pattern.test(frameUrl)); 107 if (match) { 108 return match.label; 109 } 110 111 // If it does not, it might still be one of the case where the file is used 112 // by a library but the name has not enough specificity. In such case, we want 113 // to only return the library name if there are frames matching the library 114 // pattern in the callStack (e.g. `zone.js` is used by Angular, but the name 115 // could be quite common and return false positive if evaluated alone. So we 116 // only return Angular if there are other frames matching Angular). 117 match = libraryMap.find( 118 o => o.contextPattern && o.contextPattern.test(frameUrl) 119 ); 120 if (match) { 121 const contextMatch = callStack.some(f => { 122 const url = f.location.source.url; 123 if (!url) { 124 return false; 125 } 126 127 return libraryMap.some(o => o.pattern.test(url)); 128 }); 129 130 if (contextMatch) { 131 return match.label; 132 } 133 } 134 135 return null; 136 }