‘liusuyi’
2023-08-29 510c29eecab77493816d3f0cdb5e5c4462083cc8
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
'use strict';
 
const CHUNK_OPTIONS = ['all', 'initial'];
 
const createResourceHint = require('./resource-hints.js').createResourceHint;
const common = require('./common.js');
const matches = common.matches;
const getScriptName = common.getScriptName;
const getRawScriptName = common.getRawScriptName;
const hasScriptName = common.hasScriptName;
 
const optionsMatch = (option, scriptName) => {
  return matches(option.chunks, CHUNK_OPTIONS) && matches(scriptName, option.test);
};
 
const addInitialChunkResourceHints = (options, tags) => {
  return tags
    .filter(hasScriptName)
    .reduce((hints, tag) => {
      const scriptName = getScriptName(options, tag);
      if (optionsMatch(options.preload, scriptName)) {
        hints.push(createResourceHint('preload', getRawScriptName(tag)));
      } else if (optionsMatch(options.prefetch, scriptName)) {
        hints.push(createResourceHint('prefetch', getRawScriptName(tag)));
      }
      return hints;
    },
    []
    );
};
 
module.exports = addInitialChunkResourceHints;