Skip to content

Commit

Permalink
feat: use template engine for styles (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
undefined-moe authored Aug 30, 2024
1 parent 9635757 commit 668989e
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 218 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ examples/templates/dist
examples/example#211/font
lib
package-lock.json
yarn.lock
__snapshots__

### Node ###
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,14 +655,14 @@ Define preview web content. Example:
#### website.template
> Type: `String`
> Default value: [index.ejs](src/website/index.ejs)
> Default value: [index.html](src/website/index.html)
Custom template can customize parameters. You can define your own template based on the [default template](src/website/index.ejs).
Custom template can customize parameters. You can define your own template based on the [default template](src/website/index.html).
```js
{
website: {
template: path.join(process.cwd(), "my-template.ejs")
template: path.join(process.cwd(), "my-template.html")
}
}
```
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@
}
},
"dependencies": {
"@tsbb/copy-template-dir": "^1.4.0",
"auto-config-loader": "^1.7.4",
"cheerio": "~1.0.0-rc.12",
"colors-cli": "~1.0.28",
"del": "~7.1.0",
"ejs": "~3.1.6",
"fs-extra": "~11.2.0",
"image2uri": "^2.1.2",
"move-file": "~3.1.0",
"nunjucks": "^3.2.4",
"svg2ttf": "~6.0.3",
"svgicons2svgfont": "~14.0.0",
"svgo": "~3.3.0",
Expand All @@ -99,8 +96,8 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@types/ejs": "~3.1.0",
"@types/fs-extra": "^11.0.1",
"@types/nunjucks": "^3.2.6",
"@types/svg2ttf": "~5.0.1",
"@types/ttf2eot": "~2.0.0",
"@types/ttf2woff": "~2.0.2",
Expand Down
13 changes: 8 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="../src/types" />

import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs-extra';
Expand Down Expand Up @@ -260,6 +258,7 @@ export default async (options: SvgToFontOptions = {}) => {
await fs.ensureDir(options.dist);
const unicodeObject = await createSVG(options);

/** @deprecated */
let cssToVars: string[] = [];
let cssString: string[] = [];
let cssRootVars: string[] = [];
Expand Down Expand Up @@ -322,14 +321,17 @@ export default async (options: SvgToFontOptions = {}) => {
await createSvgSymbol(options);

if (options.css) {
const styleTemplatePath = options.styleTemplates || (!options.useNameAsUnicode ? path.resolve(__dirname, 'styles') : path.resolve(__dirname, 'ligature-styles'));
await copyTemplate(styleTemplatePath, options.dist, {
const styleTemplatePath = options.styleTemplates || path.resolve(__dirname, 'styles')
const outDir = typeof options.css === 'object' ? options.css.output || options.dist : options.dist;
await copyTemplate(styleTemplatePath, outDir, {
fontname: options.fontName,
cssString: cssString.join(''),
cssToVars: cssToVars.join(''),
infoData,
fontSize: fontSize,
timestamp: new Date().getTime(),
prefix,
nameAsUnicode: options.useNameAsUnicode,
_opts: typeof options.css === 'boolean' ? {} : { ...options.css }
});
}
Expand All @@ -352,7 +354,7 @@ export default async (options: SvgToFontOptions = {}) => {
if (name === 'symbol') symbolPath = _path;
});
// default template
options.website.template = options.website.template || path.join(__dirname, 'website', 'index.ejs');
options.website.template = options.website.template || path.join(__dirname, 'website', 'index.html');
// template data
const tempData: SvgToFontOptions['website'] & {
fontname: string;
Expand Down Expand Up @@ -415,6 +417,7 @@ export default async (options: SvgToFontOptions = {}) => {
log.log(`${color.green('SUCCESS')} Created React Native Components. `);
}

return infoData;
} catch (error) {
log.log('SvgToFont:CLI:ERR:', error);
}
Expand Down
19 changes: 0 additions & 19 deletions src/ligature-styles/_{{filename}}.css

This file was deleted.

15 changes: 0 additions & 15 deletions src/ligature-styles/_{{filename}}.less

This file was deleted.

15 changes: 0 additions & 15 deletions src/ligature-styles/_{{filename}}.module.less

This file was deleted.

15 changes: 0 additions & 15 deletions src/ligature-styles/_{{filename}}.scss

This file was deleted.

15 changes: 0 additions & 15 deletions src/ligature-styles/_{{filename}}.styl

This file was deleted.

5 changes: 3 additions & 2 deletions src/styles/_{{filename}}.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
}

[class^="{{prefix}}-"], [class*=" {{prefix}}-"] {
{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
font-family: '{{fontname}}' !important;{{fontSize}}
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}


{% if not nameAsUnicode %}
{{cssString}}
{% endif %}
6 changes: 4 additions & 2 deletions src/styles/_{{filename}}.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
}

[class^="{{prefix}}-"], [class*=" {{prefix}}-"] {
{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
font-family: '{{fontname}}' !important;{{fontSize}}
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

{{cssString}}
{% if not nameAsUnicode %}
{{cssString}}
{% endif %}
6 changes: 4 additions & 2 deletions src/styles/_{{filename}}.module.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
}

[class^="{{prefix}}-"], [class*=" {{prefix}}-"] {
{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
font-family: '{{fontname}}' !important;{{fontSize}}
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

{% if not nameAsUnicode %}
:global {
{{cssString}}
}
}
{% endif %}
10 changes: 7 additions & 3 deletions src/styles/_{{filename}}.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
}

[class^="{{prefix}}-"], [class*=" {{prefix}}-"] {
{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
font-family: '{{fontname}}' !important;{{fontSize}}
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

{{cssString}}
{{cssToVars}}
{% if not nameAsUnicode %}
{{ cssString }}
{% for name, value in infoData %}
${{ name }}: {{ value.encodedCode }}
{% endfor %}
{% endif %}
9 changes: 7 additions & 2 deletions src/styles/_{{filename}}.styl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
url('{{cssPath}}{{fontname}}.svg?t={{timestamp}}#{{fontname}}') format('svg'); /* iOS 4.1- */
}

[class^="{{prefix}}-"], [class*=" {{prefix}}-"] {
{% if nameAsUnicode %}.{{prefix}}{% else %}[class^="{{prefix}}-"], [class*=" {{prefix}}-"]{% endif %} {
font-family: '{{fontname}}' !important;{{fontSize}}
font-style:normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

{{cssString}}
{% if not nameAsUnicode %}
{{ cssString }}
{% for name, value in infoData %}
${{ name }} = {{ value.encodedCode }}
{% endfor %}
{% endif %}
5 changes: 0 additions & 5 deletions src/types/index.d.ts

This file was deleted.

Loading

0 comments on commit 668989e

Please sign in to comment.