getCurrentScriptSource.js 744 B

12345678910111213141516171819202122
  1. /**
  2. * Gets the source (i.e. host) of the script currently running.
  3. * @returns {string}
  4. */
  5. function getCurrentScriptSource() {
  6. // `document.currentScript` is the most accurate way to get the current running script,
  7. // but is not supported in all browsers (most notably, IE).
  8. if (document.currentScript) {
  9. return document.currentScript.getAttribute('src');
  10. }
  11. // Fallback to getting all scripts running in the document.
  12. const scriptElements = document.scripts || [];
  13. const currentScript = scriptElements[scriptElements.length - 1];
  14. if (currentScript) {
  15. return currentScript.getAttribute('src');
  16. }
  17. throw new Error('[React Refresh] Failed to get current script source!');
  18. }
  19. module.exports = getCurrentScriptSource;