Skip to content

Commit fa413d7

Browse files
committed
feat: support esm import
1 parent b971401 commit fa413d7

File tree

16 files changed

+722
-333
lines changed

16 files changed

+722
-333
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ node
1717
.vscode
1818
woff2/woff2
1919
woff2src/woff2*
20-
lib
20+
/lib
2121
baiduHealth-deflate.woff
2222
package-lock.json
2323
test/example/output

ESM_USAGE.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ The updated library supports both CommonJS and ESM module formats. In modern bun
2121
import fonteditorCore from 'fonteditor-core';
2222

2323
// Or import specific modules
24-
import { Font, woff2 } from 'fonteditor-core';
24+
import { createFont, woff2 } from 'fonteditor-core';
2525
```
2626

2727
## TypeScript Support
2828

2929
The library includes TypeScript declarations. You can use it in TypeScript projects without any additional setup:
3030

3131
```typescript
32-
import fonteditorCore, { Font, woff2 } from 'fonteditor-core';
32+
import fonteditorCore, { createFont, woff2 } from 'fonteditor-core';
3333
import { Buffer } from 'buffer'; // If needed in browser environments
3434

3535
// Using Font with type safety
36-
const font = Font.create(buffer, {
36+
const font = createFont(buffer, {
3737
type: 'ttf',
3838
hinting: true,
3939
subset: [65, 66, 67], // A, B, C
@@ -46,7 +46,7 @@ console.log(fontObject.head.xMin);
4646
// Using woff2 with type safety
4747
async function convertFont(ttfBuffer: ArrayBuffer) {
4848
await woff2.init('/woff2.wasm');
49-
49+
5050
if (woff2.isInited()) {
5151
const woff2Buffer = fonteditorCore.ttftowoff2(ttfBuffer);
5252
return woff2Buffer;
@@ -78,15 +78,15 @@ import { woff2 } from 'fonteditor-core';
7878

7979
function FontComponent() {
8080
const [isWoff2Ready, setIsWoff2Ready] = useState(false);
81-
81+
8282
useEffect(() => {
8383
// Initialize woff2 module
8484
woff2.init('/woff2.wasm')
8585
.then(() => {
8686
setIsWoff2Ready(true);
8787
});
8888
}, []);
89-
89+
9090
// Component logic...
9191
}
9292
```
@@ -100,14 +100,14 @@ import { woff2 } from 'fonteditor-core';
100100
export default {
101101
setup() {
102102
const isWoff2Ready = ref(false);
103-
103+
104104
onMounted(() => {
105105
woff2.init('/woff2.wasm')
106106
.then(() => {
107107
isWoff2Ready.value = true;
108108
});
109109
});
110-
110+
111111
// Component logic...
112112
}
113113
}
@@ -126,7 +126,7 @@ To prevent SSR issues:
126126

127127
```javascript
128128
// Use the 'use client' directive in Next.js App Router
129-
'use client';
129+
'use client';
130130

131131
import { useEffect } from 'react';
132132
import { Font } from 'fonteditor-core';
@@ -229,4 +229,4 @@ If you encounter issues with the library in modern bundlers:
229229
1. Make sure you're using the latest version of fonteditor-core
230230
2. Check that the WASM file is properly accessible in your public assets
231231
3. For Node.js environments, ensure you're using a version that supports ESM
232-
4. In case of bundling issues, try adding fonteditor-core to your bundler's transpilation list
232+
4. In case of bundling issues, try adding fonteditor-core to your bundler's transpilation list

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Read and write sfnt font like ttf, woff, woff2, eot, svg, otf.
2626

2727
```javascript
2828
// read font file
29-
import {Font} from 'fonteditor-core';
29+
import {createFont} from 'fonteditor-core';
3030
import fs from 'fs';
3131

3232
const buffer = fs.readFileSync('font.ttf');
3333
// read font data, support format:
3434
// - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer
3535
// - for svg, support string or Document(parsed svg)
36-
const font = Font.create(buffer, {
36+
const font = createFont(buffer, {
3737
// support ttf, woff, woff2, eot, otf, svg
3838
type: 'ttf',
3939
// only read `a`, `b` glyphs
@@ -126,20 +126,22 @@ This library supports both CommonJS and ES Modules. For detailed information on
126126

127127
```javascript
128128
// ESM import
129-
import fonteditorCore, { Font, woff2 } from 'fonteditor-core';
129+
import fonteditorCore, { createFont, woff2 } from 'fonteditor-core';
130+
131+
createFont(buffer, options);
130132
```
131133

132134
### woff2
133135

134136
**Notice:** woff2 use wasm build of google woff2, before read and write `woff2`, we should first call `woff2.init()`.
135137

136138
```javascript
137-
import {Font, woff2} from 'fonteditor-core';
139+
import {createFont, woff2} from 'fonteditor-core';
138140

139141
// in nodejs
140142
woff2.init().then(() => {
141143
// read woff2
142-
const font = Font.create(buffer, {
144+
const font = createFont(buffer, {
143145
type: 'woff2'
144146
});
145147
// write woff2
@@ -149,7 +151,7 @@ woff2.init().then(() => {
149151
// in browser
150152
woff2.init('/assets/woff2.wasm').then(() => {
151153
// read woff2
152-
const font = Font.createEmpty();
154+
const font = createFont();
153155
// write woff2
154156
const arrayBuffer = font.write({type: 'woff2'});
155157
});

index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ export namespace FontEditor {
352352
*/
353353
static toBase64(buffer: FontInput): string;
354354

355+
/**
356+
* font data
357+
* @deprecated use font.get() instead
358+
*/
359+
data: TTF.TTFObject;
360+
355361
/**
356362
* read empty ttf object
357363
*/
@@ -466,6 +472,17 @@ export namespace FontEditor {
466472
* Font class
467473
*/
468474
Font: typeof Font;
475+
476+
/**
477+
* create font object with font data
478+
*
479+
* @param buffer font data, support format
480+
* - for ttf, otf, woff, woff2, support ArrayBuffer, Buffer
481+
* - for svg, support string or Document(parsed svg)
482+
* @param options font read options
483+
*/
484+
createFont(buffer: FontInput, options: FontReadOptions): Font;
485+
469486
/**
470487
* woff2 module
471488
*/
@@ -619,6 +636,7 @@ export namespace FontEditor {
619636
// Named exports
620637
export const Font: typeof FontEditor.Font;
621638
export const woff2: FontEditor.Woff2;
639+
export const createFont: FontEditor.Core["createFont"];
622640

623641
// Default export
624642
declare const fonteditorCore: FontEditor.Core;

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fonteditor-core",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "fonts (ttf, woff, woff2, eot, svg, otf) parse, write, transform, glyph adjust.",
55
"keywords": [
66
"sfnt",
@@ -40,12 +40,13 @@
4040
"scripts": {
4141
"precommit": "npm run lint",
4242
"publish:npm": "npm run build && npm publish --registry=https://registry.npmjs.org/",
43-
"test": "npm run test:browser && npm run test:node",
43+
"test": "npm run test:browser && npm run test:node && npm run test:esm",
4444
"test:browser": "mocha --require @babel/register test/spec/*.spec.js test/spec/**/*.spec.js",
4545
"test:node": "npm run build && mocha test/node-spec/**/*.spec.js ",
46+
"test:esm": "cd test/esm && npm i && mocha --extension mjs index.mjs",
4647
"test:example": "cd test/example && npm i && npm run test",
4748
"dev": "webpack-dev-server --open --config ./config/webpack.dev.js",
48-
"build": "babel src --out-dir lib && cp src/main.mjs lib/",
49+
"build": "babel src --out-dir lib",
4950
"lint": "eslint ./src ./test",
5051
"lint:fix": "eslint --fix ./src ./test"
5152
},
@@ -70,11 +71,11 @@
7071
"woff2"
7172
],
7273
"main": "./lib/main.js",
73-
"module": "./lib/main.mjs",
74+
"module": "./lib/main.esm.js",
7475
"types": "./index.d.ts",
7576
"exports": {
7677
".": {
77-
"import": "./lib/main.mjs",
78+
"import": "./lib/main.esm.js",
7879
"require": "./lib/main.js",
7980
"types": "./index.d.ts",
8081
"default": "./lib/main.js"
@@ -95,7 +96,7 @@
9596
"html-webpack-plugin": "^3.2.0",
9697
"husky": "^4.3.0",
9798
"lint-staged": "^10.4.0",
98-
"mocha": "^6.2.1",
99+
"mocha": "^11.7.1",
99100
"pako": "^1.0.10",
100101
"style-loader": "^1.0.0",
101102
"webpack": "^4.41.1",

src/main.esm.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @file 主函数
3+
* @author mengke01(kekee000@gmail.com)
4+
*/
5+
6+
import {Font, createFont} from './ttf/font';
7+
import TTF from './ttf/ttf';
8+
import TTFReader from './ttf/ttfreader';
9+
import TTFWriter from './ttf/ttfwriter';
10+
import ttf2eot from './ttf/ttf2eot';
11+
import eot2ttf from './ttf/eot2ttf';
12+
import ttf2woff from './ttf/ttf2woff';
13+
import woff2ttf from './ttf/woff2ttf';
14+
import ttf2svg from './ttf/ttf2svg';
15+
import svg2ttfobject from './ttf/svg2ttfobject';
16+
import Reader from './ttf/reader';
17+
import Writer from './ttf/writer';
18+
import OTFReader from './ttf/otfreader';
19+
import otf2ttfobject from './ttf/otf2ttfobject';
20+
import ttf2base64 from './ttf/ttf2base64';
21+
import ttf2icon from './ttf/ttf2icon';
22+
import ttftowoff2 from './ttf/ttftowoff2';
23+
import woff2tottf from './ttf/woff2tottf';
24+
import woff2 from '../woff2/index';
25+
import bufferUtil from './nodejs/buffer';
26+
27+
const modules = {
28+
createFont,
29+
Font,
30+
TTF,
31+
TTFReader,
32+
TTFWriter,
33+
ttf2eot,
34+
eot2ttf,
35+
ttf2woff,
36+
woff2ttf,
37+
ttf2svg,
38+
svg2ttfobject,
39+
Reader,
40+
Writer,
41+
OTFReader,
42+
otf2ttfobject,
43+
ttf2base64,
44+
ttf2icon,
45+
ttftowoff2,
46+
woff2tottf,
47+
woff2,
48+
toArrayBuffer: bufferUtil.toArrayBuffer,
49+
toBuffer: bufferUtil.toBuffer,
50+
};
51+
52+
// Export named exports for ESM
53+
export {Font, woff2, createFont};
54+
55+
// Export default object
56+
export default modules;

src/main.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author mengke01(kekee000@gmail.com)
44
*/
55

6-
import { Font } from './ttf/font';
6+
import {Font, createFont} from './ttf/font';
77
import TTF from './ttf/ttf';
88
import TTFReader from './ttf/ttfreader';
99
import TTFWriter from './ttf/ttfwriter';
@@ -24,7 +24,8 @@ import woff2tottf from './ttf/woff2tottf';
2424
import woff2 from '../woff2/index';
2525
import bufferUtil from './nodejs/buffer';
2626

27-
const modules = {
27+
module.exports = {
28+
createFont,
2829
Font,
2930
TTF,
3031
TTFReader,
@@ -47,14 +48,3 @@ const modules = {
4748
toArrayBuffer: bufferUtil.toArrayBuffer,
4849
toBuffer: bufferUtil.toBuffer,
4950
};
50-
51-
// Export named exports for ESM
52-
export { Font, woff2 };
53-
54-
// Export default object
55-
export default modules;
56-
57-
if (typeof exports !== 'undefined') {
58-
// eslint-disable-next-line import/no-commonjs
59-
module.exports = modules;
60-
}

src/main.mjs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/ttf/font.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,10 @@ Font.toBase64 = function (buffer) {
345345
return bytes2base64(buffer);
346346
};
347347

348-
// 기존 방식: export default Font
349-
// 새로운 방식: 직접 export
350-
export { Font };
351-
export default Font;
348+
function createFont(buffer, options) {
349+
return new Font(buffer, options);
350+
}
351+
352+
export {Font, createFont};
353+
354+
export default Font;

0 commit comments

Comments
 (0)