Skip to content

Commit

Permalink
feat: add css.hasTimestamp option. #250
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Dec 5, 2024
1 parent c9a7855 commit 8352115
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,21 +458,27 @@ type CSSOptions = {
/**
* Setting font size.
*/
fontSize?: string;
fontSize?: string | boolean;
/**
* Set the path in the css file
* https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
*/
cssPath?: string
cssPath?: string;
/**
* Set file name
* https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
*/
fileName?: string
fileName?: string;
/**
* Ad hoc template variables.
*/
templateVars?: Record<string, any>;
/**
* When including CSS files in a CSS file,
* you can add a timestamp parameter or custom text to the file path to prevent browser caching issues and ensure style updates are applied. @default true
* @example `path/to/iconfont.css?t=1612345678`
*/
hasTimestamp?: boolean | string;
}
```
Expand Down Expand Up @@ -583,6 +589,7 @@ Allows you to provide your own logging function. Set to `function(){}` to
> Default value: `undefined`
Some options can be configured with `svgoOptions` though it. [svgo](https://github.com/svg/svgo#configuration)
### svg2ttf
This is the setting for [svg2ttf](https://github.com/fontello/svg2ttf/tree/c33a126920f46b030e8ce960cc7a0e38a6946bbc#svg2ttfsvgfontstring-options---buf)
Expand Down
3 changes: 3 additions & 0 deletions examples/example#211/.svgtofontrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import path from "path";
*/
export default {
fontName: "iconfont",
css: {
hasTimestamp: "v1.2.3"
}
}
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export default async (options: SvgToFontOptions = {}) => {

options.svgicons2svgfont.fontName = options.fontName;
options.classNamePrefix = options.classNamePrefix || options.fontName;

const excludeFormat = options.excludeFormat || [];

const fontSizeOpt = typeof options.css !== 'boolean' && options.css.fontSize;
Expand Down Expand Up @@ -340,9 +340,10 @@ export default async (options: SvgToFontOptions = {}) => {
if (options.css) {
const styleTemplatePath = options.styleTemplates || path.resolve(__dirname, 'styles')
const outDir = typeof options.css === 'object' ? options.css.output || options.dist : options.dist;
const hasTimestamp = typeof options.css === 'object' ? options.css.hasTimestamp : true;

const cssOptions = typeof options.css === 'object' ? options.css : {};
const fontFamilyString = generateFontFaceCSS(options.fontName, cssOptions.cssPath || "", Date.now(), excludeFormat);
const fontFamilyString = generateFontFaceCSS(options.fontName, cssOptions.cssPath || "", Date.now(), excludeFormat, hasTimestamp);

await copyTemplate(styleTemplatePath, outDir, {
fontname: options.fontName,
Expand Down
15 changes: 11 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ export type CSSOptions = {
* Ad hoc template variables.
*/
templateVars?: Record<string, any>;
/**
* When including CSS files in a CSS file,
* you can add a timestamp parameter or custom text to the file path to prevent browser caching issues and ensure style updates are applied. @default true
* @example `path/to/iconfont.css?t=1612345678`
*/
hasTimestamp?: boolean | string;
}

// As we are processing css files, we need to eacape HTML entities.
Expand Down Expand Up @@ -327,7 +333,8 @@ export function createHTML(templatePath: string, data: Record<string, any>): str
});
};

export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[]): string {
export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[], hasTimestamp: boolean | string = true): string {
const timestamString = hasTimestamp === true ? `?t=${timestamp}` : (typeof hasTimestamp == 'string' ? `?t=${hasTimestamp}` : undefined);
const formats = [
{ ext: 'eot', format: 'embedded-opentype', ieFix: true },
{ ext: 'woff2', format: 'woff2' },
Expand All @@ -337,16 +344,16 @@ export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp
];
let cssString = ` font-family: "${fontName}";\n`;
if (!excludeFormat.includes('eot')) {
cssString += ` src: url('${cssPath}${fontName}.eot?t=${timestamp}'); /* IE9*/\n`;
cssString += ` src: url('${cssPath}${fontName}.eot${timestamString || ''}'); /* IE9*/\n`;
}
cssString += ' src: ';
const srcParts = formats
.filter(format => !excludeFormat.includes(format.ext))
.map(format => {
if (format.ext === 'eot') {
return `url('${cssPath}${fontName}.eot?t=${timestamp}#iefix') format('${format.format}') /* IE6-IE8 */`;
return `url('${cssPath}${fontName}.eot${timestamString || '?'}#iefix') format('${format.format}') /* IE6-IE8 */`;
}
return `url('${cssPath}${fontName}.${format.ext}?t=${timestamp}') format('${format.format}')`;
return `url('${cssPath}${fontName}.${format.ext}${timestamString || ''}') format('${format.format}')`;
});
cssString += srcParts.join(',\n ') + ';';
return cssString;
Expand Down

0 comments on commit 8352115

Please sign in to comment.