initial-chunk-resource-hints.js 1.0 KB

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