1 | 'use strict';
|
2 |
|
3 | const CHUNK_OPTIONS = ['all', 'initial'];
|
4 |
|
5 | const createResourceHint = require('./resource-hints.js').createResourceHint;
|
6 | const common = require('./common.js');
|
7 | const matches = common.matches;
|
8 | const getScriptName = common.getScriptName;
|
9 | const getRawScriptName = common.getRawScriptName;
|
10 | const hasScriptName = common.hasScriptName;
|
11 |
|
12 | const optionsMatch = (option, scriptName) => {
|
13 | return matches(option.chunks, CHUNK_OPTIONS) && matches(scriptName, option.test);
|
14 | };
|
15 |
|
16 | const addInitialChunkResourceHints = (options, tags) => {
|
17 | return tags
|
18 | .filter(hasScriptName)
|
19 | .reduce((hints, tag) => {
|
20 | const scriptName = getScriptName(options, tag);
|
21 | if (optionsMatch(options.preload, scriptName)) {
|
22 | hints.push(createResourceHint('preload', getRawScriptName(tag)));
|
23 | } else if (optionsMatch(options.prefetch, scriptName)) {
|
24 | hints.push(createResourceHint('prefetch', getRawScriptName(tag)));
|
25 | }
|
26 | return hints;
|
27 | },
|
28 | []
|
29 | );
|
30 | };
|
31 |
|
32 | module.exports = addInitialChunkResourceHints;
|