From 92aad438c664cc8e42f8bce99f566daa6aefc2fb Mon Sep 17 00:00:00 2001 From: 2heal1 Date: Mon, 6 Sep 2021 23:19:08 +0800 Subject: [PATCH] chore: update resource hints --- src/index.js | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index 4706359..faecd1c 100644 --- a/src/index.js +++ b/src/index.js @@ -21,12 +21,12 @@ const doesChunkBelongToHTML = require('./lib/does-chunk-belong-to-html') const extractChunks = require('./lib/extract-chunks') class PreloadPlugin { - constructor (options) { + constructor(options) { this.options = Object.assign({}, defaultOptions, options) this.webpackMajorVersion = 4 } - generateLinks (compilation, htmlPluginData) { + generateLinks(compilation, htmlPluginData) { const options = this.options const extractedChunks = extractChunks({ compilation, @@ -34,9 +34,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, @@ -83,21 +83,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({ @@ -110,7 +106,7 @@ class PreloadPlugin { return htmlPluginData } - apply (compiler) { + apply(compiler) { // for webpack5+, we can get webpack version from `compiler.webpack` if (compiler.webpack) { this.webpackMajorVersion = compiler.webpack.version.split('.')[0]