-
-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c2d0ca
commit a39f961
Showing
4 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import info from '@angular/compiler/package.json'; | ||
|
||
const url = new URL('.', __system_context__.meta.url); | ||
|
||
if (typeof window.ngVersions === 'undefined') { | ||
window.ngVersions = {}; | ||
} | ||
|
||
window.ngVersions[url.href] = info.version; | ||
|
||
const existing = Object.getOwnPropertyDescriptor(window, 'ng'); | ||
|
||
if (existing?.get === undefined) { | ||
const defaultVersion = 'default'; | ||
const ngs = { | ||
[defaultVersion]: existing?.value, | ||
}; | ||
|
||
function getUrl() { | ||
const { stack } = new Error(); | ||
const lines = stack?.split('\n') || []; | ||
|
||
if (lines[0] === 'Error') { | ||
// V8 | ||
const line = lines[3] || ''; | ||
return /\((.*):\d+:\d+\)$/.exec(line)?.[1] || ''; | ||
} else { | ||
// SpiderMonkey and JavaScriptCore | ||
const line = lines[2] || ''; | ||
return /@(.*):\d+:\d+$/.exec(line)?.[1] || ''; | ||
} | ||
} | ||
|
||
function getNgVersion(url) { | ||
try { | ||
const baseUrl = new URL('.', url); | ||
const version = window.ngVersions[baseUrl]; | ||
return version || defaultVersion; | ||
} catch { | ||
return defaultVersion; | ||
} | ||
} | ||
|
||
Object.defineProperty(window, 'ng', { | ||
configurable: true, | ||
get() { | ||
const url = getUrl(); | ||
const version = getNgVersion(url); | ||
return ngs[version]; | ||
}, | ||
set(value) { | ||
const url = getUrl(); | ||
const version = getNgVersion(url); | ||
ngs[version] = value; | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters