common.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. const debug = require('debug')('ScriptExt');
  3. const separator = '/';
  4. const isScript = (tag) => tag.tagName === 'script';
  5. const hasScriptName = tag => tag.attributes && tag.attributes.src;
  6. const getRawScriptName = tag => (tag.attributes && tag.attributes.src) || '';
  7. const getPublicPath = options => {
  8. const output = options.compilationOptions.output;
  9. if (output) {
  10. let publicPath = output.publicPath;
  11. if (publicPath) {
  12. return publicPath.endsWith(separator) ? publicPath : publicPath + separator;
  13. }
  14. }
  15. };
  16. const getScriptName = (options, tag) => {
  17. let scriptName = getRawScriptName(tag);
  18. const publicPath = getPublicPath(options);
  19. if (publicPath) {
  20. scriptName = scriptName.replace(publicPath, '');
  21. }
  22. if (options.htmlWebpackOptions.hash) {
  23. scriptName = scriptName.split('?', 1)[0];
  24. }
  25. return scriptName;
  26. };
  27. const matches = (toMatch, matchers) => {
  28. return matchers.some((matcher) => {
  29. if (matcher instanceof RegExp) {
  30. return matcher.test(toMatch);
  31. } else {
  32. return toMatch.includes(matcher);
  33. }
  34. });
  35. };
  36. module.exports = {
  37. debug,
  38. getPublicPath,
  39. getRawScriptName,
  40. getScriptName,
  41. hasScriptName,
  42. isScript,
  43. matches
  44. };