From fbc1c986a16e59ec1d36302d3cb47bb1ee9704d8 Mon Sep 17 00:00:00 2001 From: Bayram Ali Basgul Date: Sun, 2 Apr 2023 03:03:51 +0200 Subject: [PATCH 1/2] Feature for returning the links as array for easier manipulation --- src/Types.ts | 6 ++++++ src/index.ts | 5 +++++ test/utils/TestUtil.ts | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Types.ts b/src/Types.ts index e6e28d6..ce9f9a5 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -16,6 +16,11 @@ export interface ReplaceCallbackOptions { * `` tags for preloading fonts as a string */ linksAsString: string; + + /** + * `` tags for preloading fonts as an array of strings + */ + linksAsArray: string[]; } /** @@ -81,6 +86,7 @@ export interface PluginOptions { replaceCallback?: ({ indexSource, linksAsString, + linksAsArray, }: ReplaceCallbackOptions) => string; /** diff --git a/src/index.ts b/src/index.ts index 9f01811..1703589 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,9 +67,13 @@ export default class WebpackFontPreloadPlugin { const publicPath = (outputOptions && outputOptions.publicPath) || ""; if (indexSource) { let strLink = ""; + const linksAsArray: string[] = []; assets.forEach((asset) => { if (this.isFontAsset(asset.name) && this.isFiltered(asset.name)) { strLink += this.getLinkTag(asset.name, publicPath.toString()); + linksAsArray.push( + this.getLinkTag(asset.name, publicPath.toString()) + ); } }); // If `replaceCallback` is specified then app is responsible to forming the updated @@ -80,6 +84,7 @@ export default class WebpackFontPreloadPlugin { this.options.replaceCallback({ indexSource: indexSource.toString(), linksAsString: strLink, + linksAsArray, }) ); } else { diff --git a/test/utils/TestUtil.ts b/test/utils/TestUtil.ts index 3848608..e05500b 100644 --- a/test/utils/TestUtil.ts +++ b/test/utils/TestUtil.ts @@ -161,7 +161,7 @@ export function areValidFonts( validExtensions: string[] ): Promise { const expression = /(?:\.([^.]+))?$/; - const errors = []; + const errors: string[] = []; if (fonts.length === 0) { return Promise.reject(["No font's are preloaded in the output."]); } From d66b4299b545d240dd25af070ce168885c829bf6 Mon Sep 17 00:00:00 2001 From: Bayram Ali Basgul Date: Sun, 2 Apr 2023 03:48:14 +0200 Subject: [PATCH 2/2] Extended the functionality to also inlcude structured data of links --- src/Types.ts | 30 ++++++++++++++++++++++++++++++ src/index.ts | 21 ++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/Types.ts b/src/Types.ts index ce9f9a5..5ceb9c0 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -21,6 +21,11 @@ export interface ReplaceCallbackOptions { * `` tags for preloading fonts as an array of strings */ linksAsArray: string[]; + + /** + * Link data for building a `` tag + */ + linksAsStructuredData: LinkData[]; } /** @@ -87,6 +92,7 @@ export interface PluginOptions { indexSource, linksAsString, linksAsArray, + linksAsStructuredData, }: ReplaceCallbackOptions) => string; /** @@ -102,3 +108,27 @@ export interface PluginOptions { */ filter?: string | RegExp; } + +/** + * Data to build link. + */ +export interface LinkData { + /** + * Is the font request crossorigin or not. + * + * Defaults to true. + */ + crossorigin: PluginOptions["crossorigin"]; + + /** + * Type of load. It can be either "preload" or "prefetch". + * + * Defaults to "preload". + */ + loadType: PluginOptions["loadType"]; + + /** + * Final value of href attribute of the . + */ + href: string; +} diff --git a/src/index.ts b/src/index.ts index 1703589..785e01b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { JSDOM } from "jsdom"; import fs from "fs"; import path from "path"; import { Compiler, Compilation } from "webpack"; -import { Callback, LoadType, PluginOptions } from "./Types"; +import { Callback, LinkData, LoadType, PluginOptions } from "./Types"; export default class WebpackFontPreloadPlugin { private options: PluginOptions; @@ -68,12 +68,16 @@ export default class WebpackFontPreloadPlugin { if (indexSource) { let strLink = ""; const linksAsArray: string[] = []; + const linksAsStructuredData: LinkData[] = []; assets.forEach((asset) => { if (this.isFontAsset(asset.name) && this.isFiltered(asset.name)) { strLink += this.getLinkTag(asset.name, publicPath.toString()); linksAsArray.push( this.getLinkTag(asset.name, publicPath.toString()) ); + linksAsStructuredData.push( + this.getLinkData(asset.name, publicPath.toString()) + ); } }); // If `replaceCallback` is specified then app is responsible to forming the updated @@ -85,6 +89,7 @@ export default class WebpackFontPreloadPlugin { indexSource: indexSource.toString(), linksAsString: strLink, linksAsArray, + linksAsStructuredData, }) ); } else { @@ -167,6 +172,20 @@ export default class WebpackFontPreloadPlugin { >`; } + /** + * Get all the necesarry data for building of a `` tag for provided name + * and public path. + * + * @param {string} name Name of the font asset + * @param {string} publicPath Public path from webpack configuration + * @returns {LinkData} Structured data of the link + * + */ + private getLinkData(name: string, publicPath: string): LinkData { + const { crossorigin, loadType } = this.options; + return { crossorigin, loadType, href: `${publicPath}${name}` }; + } + /** * Check if the specified asset is a font asset. *