zhangjian
2023-06-05 0976d2d0f90cff460cedfdc8bd74e98c2c31a58c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'use strict';
 
const debug = require('debug')('ScriptExt');
const separator = '/';
 
const isScript = (tag) => tag.tagName === 'script';
 
const isResourceLink = (tag) => tag.tagName === 'link' && tag.attributes && tag.attributes.as === 'script';
 
const hasScriptName = tag => {
  if (isScript(tag)) {
    return tag.attributes && tag.attributes.src;
  } else if (isResourceLink(tag)) {
    return tag.attributes && tag.attributes.href;
  } else {
    return false;
  }
};
 
const getRawScriptName = tag => {
  if (isScript(tag)) {
    return (tag.attributes && tag.attributes.src) || '';
  } else if (isResourceLink(tag)) {
    return (tag.attributes && tag.attributes.href) || '';
  } else {
    return '';
  }
};
 
const getPublicPath = options => {
  const output = options.compilationOptions.output;
  if (output) {
    const publicPath = output.publicPath;
    if (publicPath) {
      return publicPath.endsWith(separator) ? publicPath : publicPath + separator;
    }
  }
};
 
const getScriptName = (options, tag) => {
  let scriptName = getRawScriptName(tag);
  const publicPath = getPublicPath(options);
  if (publicPath) {
    scriptName = scriptName.replace(publicPath, '');
  }
  if (options.htmlWebpackOptions.hash) {
    scriptName = scriptName.split('?', 1)[0];
  }
  return scriptName;
};
 
const matches = (toMatch, matchers) => {
  return matchers.some((matcher) => {
    if (matcher instanceof RegExp) {
      return matcher.test(toMatch);
    } else {
      return toMatch.includes(matcher);
    }
  });
};
 
module.exports = {
  debug,
  getPublicPath,
  getRawScriptName,
  getScriptName,
  hasScriptName,
  isResourceLink,
  isScript,
  matches
};