Skip to content

Commit 8352115

Browse files
committedDec 5, 2024·
feat: add css.hasTimestamp option. #250
1 parent c9a7855 commit 8352115

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed
 

‎README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -458,21 +458,27 @@ type CSSOptions = {
458458
/**
459459
* Setting font size.
460460
*/
461-
fontSize?: string;
461+
fontSize?: string | boolean;
462462
/**
463463
* Set the path in the css file
464464
* https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
465465
*/
466-
cssPath?: string
466+
cssPath?: string;
467467
/**
468468
* Set file name
469469
* https://github.com/jaywcjlove/svgtofont/issues/48#issuecomment-739547189
470470
*/
471-
fileName?: string
471+
fileName?: string;
472472
/**
473473
* Ad hoc template variables.
474474
*/
475475
templateVars?: Record<string, any>;
476+
/**
477+
* When including CSS files in a CSS file,
478+
* 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
479+
* @example `path/to/iconfont.css?t=1612345678`
480+
*/
481+
hasTimestamp?: boolean | string;
476482
}
477483
```
478484
@@ -583,6 +589,7 @@ Allows you to provide your own logging function. Set to `function(){}` to
583589
> Default value: `undefined`
584590
585591
Some options can be configured with `svgoOptions` though it. [svgo](https://github.com/svg/svgo#configuration)
592+
586593
### svg2ttf
587594
588595
This is the setting for [svg2ttf](https://github.com/fontello/svg2ttf/tree/c33a126920f46b030e8ce960cc7a0e38a6946bbc#svg2ttfsvgfontstring-options---buf)

‎examples/example#211/.svgtofontrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import path from "path";
44
*/
55
export default {
66
fontName: "iconfont",
7+
css: {
8+
hasTimestamp: "v1.2.3"
9+
}
710
}

‎src/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export default async (options: SvgToFontOptions = {}) => {
248248

249249
options.svgicons2svgfont.fontName = options.fontName;
250250
options.classNamePrefix = options.classNamePrefix || options.fontName;
251-
251+
252252
const excludeFormat = options.excludeFormat || [];
253253

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

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

347348
await copyTemplate(styleTemplatePath, outDir, {
348349
fontname: options.fontName,

‎src/utils.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ export type CSSOptions = {
280280
* Ad hoc template variables.
281281
*/
282282
templateVars?: Record<string, any>;
283+
/**
284+
* When including CSS files in a CSS file,
285+
* 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
286+
* @example `path/to/iconfont.css?t=1612345678`
287+
*/
288+
hasTimestamp?: boolean | string;
283289
}
284290

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

330-
export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[]): string {
336+
export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp: number, excludeFormat: string[], hasTimestamp: boolean | string = true): string {
337+
const timestamString = hasTimestamp === true ? `?t=${timestamp}` : (typeof hasTimestamp == 'string' ? `?t=${hasTimestamp}` : undefined);
331338
const formats = [
332339
{ ext: 'eot', format: 'embedded-opentype', ieFix: true },
333340
{ ext: 'woff2', format: 'woff2' },
@@ -337,16 +344,16 @@ export function generateFontFaceCSS(fontName: string, cssPath: string, timestamp
337344
];
338345
let cssString = ` font-family: "${fontName}";\n`;
339346
if (!excludeFormat.includes('eot')) {
340-
cssString += ` src: url('${cssPath}${fontName}.eot?t=${timestamp}'); /* IE9*/\n`;
347+
cssString += ` src: url('${cssPath}${fontName}.eot${timestamString || ''}'); /* IE9*/\n`;
341348
}
342349
cssString += ' src: ';
343350
const srcParts = formats
344351
.filter(format => !excludeFormat.includes(format.ext))
345352
.map(format => {
346353
if (format.ext === 'eot') {
347-
return `url('${cssPath}${fontName}.eot?t=${timestamp}#iefix') format('${format.format}') /* IE6-IE8 */`;
354+
return `url('${cssPath}${fontName}.eot${timestamString || '?'}#iefix') format('${format.format}') /* IE6-IE8 */`;
348355
}
349-
return `url('${cssPath}${fontName}.${format.ext}?t=${timestamp}') format('${format.format}')`;
356+
return `url('${cssPath}${fontName}.${format.ext}${timestamString || ''}') format('${format.format}')`;
350357
});
351358
cssString += srcParts.join(',\n ') + ';';
352359
return cssString;

0 commit comments

Comments
 (0)
Please sign in to comment.