Skip to content

chore: update resource hints #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ 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,
optionsInclude: options.include
})

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,
Expand Down Expand Up @@ -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 <link> 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 <link> to trigger CORS mode. Non-CORS
// fonts can't be used.
if (options.rel === 'preload' && attributes.as === 'font') {
attributes.crossorigin = ''
}

links.push({
Expand All @@ -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]
Expand Down