From 89597aea5c2b4dad197b4e0a15f897f079f5f07d Mon Sep 17 00:00:00 2001 From: Jocelyn Badgley Date: Thu, 17 Mar 2022 09:10:55 -0700 Subject: [PATCH] feat: Add `as` attribute to prefetch tags as well. Includes 92aad438 chore: update resource hints from https://github.com/vuejs/preload-webpack-plugin/pull/27 --- src/index.js | 30 +++++++++++++----------------- test/spec.js | 4 ++-- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/index.js b/src/index.js index 15fa124..fa0af13 100644 --- a/src/index.js +++ b/src/index.js @@ -35,9 +35,9 @@ class PreloadPlugin { }) const htmlChunks = options.include === 'allAssets' - // Handle all chunks. + // Handle all chunks. ? extractedChunks - // Only handle chunks imported by this HtmlWebpackPlugin. + // Only handle chunks imported by this HtmlWebpackPlugin. : extractedChunks.filter((chunk) => doesChunkBelongToHTML({ chunk, compilation, @@ -84,21 +84,17 @@ class PreloadPlugin { rel: options.rel } - // If we're preloading this resource (as opposed to prefetching), - // then we need to set the 'as' attribute correctly. - if (options.rel === 'preload') { - attributes.as = determineAsValue({ - href, - file, - optionsAs: options.as - }) - - // On the off chance that we have a cross-origin 'href' attribute, - // set crossOrigin on the to trigger CORS mode. Non-CORS - // fonts can't be used. - if (attributes.as === 'font') { - attributes.crossorigin = '' - } + attributes.as = determineAsValue({ + href, + file, + optionsAs: options.as + }) + + // On the off chance that we have a cross-origin 'href' attribute, + // set crossOrigin on the to trigger CORS mode. Non-CORS + // fonts can't be used. + if (options.rel === 'preload' && attributes.as === 'font') { + attributes.crossorigin = '' } links.push({ diff --git a/test/spec.js b/test/spec.js index 9a7c371..c37eff2 100644 --- a/test/spec.js +++ b/test/spec.js @@ -400,10 +400,10 @@ module.exports = ({ const links = dom.window.document.head.querySelectorAll('link') expect(links.length).toBe(2) expect(links[0].getAttribute('rel')).toBe('prefetch') - expect(links[0].hasAttribute('as')).toBeFalsy() + expect(links[0].getAttribute('as')).toBe('script') expect(links[0].getAttribute('href')).toBe('home.js') expect(links[1].getAttribute('rel')).toBe('prefetch') - expect(links[1].hasAttribute('as')).toBeFalsy() + expect(links[1].getAttribute('as')).toBe('script') expect(links[1].getAttribute('href')).toBe('main.js') done()