getCurrentScriptSource.js 868 B

123456789101112131415161718192021222324
  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. }
  10. // Fallback to getting all scripts running in the document.
  11. var scriptElements = document.scripts || [];
  12. var scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (element) {
  13. return element.getAttribute("src");
  14. });
  15. if (scriptElementsWithSrc.length > 0) {
  16. var currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1];
  17. return currentScript.getAttribute("src");
  18. }
  19. // Fail as there was no script to use.
  20. throw new Error("[webpack-dev-server] Failed to get current script source.");
  21. }
  22. export default getCurrentScriptSource;