From 81a198429b9590c8242843974c606c2d7f4ee143 Mon Sep 17 00:00:00 2001 From: moyuboy Date: Tue, 23 Oct 2018 22:49:31 +0800 Subject: [PATCH] add custom attrs support for preload/prefetch --- lib/async-chunk-resource-hints.js | 4 ++-- lib/config.js | 5 +++-- lib/initial-chunk-resource-hints.js | 4 ++-- lib/resource-hints.js | 6 +++--- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/async-chunk-resource-hints.js b/lib/async-chunk-resource-hints.js index a67c232..963fdbf 100644 --- a/lib/async-chunk-resource-hints.js +++ b/lib/async-chunk-resource-hints.js @@ -16,9 +16,9 @@ const addAsyncChunkResourceHints = (chunks, options) => { []) .forEach(file => { if (optionsMatch(options.preload, file)) { - hints.push(createResourceHint('preload', getRef(file))); + hints.push(createResourceHint('preload', getRef(file), options.preload.attrs)); } else if (optionsMatch(options.prefetch, file)) { - hints.push(createResourceHint('prefetch', getRef(file))); + hints.push(createResourceHint('prefetch', getRef(file), options.prefetch.attrs)); } }); return hints; diff --git a/lib/config.js b/lib/config.js index 3d9e003..7b2f967 100644 --- a/lib/config.js +++ b/lib/config.js @@ -7,7 +7,8 @@ const DEFAULT_HASH = { }; const DEFAULT_RESOURCE_HINT_HASH = { test: [], - chunks: 'initial' + chunks: 'initial', + attrs: {} }; const DEFAULT_CUSTOM_HASH = { test: [], @@ -26,7 +27,7 @@ const DEFAULT_OPTIONS = { removeInlinedAssets: true, custom: [] }; -const POSSIBLE_VALUES = ['chunks', 'attribute', 'value']; +const POSSIBLE_VALUES = ['chunks', 'attribute', 'value', 'attrs']; const normaliseOptions = options => { if (!options) return DEFAULT_OPTIONS; diff --git a/lib/initial-chunk-resource-hints.js b/lib/initial-chunk-resource-hints.js index 8421950..a7c874d 100644 --- a/lib/initial-chunk-resource-hints.js +++ b/lib/initial-chunk-resource-hints.js @@ -19,9 +19,9 @@ const addInitialChunkResourceHints = (options, tags) => { .reduce((hints, tag) => { const scriptName = getScriptName(options, tag); if (optionsMatch(options.preload, scriptName)) { - hints.push(createResourceHint('preload', getRawScriptName(tag))); + hints.push(createResourceHint('preload', getRawScriptName(tag), options.preload.attrs)); } else if (optionsMatch(options.prefetch, scriptName)) { - hints.push(createResourceHint('prefetch', getRawScriptName(tag))); + hints.push(createResourceHint('prefetch', getRawScriptName(tag), options.prefetch.attrs)); } return hints; }, diff --git a/lib/resource-hints.js b/lib/resource-hints.js index 448a790..e47751e 100644 --- a/lib/resource-hints.js +++ b/lib/resource-hints.js @@ -5,15 +5,15 @@ const shouldAddResourceHints = options => { options.preload.test.length === 0); }; -const createResourceHint = (rel, href) => { +const createResourceHint = (rel, href, attrs) => { return { tagName: 'link', selfClosingTag: true, - attributes: { + attributes: Object.assign({}, attrs, { rel: rel, href: href, as: 'script' - } + }) }; };