Skip to content
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

Extending replaceCallback capabilities #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 36 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export interface ReplaceCallbackOptions {
* `<link>` tags for preloading fonts as a string
*/
linksAsString: string;

/**
* `<link>` tags for preloading fonts as an array of strings
*/
linksAsArray: string[];

/**
* Link data for building a `<link>` tag
*/
linksAsStructuredData: LinkData[];
}

/**
Expand Down Expand Up @@ -81,6 +91,8 @@ export interface PluginOptions {
replaceCallback?: ({
indexSource,
linksAsString,
linksAsArray,
linksAsStructuredData,
}: ReplaceCallbackOptions) => string;

/**
Expand All @@ -96,3 +108,27 @@ export interface PluginOptions {
*/
filter?: string | RegExp;
}

/**
* Data to build <font> 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 <link>.
*/
href: string;
}
26 changes: 25 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,9 +67,17 @@ export default class WebpackFontPreloadPlugin {
const publicPath = (outputOptions && outputOptions.publicPath) || "";
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
Expand All @@ -80,6 +88,8 @@ export default class WebpackFontPreloadPlugin {
this.options.replaceCallback({
indexSource: indexSource.toString(),
linksAsString: strLink,
linksAsArray,
linksAsStructuredData,
})
);
} else {
Expand Down Expand Up @@ -162,6 +172,20 @@ export default class WebpackFontPreloadPlugin {
>`;
}

/**
* Get all the necesarry data for building of a `<link>` 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.
*
Expand Down
2 changes: 1 addition & 1 deletion test/utils/TestUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function areValidFonts(
validExtensions: string[]
): Promise<true | string[]> {
const expression = /(?:\.([^.]+))?$/;
const errors = [];
const errors: string[] = [];
if (fonts.length === 0) {
return Promise.reject(["No font's are preloaded in the output."]);
}
Expand Down