@fastkit/icon-font-gen 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/icon-font-gen.d.ts +1 -1
- package/dist/{icon-font-gen.cjs.js → icon-font-gen.mjs} +40 -76
- package/lib/cli.mjs +4 -0
- package/package.json +12 -14
- package/dist/icon-font-gen.cjs.prod.js +0 -404
- package/index.js +0 -6
- package/lib/cli.js +0 -4
package/dist/icon-font-gen.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type webfont from 'webfont';
|
|
|
5
5
|
|
|
6
6
|
export declare function ceateIconFontConfig(options: IconFontConfig): IconFontConfig;
|
|
7
7
|
|
|
8
|
-
export declare function cli(): void
|
|
8
|
+
export declare function cli(): Promise<void>;
|
|
9
9
|
|
|
10
10
|
export declare const DEFAULT_CONFIG_FILENAME = "icon-font.config";
|
|
11
11
|
|
|
@@ -1,32 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var chokidar = require('chokidar');
|
|
9
|
-
var ev = require('@fastkit/ev');
|
|
10
|
-
var tinyLogger = require('@fastkit/tiny-logger');
|
|
11
|
-
var cac = require('cac');
|
|
12
|
-
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
14
|
-
|
|
15
|
-
function _interopNamespace(e) {
|
|
16
|
-
if (e && e.__esModule) return e;
|
|
17
|
-
var n = Object.create(null);
|
|
18
|
-
if (e) {
|
|
19
|
-
for (var k in e) {
|
|
20
|
-
n[k] = e[k];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
n["default"] = e;
|
|
24
|
-
return Object.freeze(n);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
28
|
-
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
29
|
-
var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import { installPackage, HashComparator, findPackageDir, esbuildRequire } from '@fastkit/node-util';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import chokidar from 'chokidar';
|
|
5
|
+
import { EV } from '@fastkit/ev';
|
|
6
|
+
import { TinyLogger, createTinyError } from '@fastkit/tiny-logger';
|
|
7
|
+
import { cac } from 'cac';
|
|
30
8
|
|
|
31
9
|
const ICON_FONT_FORMATS = [
|
|
32
10
|
'eot',
|
|
@@ -49,7 +27,7 @@ async function resolveRawIconFontEntry(rootDir, rawEntry) {
|
|
|
49
27
|
// mdi support
|
|
50
28
|
if (entry.src === '@mdi') {
|
|
51
29
|
const installedDir = await findOrInstallMDI();
|
|
52
|
-
entry.src =
|
|
30
|
+
entry.src = path.join(installedDir, 'svg');
|
|
53
31
|
if (entry.fontHeight == null) {
|
|
54
32
|
entry.fontHeight = 512;
|
|
55
33
|
}
|
|
@@ -60,9 +38,9 @@ async function resolveRawIconFontEntry(rootDir, rawEntry) {
|
|
|
60
38
|
entry.name = 'mdi';
|
|
61
39
|
}
|
|
62
40
|
}
|
|
63
|
-
const name = entry.name ||
|
|
41
|
+
const name = entry.name || path.basename(entry.src);
|
|
64
42
|
const fontName = entry.fontName || `${name}-icon`;
|
|
65
|
-
const dest =
|
|
43
|
+
const dest = path.join(rootDir, name);
|
|
66
44
|
const prefix = entry.disablePrefix ? '' : `${name}-`;
|
|
67
45
|
return {
|
|
68
46
|
...entry,
|
|
@@ -73,7 +51,7 @@ async function resolveRawIconFontEntry(rootDir, rawEntry) {
|
|
|
73
51
|
};
|
|
74
52
|
}
|
|
75
53
|
function findOrInstallMDI() {
|
|
76
|
-
return
|
|
54
|
+
return installPackage('@mdi/svg', { dev: true });
|
|
77
55
|
}
|
|
78
56
|
const DEFAULT_CONFIG_FILENAME = 'icon-font.config';
|
|
79
57
|
const DEFAULT_DEST_DIRNAME = '.icon-font';
|
|
@@ -82,8 +60,8 @@ function ceateIconFontConfig(options) {
|
|
|
82
60
|
}
|
|
83
61
|
|
|
84
62
|
const name = 'icon-font-gen';
|
|
85
|
-
const logger = new
|
|
86
|
-
const IconFontGenError =
|
|
63
|
+
const logger = new TinyLogger(name);
|
|
64
|
+
const IconFontGenError = createTinyError(name);
|
|
87
65
|
|
|
88
66
|
const DEFAULT_OPTIONS = {
|
|
89
67
|
formats: ['woff2'],
|
|
@@ -122,8 +100,8 @@ export const ICON_NAMES = registerIconNames([
|
|
|
122
100
|
export type { IconName, IconNameMap } from '@fastkit/icon-font';
|
|
123
101
|
`.trim();
|
|
124
102
|
const fileName = `${entry.name || 'icons'}.ts`;
|
|
125
|
-
const dest =
|
|
126
|
-
await
|
|
103
|
+
const dest = path.resolve(entry.dest, fileName);
|
|
104
|
+
await fs.writeFile(dest, code);
|
|
127
105
|
return {
|
|
128
106
|
fileName,
|
|
129
107
|
dest,
|
|
@@ -135,14 +113,14 @@ async function generateEntry(entry) {
|
|
|
135
113
|
// @TODO support empty prefix
|
|
136
114
|
// const namePrefix = options.name;
|
|
137
115
|
const cssPrefix = `icon-${options.prefix}`;
|
|
138
|
-
const hash = new
|
|
116
|
+
const hash = new HashComparator(options.src, options.dest);
|
|
139
117
|
const srcHash = await hash.hasChanged();
|
|
140
118
|
if (!srcHash) {
|
|
141
119
|
logger.info(`Has not chaged files. Skip process. >>> ${options.src}`);
|
|
142
120
|
return { entry };
|
|
143
121
|
}
|
|
144
|
-
await
|
|
145
|
-
const webfont = await (
|
|
122
|
+
await fs.ensureDir(options.dest);
|
|
123
|
+
const { webfont } = await import('webfont');
|
|
146
124
|
// const { startUnicode = 0xea01 } = options;
|
|
147
125
|
const { startUnicode = 0xba01 } = options;
|
|
148
126
|
let i = startUnicode;
|
|
@@ -172,7 +150,7 @@ async function generateEntry(entry) {
|
|
|
172
150
|
}
|
|
173
151
|
});
|
|
174
152
|
await Promise.all(buildedFormats.map(({ format, code }) => {
|
|
175
|
-
return
|
|
153
|
+
return fs.writeFile(path.join(options.dest, `${options.name}.${format}`), code);
|
|
176
154
|
}));
|
|
177
155
|
const glyphs = [];
|
|
178
156
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
@@ -221,7 +199,7 @@ ${glyphs
|
|
|
221
199
|
})
|
|
222
200
|
.join('\n')}
|
|
223
201
|
`;
|
|
224
|
-
await
|
|
202
|
+
await fs.writeFile(path.join(options.dest, `${options.name}.css`), cssCode);
|
|
225
203
|
// const prefix = options.prefix ? `${options.prefix}-` : '';
|
|
226
204
|
const ids = glyphs.map(({ name }) => `${options.prefix}${name}`);
|
|
227
205
|
await generateTS(options, ids);
|
|
@@ -246,23 +224,23 @@ ${names
|
|
|
246
224
|
export type { IconName } from '@fastkit/icon-font';
|
|
247
225
|
export { ICON_NAMES } from '@fastkit/icon-font';
|
|
248
226
|
`.trim();
|
|
249
|
-
const tsDest =
|
|
250
|
-
await
|
|
227
|
+
const tsDest = path.join(dest, 'index.ts');
|
|
228
|
+
await fs.writeFile(tsDest, tsCode);
|
|
251
229
|
const cssCode = `
|
|
252
230
|
/* stylelint-disable */
|
|
253
231
|
${BANNER}
|
|
254
232
|
${names.map((name) => `@import './${name}/${name}.css';`).join('\n')}
|
|
255
233
|
`.trim();
|
|
256
|
-
const cssDest =
|
|
257
|
-
await
|
|
234
|
+
const cssDest = path.join(dest, 'index.css');
|
|
235
|
+
await fs.writeFile(cssDest, cssCode);
|
|
258
236
|
}
|
|
259
237
|
async function generate(opts) {
|
|
260
|
-
await
|
|
238
|
+
await fs.emptyDir(opts.dest);
|
|
261
239
|
const results = await Promise.all(opts.entries.map((entry) => resolveRawIconFontEntry(opts.dest, entry).then((entry) => generateEntry(entry))));
|
|
262
240
|
const entries = results.map(({ entry }) => entry);
|
|
263
241
|
await generateIndex(opts.dest, entries);
|
|
264
242
|
}
|
|
265
|
-
class IconFontRunnerItem extends
|
|
243
|
+
class IconFontRunnerItem extends EV {
|
|
266
244
|
entry;
|
|
267
245
|
ctx;
|
|
268
246
|
_resolveEntryPromise;
|
|
@@ -281,8 +259,8 @@ class IconFontRunnerItem extends ev.EV {
|
|
|
281
259
|
async run() {
|
|
282
260
|
const result = await this.build();
|
|
283
261
|
if (this.watchMode && !this._watcher) {
|
|
284
|
-
const watchDir =
|
|
285
|
-
this._watcher =
|
|
262
|
+
const watchDir = path.resolve(this.entry.src);
|
|
263
|
+
this._watcher = chokidar.watch(watchDir, { ignoreInitial: true });
|
|
286
264
|
this._watcher.on('all', this.build);
|
|
287
265
|
}
|
|
288
266
|
return result;
|
|
@@ -310,7 +288,7 @@ class IconFontRunnerItem extends ev.EV {
|
|
|
310
288
|
}
|
|
311
289
|
}
|
|
312
290
|
}
|
|
313
|
-
class IconFontRunner extends
|
|
291
|
+
class IconFontRunner extends EV {
|
|
314
292
|
items = [];
|
|
315
293
|
dest;
|
|
316
294
|
// private _opts: IconFontOptions;
|
|
@@ -335,7 +313,7 @@ class IconFontRunner extends ev.EV {
|
|
|
335
313
|
return generateIndex(this.dest, entries);
|
|
336
314
|
}
|
|
337
315
|
async run() {
|
|
338
|
-
await
|
|
316
|
+
await fs.ensureDir(this.dest);
|
|
339
317
|
await Promise.all([
|
|
340
318
|
this.buildIndex(),
|
|
341
319
|
Promise.all(this.items.map((item) => item.run())),
|
|
@@ -349,15 +327,14 @@ class IconFontRunner extends ev.EV {
|
|
|
349
327
|
}
|
|
350
328
|
}
|
|
351
329
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const cli = cac.cac('icon-font');
|
|
330
|
+
async function cli() {
|
|
331
|
+
const cli = cac('icon-font');
|
|
355
332
|
cli
|
|
356
333
|
.option('-d, --dest <string>', 'Destination for generated fonts.', {
|
|
357
|
-
default:
|
|
334
|
+
default: path.join(process.cwd(), `${DEFAULT_DEST_DIRNAME}`),
|
|
358
335
|
})
|
|
359
336
|
.command('[config file path]', `
|
|
360
|
-
Path of the configuration file. (default: [your package directory]${
|
|
337
|
+
Path of the configuration file. (default: [your package directory]${path.sep}${DEFAULT_CONFIG_FILENAME})
|
|
361
338
|
You must export default <IconFontConfig>.
|
|
362
339
|
|
|
363
340
|
e.g.
|
|
@@ -369,14 +346,14 @@ Path of the configuration file. (default: [your package directory]${path__defaul
|
|
|
369
346
|
`.trim())
|
|
370
347
|
.action(async (configPath, options) => {
|
|
371
348
|
if (!configPath) {
|
|
372
|
-
const pkgDir = await
|
|
349
|
+
const pkgDir = await findPackageDir();
|
|
373
350
|
if (!pkgDir) {
|
|
374
351
|
throw new IconFontGenError('missing package directory.');
|
|
375
352
|
}
|
|
376
|
-
configPath =
|
|
353
|
+
configPath = path.join(pkgDir, DEFAULT_CONFIG_FILENAME);
|
|
377
354
|
}
|
|
378
|
-
const dest =
|
|
379
|
-
const mod = await
|
|
355
|
+
const dest = path.resolve(options.dest);
|
|
356
|
+
const mod = await esbuildRequire(configPath);
|
|
380
357
|
const config = mod.exports.default;
|
|
381
358
|
await generate({
|
|
382
359
|
...config,
|
|
@@ -384,21 +361,8 @@ Path of the configuration file. (default: [your package directory]${path__defaul
|
|
|
384
361
|
});
|
|
385
362
|
});
|
|
386
363
|
cli.help();
|
|
387
|
-
cli.version(
|
|
364
|
+
cli.version("0.10.1");
|
|
388
365
|
cli.parse();
|
|
389
366
|
}
|
|
390
367
|
|
|
391
|
-
|
|
392
|
-
exports.DEFAULT_DEST_DIRNAME = DEFAULT_DEST_DIRNAME;
|
|
393
|
-
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
394
|
-
exports.ICON_FONT_FORMATS = ICON_FONT_FORMATS;
|
|
395
|
-
exports.ICON_FONT_FORMAT_MAP = ICON_FONT_FORMAT_MAP;
|
|
396
|
-
exports.IconFontRunner = IconFontRunner;
|
|
397
|
-
exports.IconFontRunnerItem = IconFontRunnerItem;
|
|
398
|
-
exports.ceateIconFontConfig = ceateIconFontConfig;
|
|
399
|
-
exports.cli = cli;
|
|
400
|
-
exports.generate = generate;
|
|
401
|
-
exports.generateEntry = generateEntry;
|
|
402
|
-
exports.generateIndex = generateIndex;
|
|
403
|
-
exports.mergeDefaults = mergeDefaults;
|
|
404
|
-
exports.resolveRawIconFontEntry = resolveRawIconFontEntry;
|
|
368
|
+
export { DEFAULT_CONFIG_FILENAME, DEFAULT_DEST_DIRNAME, DEFAULT_OPTIONS, ICON_FONT_FORMATS, ICON_FONT_FORMAT_MAP, IconFontRunner, IconFontRunnerItem, ceateIconFontConfig, cli, generate, generateEntry, generateIndex, mergeDefaults, resolveRawIconFontEntry };
|
package/lib/cli.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastkit/icon-font-gen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.1",
|
|
4
4
|
"description": "A tool to generate icon web fonts and their definitions Type-safe for your application.",
|
|
5
5
|
"buildOptions": {
|
|
6
|
-
"name": "IconFont"
|
|
7
|
-
"formats": [
|
|
8
|
-
"cjs"
|
|
9
|
-
]
|
|
6
|
+
"name": "IconFont"
|
|
10
7
|
},
|
|
11
8
|
"_docs": {
|
|
12
9
|
"scope": "",
|
|
@@ -16,13 +13,14 @@
|
|
|
16
13
|
"ja": "アプリケーションのためにアイコンWebフォントとその定義をTypeセーフに生成するツール。"
|
|
17
14
|
}
|
|
18
15
|
},
|
|
19
|
-
"main": "index.js",
|
|
20
16
|
"bin": {
|
|
21
|
-
"icon-font": "lib/cli.
|
|
17
|
+
"icon-font": "lib/cli.mjs"
|
|
18
|
+
},
|
|
19
|
+
"main": "./dist/icon-font-gen.mjs",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./dist/icon-font-gen.mjs"
|
|
22
22
|
},
|
|
23
|
-
"module": "dist/icon-font-gen.mjs",
|
|
24
23
|
"files": [
|
|
25
|
-
"index.js",
|
|
26
24
|
"lib",
|
|
27
25
|
"dist"
|
|
28
26
|
],
|
|
@@ -42,13 +40,13 @@
|
|
|
42
40
|
},
|
|
43
41
|
"homepage": "https://github.com/dadajam4/fastkit/tree/main/packages/icon-font-gen#readme",
|
|
44
42
|
"dependencies": {
|
|
45
|
-
"@fastkit/ev": "0.
|
|
46
|
-
"@fastkit/icon-font": "0.
|
|
47
|
-
"@fastkit/node-util": "0.
|
|
48
|
-
"@fastkit/tiny-logger": "0.
|
|
43
|
+
"@fastkit/ev": "0.10.1",
|
|
44
|
+
"@fastkit/icon-font": "0.10.1",
|
|
45
|
+
"@fastkit/node-util": "0.10.1",
|
|
46
|
+
"@fastkit/tiny-logger": "0.10.1",
|
|
49
47
|
"cac": "^6.7.11",
|
|
50
48
|
"chokidar": "^3.5.0",
|
|
51
|
-
"fs-extra": "^
|
|
49
|
+
"fs-extra": "^11.1.0",
|
|
52
50
|
"webfont": "^11.2.26"
|
|
53
51
|
}
|
|
54
52
|
}
|
|
@@ -1,404 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var path = require('path');
|
|
6
|
-
var nodeUtil = require('@fastkit/node-util');
|
|
7
|
-
var fs = require('fs-extra');
|
|
8
|
-
var chokidar = require('chokidar');
|
|
9
|
-
var ev = require('@fastkit/ev');
|
|
10
|
-
var tinyLogger = require('@fastkit/tiny-logger');
|
|
11
|
-
var cac = require('cac');
|
|
12
|
-
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; }
|
|
14
|
-
|
|
15
|
-
function _interopNamespace(e) {
|
|
16
|
-
if (e && e.__esModule) return e;
|
|
17
|
-
var n = Object.create(null);
|
|
18
|
-
if (e) {
|
|
19
|
-
for (var k in e) {
|
|
20
|
-
n[k] = e[k];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
n["default"] = e;
|
|
24
|
-
return Object.freeze(n);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
|
|
28
|
-
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
|
|
29
|
-
var chokidar__default = /*#__PURE__*/_interopDefaultLegacy(chokidar);
|
|
30
|
-
|
|
31
|
-
const ICON_FONT_FORMATS = [
|
|
32
|
-
'eot',
|
|
33
|
-
'woff',
|
|
34
|
-
'woff2',
|
|
35
|
-
'svg',
|
|
36
|
-
'ttf',
|
|
37
|
-
];
|
|
38
|
-
const ICON_FONT_FORMAT_MAP = {
|
|
39
|
-
eot: 'embedded-opentype',
|
|
40
|
-
woff2: 'woff2',
|
|
41
|
-
woff: 'woff',
|
|
42
|
-
ttf: 'truetype',
|
|
43
|
-
svg: 'svg',
|
|
44
|
-
};
|
|
45
|
-
async function resolveRawIconFontEntry(rootDir, rawEntry) {
|
|
46
|
-
const entry = {
|
|
47
|
-
...rawEntry,
|
|
48
|
-
};
|
|
49
|
-
// mdi support
|
|
50
|
-
if (entry.src === '@mdi') {
|
|
51
|
-
const installedDir = await findOrInstallMDI();
|
|
52
|
-
entry.src = path__default.join(installedDir, 'svg');
|
|
53
|
-
if (entry.fontHeight == null) {
|
|
54
|
-
entry.fontHeight = 512;
|
|
55
|
-
}
|
|
56
|
-
if (entry.descent == null) {
|
|
57
|
-
entry.descent = 64;
|
|
58
|
-
}
|
|
59
|
-
if (entry.name == null) {
|
|
60
|
-
entry.name = 'mdi';
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
const name = entry.name || path__default.basename(entry.src);
|
|
64
|
-
const fontName = entry.fontName || `${name}-icon`;
|
|
65
|
-
const dest = path__default.join(rootDir, name);
|
|
66
|
-
const prefix = entry.disablePrefix ? '' : `${name}-`;
|
|
67
|
-
return {
|
|
68
|
-
...entry,
|
|
69
|
-
name,
|
|
70
|
-
fontName,
|
|
71
|
-
prefix,
|
|
72
|
-
dest,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
function findOrInstallMDI() {
|
|
76
|
-
return nodeUtil.installPackage('@mdi/svg', { dev: true });
|
|
77
|
-
}
|
|
78
|
-
const DEFAULT_CONFIG_FILENAME = 'icon-font.config';
|
|
79
|
-
const DEFAULT_DEST_DIRNAME = '.icon-font';
|
|
80
|
-
function ceateIconFontConfig(options) {
|
|
81
|
-
return options;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const name = 'icon-font-gen';
|
|
85
|
-
const logger = new tinyLogger.TinyLogger(name);
|
|
86
|
-
const IconFontGenError = tinyLogger.createTinyError(name);
|
|
87
|
-
|
|
88
|
-
const DEFAULT_OPTIONS = {
|
|
89
|
-
formats: ['woff2'],
|
|
90
|
-
fixedWidth: true,
|
|
91
|
-
normalize: true,
|
|
92
|
-
};
|
|
93
|
-
const BANNER = `
|
|
94
|
-
/**
|
|
95
|
-
* This is auto generated file.
|
|
96
|
-
* Do not edit !!!
|
|
97
|
-
*
|
|
98
|
-
* @see: https://github.com/dadajam4/fastkit/tree/main/packages/icon-font-gen
|
|
99
|
-
*/
|
|
100
|
-
`.trim();
|
|
101
|
-
function mergeDefaults(entry) {
|
|
102
|
-
return {
|
|
103
|
-
...DEFAULT_OPTIONS,
|
|
104
|
-
...entry,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
async function generateTS(entry, ids) {
|
|
108
|
-
const code = `
|
|
109
|
-
/* eslint-disable */
|
|
110
|
-
// @ts-nocheck
|
|
111
|
-
${BANNER}
|
|
112
|
-
import type { IconName, IconNameMap } from '@fastkit/icon-font';
|
|
113
|
-
import { registerIconNames } from '@fastkit/icon-font';
|
|
114
|
-
declare module "@fastkit/icon-font" {
|
|
115
|
-
export interface IconNameMap {
|
|
116
|
-
${ids.map((id) => ` '${id}': true,`).join('\n')}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
export const ICON_NAMES = registerIconNames([
|
|
120
|
-
${ids.map((id) => `'${id}'`).join(',\n ')}
|
|
121
|
-
]);
|
|
122
|
-
export type { IconName, IconNameMap } from '@fastkit/icon-font';
|
|
123
|
-
`.trim();
|
|
124
|
-
const fileName = `${entry.name || 'icons'}.ts`;
|
|
125
|
-
const dest = path__default.resolve(entry.dest, fileName);
|
|
126
|
-
await fs__default.writeFile(dest, code);
|
|
127
|
-
return {
|
|
128
|
-
fileName,
|
|
129
|
-
dest,
|
|
130
|
-
code,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
async function generateEntry(entry) {
|
|
134
|
-
const options = mergeDefaults(entry);
|
|
135
|
-
// @TODO support empty prefix
|
|
136
|
-
// const namePrefix = options.name;
|
|
137
|
-
const cssPrefix = `icon-${options.prefix}`;
|
|
138
|
-
const hash = new nodeUtil.HashComparator(options.src, options.dest);
|
|
139
|
-
const srcHash = await hash.hasChanged();
|
|
140
|
-
if (!srcHash) {
|
|
141
|
-
logger.info(`Has not chaged files. Skip process. >>> ${options.src}`);
|
|
142
|
-
return { entry };
|
|
143
|
-
}
|
|
144
|
-
await fs__default.ensureDir(options.dest);
|
|
145
|
-
const webfont = await (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('webfont')); })).default;
|
|
146
|
-
// const { startUnicode = 0xea01 } = options;
|
|
147
|
-
const { startUnicode = 0xba01 } = options;
|
|
148
|
-
let i = startUnicode;
|
|
149
|
-
const result = await webfont({
|
|
150
|
-
files: options.src,
|
|
151
|
-
fontName: options.fontName,
|
|
152
|
-
formats: options.formats,
|
|
153
|
-
fixedWidth: options.fixedWidth,
|
|
154
|
-
centerHorizontally: options.centerHorizontally,
|
|
155
|
-
normalize: options.normalize,
|
|
156
|
-
fontHeight: options.fontHeight,
|
|
157
|
-
round: options.round,
|
|
158
|
-
descent: options.descent,
|
|
159
|
-
addHashInFontUrl: options.addHashInFontUrl,
|
|
160
|
-
glyphTransformFn: (obj) => {
|
|
161
|
-
const char = String.fromCodePoint(i);
|
|
162
|
-
obj.unicode = [char];
|
|
163
|
-
i++;
|
|
164
|
-
return obj;
|
|
165
|
-
},
|
|
166
|
-
});
|
|
167
|
-
const buildedFormats = [];
|
|
168
|
-
ICON_FONT_FORMATS.forEach((format) => {
|
|
169
|
-
const code = result[format];
|
|
170
|
-
if (code) {
|
|
171
|
-
buildedFormats.push({ format, code });
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
await Promise.all(buildedFormats.map(({ format, code }) => {
|
|
175
|
-
return fs__default.writeFile(path__default.join(options.dest, `${options.name}.${format}`), code);
|
|
176
|
-
}));
|
|
177
|
-
const glyphs = [];
|
|
178
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
179
|
-
result.glyphsData.forEach(({ metadata }) => {
|
|
180
|
-
if (!metadata)
|
|
181
|
-
return;
|
|
182
|
-
const { name, unicode } = metadata;
|
|
183
|
-
if (!unicode) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
const code = unicode[0].charCodeAt(0).toString(16);
|
|
187
|
-
glyphs.push({ name, code });
|
|
188
|
-
});
|
|
189
|
-
const urlPath = options.absolutePath ? options.dest : '.';
|
|
190
|
-
const hashSuffix = options.addHashInFontUrl ? `?${Date.now()}` : '';
|
|
191
|
-
const src = buildedFormats
|
|
192
|
-
.map(({ format }) => {
|
|
193
|
-
return `url("${urlPath}/${options.name}.${format}${hashSuffix}") format("${ICON_FONT_FORMAT_MAP[format]}")`;
|
|
194
|
-
})
|
|
195
|
-
.join(',');
|
|
196
|
-
const cssCode = `/* stylelint-disable */
|
|
197
|
-
@font-face {
|
|
198
|
-
font-family: "${options.fontName}";
|
|
199
|
-
font-display: block;
|
|
200
|
-
font-style: normal;
|
|
201
|
-
font-weight: 400;
|
|
202
|
-
src: ${src};
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
i[class^="${cssPrefix}"]:before, i[class*=" ${cssPrefix}"]:before {
|
|
206
|
-
font-family: ${options.fontName} !important;
|
|
207
|
-
font-style: normal;
|
|
208
|
-
font-weight: normal !important;
|
|
209
|
-
font-variant: normal;
|
|
210
|
-
text-transform: none;
|
|
211
|
-
text-rendering: auto;
|
|
212
|
-
line-height: 1;
|
|
213
|
-
-webkit-font-smoothing: antialiased;
|
|
214
|
-
-moz-osx-font-smoothing: grayscale;
|
|
215
|
-
}
|
|
216
|
-
${glyphs
|
|
217
|
-
.map(({ name, code }) => {
|
|
218
|
-
return `
|
|
219
|
-
.${cssPrefix}${name}:before { content: "\\${code}"; }
|
|
220
|
-
`;
|
|
221
|
-
})
|
|
222
|
-
.join('\n')}
|
|
223
|
-
`;
|
|
224
|
-
await fs__default.writeFile(path__default.join(options.dest, `${options.name}.css`), cssCode);
|
|
225
|
-
// const prefix = options.prefix ? `${options.prefix}-` : '';
|
|
226
|
-
const ids = glyphs.map(({ name }) => `${options.prefix}${name}`);
|
|
227
|
-
await generateTS(options, ids);
|
|
228
|
-
await hash.commit(srcHash);
|
|
229
|
-
return {
|
|
230
|
-
entry,
|
|
231
|
-
result,
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
async function generateIndex(dest, entries) {
|
|
235
|
-
const names = entries.map(({ name }) => name);
|
|
236
|
-
const tsCode = `
|
|
237
|
-
/* eslint-disable */
|
|
238
|
-
// @ts-nocheck
|
|
239
|
-
import './index.css';
|
|
240
|
-
${BANNER}
|
|
241
|
-
${names
|
|
242
|
-
.map((name, index) =>
|
|
243
|
-
// `import { IconName as IconName_${index}, IconNameMap as IconNameMap_${index} } from './${name}/${name}';`,
|
|
244
|
-
`import './${name}/${name}';`)
|
|
245
|
-
.join('\n')}
|
|
246
|
-
export type { IconName } from '@fastkit/icon-font';
|
|
247
|
-
export { ICON_NAMES } from '@fastkit/icon-font';
|
|
248
|
-
`.trim();
|
|
249
|
-
const tsDest = path__default.join(dest, 'index.ts');
|
|
250
|
-
await fs__default.writeFile(tsDest, tsCode);
|
|
251
|
-
const cssCode = `
|
|
252
|
-
/* stylelint-disable */
|
|
253
|
-
${BANNER}
|
|
254
|
-
${names.map((name) => `@import './${name}/${name}.css';`).join('\n')}
|
|
255
|
-
`.trim();
|
|
256
|
-
const cssDest = path__default.join(dest, 'index.css');
|
|
257
|
-
await fs__default.writeFile(cssDest, cssCode);
|
|
258
|
-
}
|
|
259
|
-
async function generate(opts) {
|
|
260
|
-
await fs__default.emptyDir(opts.dest);
|
|
261
|
-
const results = await Promise.all(opts.entries.map((entry) => resolveRawIconFontEntry(opts.dest, entry).then((entry) => generateEntry(entry))));
|
|
262
|
-
const entries = results.map(({ entry }) => entry);
|
|
263
|
-
await generateIndex(opts.dest, entries);
|
|
264
|
-
}
|
|
265
|
-
class IconFontRunnerItem extends ev.EV {
|
|
266
|
-
entry;
|
|
267
|
-
ctx;
|
|
268
|
-
_resolveEntryPromise;
|
|
269
|
-
_watcher = null;
|
|
270
|
-
watchMode;
|
|
271
|
-
// get name() {
|
|
272
|
-
// return this.entry.name;
|
|
273
|
-
// }
|
|
274
|
-
constructor(ctx, entry, watch = false) {
|
|
275
|
-
super();
|
|
276
|
-
this.ctx = ctx;
|
|
277
|
-
this.entry = entry;
|
|
278
|
-
this.watchMode = watch;
|
|
279
|
-
this.build = this.build.bind(this);
|
|
280
|
-
}
|
|
281
|
-
async run() {
|
|
282
|
-
const result = await this.build();
|
|
283
|
-
if (this.watchMode && !this._watcher) {
|
|
284
|
-
const watchDir = path__default.resolve(this.entry.src);
|
|
285
|
-
this._watcher = chokidar__default.watch(watchDir, { ignoreInitial: true });
|
|
286
|
-
this._watcher.on('all', this.build);
|
|
287
|
-
}
|
|
288
|
-
return result;
|
|
289
|
-
}
|
|
290
|
-
resolveEntry() {
|
|
291
|
-
if (!this._resolveEntryPromise) {
|
|
292
|
-
this._resolveEntryPromise = resolveRawIconFontEntry(this.ctx.dest, this.entry);
|
|
293
|
-
}
|
|
294
|
-
return this._resolveEntryPromise;
|
|
295
|
-
}
|
|
296
|
-
async build() {
|
|
297
|
-
const entry = await this.resolveEntry();
|
|
298
|
-
const result = await generateEntry(entry);
|
|
299
|
-
this.emit('build', result);
|
|
300
|
-
return { entry };
|
|
301
|
-
}
|
|
302
|
-
destroy() {
|
|
303
|
-
if (this._watcher) {
|
|
304
|
-
this._watcher.close();
|
|
305
|
-
this._watcher = null;
|
|
306
|
-
}
|
|
307
|
-
this.offAll();
|
|
308
|
-
if (this.ctx) {
|
|
309
|
-
delete this.ctx;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
class IconFontRunner extends ev.EV {
|
|
314
|
-
items = [];
|
|
315
|
-
dest;
|
|
316
|
-
// private _opts: IconFontOptions;
|
|
317
|
-
// private entries: IconFontEntry[] = [];
|
|
318
|
-
constructor(opts, watch) {
|
|
319
|
-
super();
|
|
320
|
-
// this._opts = opts;
|
|
321
|
-
this.dest = opts.dest;
|
|
322
|
-
// this.entries = opts.entries.map((entry) =>
|
|
323
|
-
// resolveRawIconFontEntry(this.outputDir, entry),
|
|
324
|
-
// );
|
|
325
|
-
opts.entries.forEach((entry) => {
|
|
326
|
-
const item = new IconFontRunnerItem(this, entry, watch);
|
|
327
|
-
item.on('build', (result) => {
|
|
328
|
-
this.emit('build', { item, result });
|
|
329
|
-
});
|
|
330
|
-
this.items.push(item);
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
async buildIndex() {
|
|
334
|
-
const entries = await Promise.all(this.items.map((item) => item.resolveEntry()));
|
|
335
|
-
return generateIndex(this.dest, entries);
|
|
336
|
-
}
|
|
337
|
-
async run() {
|
|
338
|
-
await fs__default.ensureDir(this.dest);
|
|
339
|
-
await Promise.all([
|
|
340
|
-
this.buildIndex(),
|
|
341
|
-
Promise.all(this.items.map((item) => item.run())),
|
|
342
|
-
]);
|
|
343
|
-
}
|
|
344
|
-
destroy() {
|
|
345
|
-
this.items.forEach((item) => item.destroy());
|
|
346
|
-
this.items.length = 0;
|
|
347
|
-
this.offAll();
|
|
348
|
-
delete this._opts;
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
const { version } = require('../package.json');
|
|
353
|
-
function cli() {
|
|
354
|
-
const cli = cac.cac('icon-font');
|
|
355
|
-
cli
|
|
356
|
-
.option('-d, --dest <string>', 'Destination for generated fonts.', {
|
|
357
|
-
default: path__default.join(process.cwd(), `${DEFAULT_DEST_DIRNAME}`),
|
|
358
|
-
})
|
|
359
|
-
.command('[config file path]', `
|
|
360
|
-
Path of the configuration file. (default: [your package directory]${path__default.sep}${DEFAULT_CONFIG_FILENAME})
|
|
361
|
-
You must export default <IconFontConfig>.
|
|
362
|
-
|
|
363
|
-
e.g.
|
|
364
|
-
\`\`\`
|
|
365
|
-
import { ceateIconFontConfig } from '@fastkit/icon-font-gen';
|
|
366
|
-
|
|
367
|
-
export default ceateIconFontConfig({ ... });
|
|
368
|
-
\`\`\`
|
|
369
|
-
`.trim())
|
|
370
|
-
.action(async (configPath, options) => {
|
|
371
|
-
if (!configPath) {
|
|
372
|
-
const pkgDir = await nodeUtil.findPackageDir();
|
|
373
|
-
if (!pkgDir) {
|
|
374
|
-
throw new IconFontGenError('missing package directory.');
|
|
375
|
-
}
|
|
376
|
-
configPath = path__default.join(pkgDir, DEFAULT_CONFIG_FILENAME);
|
|
377
|
-
}
|
|
378
|
-
const dest = path__default.resolve(options.dest);
|
|
379
|
-
const mod = await nodeUtil.esbuildRequire(configPath);
|
|
380
|
-
const config = mod.exports.default;
|
|
381
|
-
await generate({
|
|
382
|
-
...config,
|
|
383
|
-
dest,
|
|
384
|
-
});
|
|
385
|
-
});
|
|
386
|
-
cli.help();
|
|
387
|
-
cli.version(version);
|
|
388
|
-
cli.parse();
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
exports.DEFAULT_CONFIG_FILENAME = DEFAULT_CONFIG_FILENAME;
|
|
392
|
-
exports.DEFAULT_DEST_DIRNAME = DEFAULT_DEST_DIRNAME;
|
|
393
|
-
exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
|
|
394
|
-
exports.ICON_FONT_FORMATS = ICON_FONT_FORMATS;
|
|
395
|
-
exports.ICON_FONT_FORMAT_MAP = ICON_FONT_FORMAT_MAP;
|
|
396
|
-
exports.IconFontRunner = IconFontRunner;
|
|
397
|
-
exports.IconFontRunnerItem = IconFontRunnerItem;
|
|
398
|
-
exports.ceateIconFontConfig = ceateIconFontConfig;
|
|
399
|
-
exports.cli = cli;
|
|
400
|
-
exports.generate = generate;
|
|
401
|
-
exports.generateEntry = generateEntry;
|
|
402
|
-
exports.generateIndex = generateIndex;
|
|
403
|
-
exports.mergeDefaults = mergeDefaults;
|
|
404
|
-
exports.resolveRawIconFontEntry = resolveRawIconFontEntry;
|
package/index.js
DELETED
package/lib/cli.js
DELETED