Skip to content

Commit 3324321

Browse files
committed
feat: add dts, support esm and ts
1 parent a52bb41 commit 3324321

15 files changed

+479
-42
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.DS_Store
22
npm-debug.log
3-
/node_modules
3+
node_modules
44
fonts/dest*
55
/coverage
6+
.nyc_output
7+
package-lock.json
8+
test/ts/example.js

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ $ npm install --save fontmin
3131
## Usage
3232

3333
```js
34-
var Fontmin = require('fontmin');
34+
import Fontmin from 'fontmin';
3535

36-
var fontmin = new Fontmin()
36+
const fontmin = new Fontmin()
3737
.src('fonts/*.ttf')
3838
.dest('build/fonts');
3939

@@ -50,10 +50,10 @@ fontmin.run(function (err, files) {
5050
You can use [gulp-rename](https://github.com/hparra/gulp-rename) to rename your files:
5151

5252
```js
53-
var Fontmin = require('fontmin');
54-
var rename = require('gulp-rename');
53+
import Fontmin from 'fontmin';
54+
const rename = require('gulp-rename');
5555

56-
var fontmin = new Fontmin()
56+
const fontmin = new Fontmin()
5757
.src('fonts/big.ttf')
5858
.use(rename('small.ttf'));
5959
```
@@ -114,10 +114,10 @@ The following plugins are bundled with fontmin:
114114
Compress ttf by glyph.
115115

116116
```js
117-
var Fontmin = require('fontmin');
117+
import Fontmin from 'fontmin';
118118

119-
var fontmin = new Fontmin()
120-
.use(Fontmin.glyph({
119+
const fontmin = new Fontmin()
120+
.use(Fontmin.glyph({
121121
text: '天地玄黄 宇宙洪荒',
122122
hinting: false // keep ttf hint info (fpgm, prep, cvt). default = true
123123
}));
@@ -128,9 +128,9 @@ var fontmin = new Fontmin()
128128
Convert ttf to eot.
129129

130130
```js
131-
var Fontmin = require('fontmin');
131+
import Fontmin from 'fontmin';
132132

133-
var fontmin = new Fontmin()
133+
const fontmin = new Fontmin()
134134
.use(Fontmin.ttf2eot());
135135
```
136136

@@ -139,9 +139,9 @@ var fontmin = new Fontmin()
139139
Convert ttf to woff.
140140

141141
```js
142-
var Fontmin = require('fontmin');
142+
import Fontmin from 'fontmin';
143143

144-
var fontmin = new Fontmin()
144+
const fontmin = new Fontmin()
145145
.use(Fontmin.ttf2woff({
146146
deflate: true // deflate woff. default = false
147147
}));
@@ -152,9 +152,9 @@ var fontmin = new Fontmin()
152152
Convert ttf to woff2.
153153

154154
```js
155-
var Fontmin = require('fontmin');
155+
import Fontmin from 'fontmin';
156156

157-
var fontmin = new Fontmin()
157+
const fontmin = new Fontmin()
158158
.use(Fontmin.ttf2woff2());
159159
```
160160

@@ -165,10 +165,10 @@ Convert ttf to svg.
165165
you can use [imagemin-svgo](https://github.com/imagemin/imagemin-svgo) to compress svg:
166166

167167
```js
168-
var Fontmin = require('fontmin');
169-
var svgo = require('imagemin-svgo');
168+
import Fontmin from 'fontmin';
169+
const svgo = require('imagemin-svgo');
170170

171-
var fontmin = new Fontmin()
171+
const fontmin = new Fontmin()
172172
.use(Fontmin.ttf2svg())
173173
.use(svgo());
174174

@@ -179,12 +179,12 @@ var fontmin = new Fontmin()
179179
Generate css from ttf, often used to make iconfont.
180180

181181
```js
182-
var Fontmin = require('fontmin');
182+
import Fontmin from 'fontmin';
183183

184-
var fontmin = new Fontmin()
184+
const fontmin = new Fontmin()
185185
.use(Fontmin.css({
186-
fontPath: './', // location of font file
187-
base64: true, // inject base64 data:application/x-font-ttf; (gzip font with css).
186+
fontPath: './', // location of font file
187+
base64: true, // inject base64 data:application/x-font-ttf; (gzip font with css).
188188
// default = false
189189
glyph: true, // generate class for each glyph. default = false
190190
iconPrefix: 'my-icon', // class prefix, only work when glyph is `true`. default to "icon"
@@ -196,9 +196,9 @@ var fontmin = new Fontmin()
196196

197197
Alternatively, a transform function can be passed as `fontFamily` option.
198198
```js
199-
var Fontmin = require('fontmin');
199+
import Fontmin from 'fontmin';
200200

201-
var fontmin = new Fontmin()
201+
const fontmin = new Fontmin()
202202
.use(Fontmin.css({
203203
// ...
204204
fontFamily: function(fontInfo, ttf) {
@@ -213,9 +213,9 @@ var fontmin = new Fontmin()
213213
Convert font format svg to ttf.
214214

215215
```js
216-
var Fontmin = require('fontmin');
216+
import Fontmin from 'fontmin';
217217

218-
var fontmin = new Fontmin()
218+
const fontmin = new Fontmin()
219219
.src('font.svg')
220220
.use(Fontmin.svg2ttf());
221221
```
@@ -227,9 +227,9 @@ Concat svg files to a ttf, just like css sprite.
227227
awesome work with [css](#css) plugin:
228228

229229
```js
230-
var Fontmin = require('fontmin');
230+
import Fontmin from 'fontmin';
231231

232-
var fontmin = new Fontmin()
232+
const fontmin = new Fontmin()
233233
.src('svgs/*.svg')
234234
.use(Fontmin.svgs2ttf('font.ttf', {fontName: 'iconfont'}))
235235
.use(Fontmin.css({
@@ -242,9 +242,9 @@ var fontmin = new Fontmin()
242242
Convert otf to ttf.
243243

244244
```js
245-
var Fontmin = require('fontmin');
245+
import Fontmin from 'fontmin';
246246

247-
var fontmin = new Fontmin()
247+
const fontmin = new Fontmin()
248248
.src('fonts/*.otf')
249249
.use(Fontmin.otf2ttf());
250250
```

index.d.ts

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/**
2+
* @file index.d.ts
3+
* @author kekee000([email protected])
4+
*/
5+
import through from 'through2';
6+
import {EventEmitter} from 'events';
7+
import {Transform} from 'stream';
8+
import {TTF} from 'fonteditor-core'
9+
10+
type PluginDesc = (...args: any[]) => Transform;
11+
type InternalPlugin<T = any> = (opts?: T) => PluginDesc;
12+
13+
interface GlyphPluginOptions {
14+
/**
15+
* use this text to generate compressed font
16+
*/
17+
text: string;
18+
/**
19+
* add basic chars to glyph
20+
* @example "!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}"
21+
*/
22+
basicText?: boolean;
23+
/**
24+
* keep gylph hinting, defaul true
25+
*/
26+
hinting?: boolean;
27+
/**
28+
* use other plugin
29+
*/
30+
use?: PluginDesc;
31+
}
32+
33+
interface FontInfo {
34+
fontFile: string;
35+
fontPath: string;
36+
base64: string;
37+
glyph: boolean;
38+
iconPrefix: string;
39+
local: boolean;
40+
}
41+
42+
interface CssPluginOptions {
43+
/**
44+
*generate class for each glyph. default = false
45+
*/
46+
glyph?: boolean;
47+
/**
48+
* inject base64 data:application/x-font-ttf; (gzip font with css). default = false
49+
*/
50+
base64?: boolean;
51+
/**
52+
* class prefix, only work when glyph is `true`. default to "icon"
53+
*/
54+
iconPrefix?: string;
55+
/**
56+
* rewrite fontFamily as filename force. default = false
57+
*/
58+
asFileName?: boolean;
59+
/**
60+
* location of font file
61+
*/
62+
fontPath?: string;
63+
/**
64+
* custom fontFamily, default to filename or get from analysed ttf file
65+
*/
66+
fontFamily?: string | ((fontInfo: FontInfo, ttf: TTF.TTFObject) => string);
67+
/**
68+
* boolean to add local font. default = false
69+
*/
70+
local?: boolean;
71+
}
72+
73+
interface Svgs2ttfPluginOptions {
74+
/**
75+
* set font name
76+
*/
77+
fontName?: string;
78+
}
79+
80+
declare namespace Fontmin {
81+
/*
82+
* get subset font using giving text
83+
*/
84+
const glyph: InternalPlugin<GlyphPluginOptions>;
85+
86+
/*
87+
* convert ttf to eot
88+
*/
89+
const ttf2eot: InternalPlugin;
90+
91+
/*
92+
* convert ttf to woff
93+
*/
94+
const ttf2woff: InternalPlugin<{
95+
/**
96+
* use deflate to transform woff, default false
97+
*/
98+
deflate: boolean;
99+
}>;
100+
101+
/*
102+
* convert ttf to woff2
103+
*/
104+
const ttf2woff2: InternalPlugin;
105+
106+
/*
107+
* convert ttf to svg text
108+
*/
109+
const ttf2svg: InternalPlugin;
110+
111+
/*
112+
* Generate css from ttf, often used to make iconfont.
113+
*/
114+
const css: InternalPlugin<CssPluginOptions>;
115+
116+
/**
117+
* convert font format svg to ttf
118+
*/
119+
const svg2ttf: InternalPlugin<{hinting?: boolean}>;
120+
121+
/**
122+
* concat svg files to a ttf, just like css sprite
123+
*/
124+
const svgs2ttf: (file: string, opts?: Svgs2ttfPluginOptions) => through.Through2Constructor;
125+
126+
/**
127+
* convert otf to ttf
128+
*/
129+
const otf2ttf: InternalPlugin;
130+
}
131+
132+
type PluginNames = keyof typeof Fontmin;
133+
134+
declare class Fontmin extends EventEmitter {
135+
static plugins: PluginNames[];
136+
137+
/**
138+
* Get or set the source files
139+
* @param file files to be optimized
140+
*/
141+
src(src: ArrayLike<number> | Buffer | string): this;
142+
143+
/**
144+
* Get or set the destination folder
145+
* @param dir folder to written
146+
*/
147+
dest(dest: string): this;
148+
149+
/**
150+
* Add a plugin to the middleware stack
151+
* @param plugin plugin function
152+
*/
153+
use(plugin: PluginDesc): this;
154+
155+
/**
156+
* run Optimize files
157+
* @param callback plugin function
158+
*/
159+
run(callback: (err: Error, files: Buffer[]) => void): Transform;
160+
161+
/**
162+
* run Optimize files with return Promise
163+
*/
164+
runAsync(): Promise<Buffer[]>;
165+
}
166+
167+
export default Fontmin;
168+
169+
export const mime: {
170+
'.*': 'application/octet-stream',
171+
'ttf': 'application/font-sfnt',
172+
'otf': 'application/font-sfnt',
173+
'woff': 'application/font-woff',
174+
'woff2': 'application/font-woff2',
175+
'eot': 'application/octet-stream',
176+
'svg': 'image/svg+xml',
177+
'svgz': 'image/svg+xml'
178+
};

index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ Fontmin.prototype.run = function (cb) {
9595
return stream;
9696
};
9797

98+
/**
99+
* run Optimize files with return Promise
100+
*
101+
* @return {Array<Buffer>} file result
102+
* @api public
103+
*/
104+
Fontmin.prototype.runAsync = function () {
105+
return new Promise((resolve, reject) => {
106+
var stream = this.createStream();
107+
stream.on('error', reject);
108+
109+
stream.pipe(concat(resolve));
110+
});
111+
};
112+
113+
98114
/**
99115
* Create stream
100116
*
@@ -165,5 +181,6 @@ Fontmin.plugins.forEach(function (plugin) {
165181
module.exports = Fontmin;
166182

167183
// exports util, mime
184+
module.exports.default = Fontmin;
168185
module.exports.util = exports.util = require('./lib/util');
169186
module.exports.mime = exports.mime = require('./lib/mime-types');

0 commit comments

Comments
 (0)