getCurrentScriptSource.js 868 B

1234567891011121314151617181920212223242526
  1. /**
  2. * @returns {string}
  3. */
  4. function getCurrentScriptSource() {
  5. // `document.currentScript` is the most accurate way to find the current script,
  6. // but is not supported in all browsers.
  7. if (document.currentScript) {
  8. return document.currentScript.getAttribute("src");
  9. } // Fallback to getting all scripts running in the document.
  10. var scriptElements = document.scripts || [];
  11. var scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (element) {
  12. return element.getAttribute("src");
  13. });
  14. if (scriptElementsWithSrc.length > 0) {
  15. var currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1];
  16. return currentScript.getAttribute("src");
  17. } // Fail as there was no script to use.
  18. throw new Error("[webpack-dev-server] Failed to get current script source.");
  19. }
  20. export default getCurrentScriptSource;