Skip to content

Commit a6b5820

Browse files
fix: esm import (#172)
* chore: add script to patch esm * refactor: replace css with postcss * fix: fix cr issue * chore: update version * refactor: use tsc-alias to resolve * chore: update write package.json shell Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 51a4899 commit a6b5820

4 files changed

Lines changed: 35 additions & 38 deletions

File tree

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/infographic",
3-
"version": "0.2.10",
3+
"version": "0.2.11",
44
"description": "An Infographic Generation and Rendering Framework, bring words to life!",
55
"keywords": [
66
"antv",
@@ -50,7 +50,7 @@
5050
"scripts": {
5151
"build": "run-s clean build:esm build:cjs build:umd size",
5252
"build:cjs": "tsc --module commonjs --outDir lib",
53-
"build:esm": "tsc --module ESNext --outDir esm",
53+
"build:esm": "tsc --module ESNext --outDir esm && tsc-alias -p tsconfig.json --outDir esm && node -e \"require('fs').writeFileSync('esm/package.json', JSON.stringify({ type: 'module' }))\"",
5454
"build:site": "npm --prefix site run build:static",
5555
"build:umd": "vite build",
5656
"build:watch": "tsc -p tsconfig.json --watch",
@@ -99,14 +99,14 @@
9999
"dependencies": {
100100
"@antv/hierarchy": "^0.7.0",
101101
"@antv/layout": "^2.0.0-beta.0",
102-
"css": "^3.0.0",
103102
"culori": "^4.0.2",
104103
"d3": "^7.9.0",
105104
"eventemitter3": "^5.0.1",
106105
"flru": "^1.0.2",
107106
"linkedom": "^0.18.12",
108107
"lodash-es": "^4.17.21",
109108
"measury": "^0.1.5",
109+
"postcss": "^8.5.6",
110110
"roughjs": "^4.6.6",
111111
"round-polygon": "^0.6.7",
112112
"tinycolor2": "^1.6.0"
@@ -119,7 +119,6 @@
119119
"@rollup/plugin-terser": "^0.4.4",
120120
"@rollup/plugin-typescript": "^12.3.0",
121121
"@size-limit/file": "^11.2.0",
122-
"@types/css": "^0.0.38",
123122
"@types/culori": "^4.0.1",
124123
"@types/d3": "^7.4.3",
125124
"@types/jsdom": "^21.1.7",
@@ -144,6 +143,7 @@
144143
"rollup": "^4.53.2",
145144
"rollup-plugin-visualizer": "^6.0.5",
146145
"size-limit": "^11.2.0",
146+
"tsc-alias": "^1.8.16",
147147
"typescript": "^5.9.2",
148148
"typescript-eslint": "^8.43.0",
149149
"vite": "^7.1.5",

src/exporter/font.ts

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { FontFace as CSSFontFace, Declaration, Stylesheet } from 'css';
2-
// @ts-expect-error ignore
3-
import parse from 'css/lib/parse';
1+
import postcss from 'postcss';
42
import { getFontURLs, getWoff2BaseURL } from '../renderer';
53
import {
64
createElement,
@@ -116,27 +114,36 @@ function collectUsedFonts(svg: SVGSVGElement) {
116114
/**
117115
* 解析给定 font-family 对应的 CSS @font-face
118116
*/
119-
export async function parseFontFamily(fontFamily: string) {
117+
async function parseFontFamily(fontFamily: string) {
120118
const urls = getFontURLs(fontFamily);
121-
122119
const fontFaces: Partial<FontFaceAttributes>[] = [];
123120

124121
await Promise.allSettled(
125122
urls.map(async (url) => {
126-
const css = await fetchWithCache(url)
123+
const cssText = await fetchWithCache(url)
127124
.then((res) => res.text())
128-
.then((text) => parse(text) as Stylesheet)
129125
.catch(() => {
130-
console.error(`Failed to fetch or parse font CSS: ${url}`);
126+
console.error(`Failed to fetch font CSS: ${url}`);
131127
return null;
132128
});
133129

134-
css?.stylesheet?.rules.forEach((rule) => {
135-
if (rule.type === 'font-face') {
136-
const fontFace = parseFontFace(rule as CSSFontFace);
137-
fontFaces.push(fontFace);
138-
}
139-
});
130+
if (!cssText) return;
131+
132+
try {
133+
const root = postcss.parse(cssText);
134+
135+
root.walkAtRules('font-face', (rule) => {
136+
const fontFace: Record<string, string> = {};
137+
138+
rule.walkDecls((decl) => {
139+
fontFace[decl.prop] = decl.value;
140+
});
141+
142+
fontFaces.push(fontFace as Partial<FontFaceAttributes>);
143+
});
144+
} catch (error) {
145+
console.error(`Failed to parse CSS: ${url}`, error);
146+
}
140147
}),
141148
);
142149

@@ -164,24 +171,6 @@ export function getActualLoadedFontFace(fontFamily: string) {
164171
return fonts;
165172
}
166173

167-
/**
168-
* 从 css 的 FontFace 规则中提取声明
169-
*/
170-
function parseFontFace(rule: CSSFontFace) {
171-
const declarations = (rule.declarations || []) as Declaration[];
172-
const attrs: Record<string, string> = {};
173-
174-
declarations.forEach((declaration) => {
175-
const { property, value } = declaration;
176-
if (property && value) {
177-
attrs[property] = value;
178-
}
179-
});
180-
181-
// 这里返回 Partial,后面统一 normalize
182-
return attrs as Partial<FontFaceAttributes>;
183-
}
184-
185174
/**
186175
* 将不完整的 FontFaceAttributes 补全为完整结构,给后续逻辑使用
187176
*/

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const VERSION = '0.2.10';
1+
export const VERSION = '0.2.11';

tsconfig.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,13 @@
2222
}
2323
},
2424
"include": ["src"],
25-
"exclude": ["node_modules", "dist", "lib", "esm"]
25+
"exclude": ["node_modules", "dist", "lib", "esm"],
26+
"tsc-alias": {
27+
"verbose": false,
28+
"resolveFullPaths": true,
29+
"fileExtensions": {
30+
"inputGlob": "{js,jsx,mjs}",
31+
"outputCheck": ["js", "json", "jsx", "mjs"]
32+
}
33+
}
2634
}

0 commit comments

Comments
 (0)