@fontmin-plugin/plugin 0.1.0 → 0.1.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/index.cjs ADDED
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.tsx
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => index_default,
34
+ frameworksConfig: () => frameworksConfig,
35
+ handleImport: () => handleImport,
36
+ unplugin: () => unplugin,
37
+ unpluginFactory: () => unpluginFactory
38
+ });
39
+ module.exports = __toCommonJS(index_exports);
40
+ var import_node_path = __toESM(require("path"), 1);
41
+ var import_unplugin = require("unplugin");
42
+
43
+ // src/core.ts
44
+ var import_node_util = require("util");
45
+ var import_fontmin = __toESM(require("fontmin"), 1);
46
+ var import_radash = require("radash");
47
+ var presets = {
48
+ numbers: "0123456789",
49
+ lowerCase: "abcdefghijklmnopqrstuvwxyz",
50
+ upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
51
+ symbols: "!@#$%^&*()_+-=[]{}|;:,.<>?`~",
52
+ allBasic: String.fromCharCode.apply(void 0, (0, import_radash.list)(33, 126))
53
+ };
54
+ var classNameCounter = 0;
55
+ var generateClassName = () => {
56
+ classNameCounter++;
57
+ return `fontmin_${classNameCounter}_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
58
+ };
59
+ var loadAndProcessFont = async (config) => {
60
+ const text = `${config.presets?.map((preset) => presets[preset] || "").join("") || ""}${config.glyph || ""}`;
61
+ if (!text || !config.fontPath) {
62
+ console.error(
63
+ new Error("No text or font path to process, Please check your config")
64
+ );
65
+ return "";
66
+ }
67
+ const fontmin = new import_fontmin.default().src(config.fontPath).use(import_fontmin.default.glyph({ text, hinting: false }));
68
+ const runAsync = (0, import_node_util.promisify)(fontmin.run.bind(fontmin));
69
+ const files = await runAsync();
70
+ if (!files?.[0]) {
71
+ console.error(new Error("Font processing failed"));
72
+ return "";
73
+ }
74
+ const buffer = files[0].contents;
75
+ return buffer.toString("base64");
76
+ };
77
+ async function createFontStyle(config) {
78
+ try {
79
+ const className = generateClassName();
80
+ const base64 = await loadAndProcessFont(config);
81
+ return {
82
+ fontClassName: className,
83
+ fontBase64: base64
84
+ };
85
+ } catch (error) {
86
+ const message = error instanceof Error ? error.message : "Unknown error";
87
+ throw new Error(`Failed to create font style: ${message}`);
88
+ }
89
+ }
90
+
91
+ // src/index.tsx
92
+ var importerPrefix = "@fontmin-plugin/";
93
+ var frameworksConfig = {
94
+ react: {
95
+ prefix: `${importerPrefix}react/`,
96
+ generateModule: (fontVarString) => `
97
+ import { createElement } from 'react';
98
+ import FontminText from '@fontmin-plugin/react';
99
+ const fontConfig = ${fontVarString};
100
+ export default (props) => createElement(FontminText, { ...fontConfig, ...props });
101
+ `
102
+ },
103
+ vue: {
104
+ prefix: `${importerPrefix}vue/`,
105
+ generateModule: (fontVarString) => `
106
+ import { h } from 'vue';
107
+ import FontminText from '@fontmin-plugin/vue';
108
+ const fontConfig = ${fontVarString};
109
+ export default h(FontminText, { ...fontConfig });
110
+ `
111
+ }
112
+ };
113
+ async function handleImport(groupConfig, request, context) {
114
+ for (const [_, config] of Object.entries(frameworksConfig)) {
115
+ if (request.startsWith(config.prefix)) {
116
+ const groupName = request.substring(config.prefix.length);
117
+ const relativeModulePath = `node_modules/${config.prefix}${groupName}.js`;
118
+ const absoluteModulePath = import_node_path.default.resolve(context, relativeModulePath);
119
+ if (!groupConfig[groupName]) {
120
+ throw new Error(`Font group ${groupName} not found`);
121
+ }
122
+ const curConfig = groupConfig[groupName];
123
+ const fontResult = await createFontStyle(curConfig);
124
+ const fontVarString = `{ fontClassName: "${fontResult.fontClassName}", fontBase64: "${fontResult.fontBase64}", fontText: "${curConfig.glyph}" }`;
125
+ const moduleContent = config.generateModule(fontVarString);
126
+ return {
127
+ moduleContent,
128
+ absoluteModulePath
129
+ };
130
+ }
131
+ }
132
+ return {
133
+ moduleContent: void 0,
134
+ absoluteModulePath: void 0
135
+ };
136
+ }
137
+ var unpluginFactory = (options) => {
138
+ const groups = options?.groups || {};
139
+ const virtualModules = /* @__PURE__ */ new Map();
140
+ return {
141
+ name: "fontmin-plugin",
142
+ enforce: "pre",
143
+ async resolveId(source, importer) {
144
+ const { moduleContent, absoluteModulePath } = await handleImport(
145
+ groups,
146
+ source,
147
+ importer ? process.cwd() : ""
148
+ );
149
+ if (absoluteModulePath) {
150
+ virtualModules.set(source, moduleContent || "");
151
+ return source;
152
+ }
153
+ return null;
154
+ },
155
+ load(id) {
156
+ if (virtualModules.has(id)) {
157
+ return virtualModules.get(id);
158
+ }
159
+ return null;
160
+ },
161
+ loadInclude(id) {
162
+ return id.startsWith(importerPrefix);
163
+ }
164
+ };
165
+ };
166
+ var unplugin = (0, import_unplugin.createUnplugin)(unpluginFactory);
167
+ var index_default = unplugin;
168
+ // Annotate the CommonJS export names for ESM import in node:
169
+ 0 && (module.exports = {
170
+ frameworksConfig,
171
+ handleImport,
172
+ unplugin,
173
+ unpluginFactory
174
+ });
@@ -0,0 +1,35 @@
1
+ import * as _unplugin from 'unplugin';
2
+ import { UnpluginFactory } from 'unplugin';
3
+
4
+ declare const presets: {
5
+ numbers: string;
6
+ lowerCase: string;
7
+ upperCase: string;
8
+ symbols: string;
9
+ allBasic: string;
10
+ };
11
+ interface FontConfig {
12
+ fontPath: string;
13
+ glyph?: string;
14
+ presets?: Array<keyof typeof presets>;
15
+ }
16
+
17
+ interface Options {
18
+ groups?: Record<string, FontConfig>;
19
+ }
20
+ interface FrameworkConfig {
21
+ prefix: string;
22
+ generateModule: (fontVarString: string) => string;
23
+ }
24
+ declare const frameworksConfig: Record<'react' | 'vue', FrameworkConfig>;
25
+ declare function handleImport(groupConfig: Record<string, FontConfig>, request: string, context: string): Promise<{
26
+ moduleContent: string;
27
+ absoluteModulePath: string;
28
+ } | {
29
+ moduleContent: undefined;
30
+ absoluteModulePath: undefined;
31
+ }>;
32
+ declare const unpluginFactory: UnpluginFactory<Options | undefined>;
33
+ declare const unplugin: _unplugin.UnpluginInstance<Options | undefined, boolean>;
34
+
35
+ export { type Options, unplugin as default, frameworksConfig, handleImport, unplugin, unpluginFactory };
package/dist/index.d.ts CHANGED
@@ -1,20 +1,35 @@
1
- import type { FontConfig } from '@fontmin-plugin/core';
2
- import type { UnpluginFactory } from 'unplugin';
3
- export interface Options {
1
+ import * as _unplugin from 'unplugin';
2
+ import { UnpluginFactory } from 'unplugin';
3
+
4
+ declare const presets: {
5
+ numbers: string;
6
+ lowerCase: string;
7
+ upperCase: string;
8
+ symbols: string;
9
+ allBasic: string;
10
+ };
11
+ interface FontConfig {
12
+ fontPath: string;
13
+ glyph?: string;
14
+ presets?: Array<keyof typeof presets>;
15
+ }
16
+
17
+ interface Options {
4
18
  groups?: Record<string, FontConfig>;
5
19
  }
6
20
  interface FrameworkConfig {
7
21
  prefix: string;
8
22
  generateModule: (fontVarString: string) => string;
9
23
  }
10
- export declare const frameworksConfig: Record<'react' | 'vue', FrameworkConfig>;
11
- export declare function handleImport(groupConfig: Record<string, FontConfig>, request: string, context: string): Promise<{
24
+ declare const frameworksConfig: Record<'react' | 'vue', FrameworkConfig>;
25
+ declare function handleImport(groupConfig: Record<string, FontConfig>, request: string, context: string): Promise<{
12
26
  moduleContent: string;
13
27
  absoluteModulePath: string;
14
28
  } | {
15
29
  moduleContent: undefined;
16
30
  absoluteModulePath: undefined;
17
31
  }>;
18
- export declare const unpluginFactory: UnpluginFactory<Options | undefined>;
19
- export declare const unplugin: import("unplugin").UnpluginInstance<Options | undefined, boolean>;
20
- export default unplugin;
32
+ declare const unpluginFactory: UnpluginFactory<Options | undefined>;
33
+ declare const unplugin: _unplugin.UnpluginInstance<Options | undefined, boolean>;
34
+
35
+ export { type Options, unplugin as default, frameworksConfig, handleImport, unplugin, unpluginFactory };
package/dist/index.js CHANGED
@@ -1,79 +1,136 @@
1
- import path from 'node:path';
2
- import { createFontStyle } from '@fontmin-plugin/core';
3
- import { createUnplugin } from 'unplugin';
4
- const importerPrefix = '@fontmin-plugin/';
5
- export const frameworksConfig = {
6
- react: {
7
- prefix: `${importerPrefix}react/`,
8
- generateModule: (fontVarString) => `
1
+ // src/index.tsx
2
+ import path from "path";
3
+ import { createUnplugin } from "unplugin";
4
+
5
+ // src/core.ts
6
+ import { promisify } from "util";
7
+ import Fontmin from "fontmin";
8
+ import { list } from "radash";
9
+ var presets = {
10
+ numbers: "0123456789",
11
+ lowerCase: "abcdefghijklmnopqrstuvwxyz",
12
+ upperCase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
13
+ symbols: "!@#$%^&*()_+-=[]{}|;:,.<>?`~",
14
+ allBasic: String.fromCharCode.apply(void 0, list(33, 126))
15
+ };
16
+ var classNameCounter = 0;
17
+ var generateClassName = () => {
18
+ classNameCounter++;
19
+ return `fontmin_${classNameCounter}_${Date.now()}_${Math.random().toString(36).slice(2, 6)}`;
20
+ };
21
+ var loadAndProcessFont = async (config) => {
22
+ const text = `${config.presets?.map((preset) => presets[preset] || "").join("") || ""}${config.glyph || ""}`;
23
+ if (!text || !config.fontPath) {
24
+ console.error(
25
+ new Error("No text or font path to process, Please check your config")
26
+ );
27
+ return "";
28
+ }
29
+ const fontmin = new Fontmin().src(config.fontPath).use(Fontmin.glyph({ text, hinting: false }));
30
+ const runAsync = promisify(fontmin.run.bind(fontmin));
31
+ const files = await runAsync();
32
+ if (!files?.[0]) {
33
+ console.error(new Error("Font processing failed"));
34
+ return "";
35
+ }
36
+ const buffer = files[0].contents;
37
+ return buffer.toString("base64");
38
+ };
39
+ async function createFontStyle(config) {
40
+ try {
41
+ const className = generateClassName();
42
+ const base64 = await loadAndProcessFont(config);
43
+ return {
44
+ fontClassName: className,
45
+ fontBase64: base64
46
+ };
47
+ } catch (error) {
48
+ const message = error instanceof Error ? error.message : "Unknown error";
49
+ throw new Error(`Failed to create font style: ${message}`);
50
+ }
51
+ }
52
+
53
+ // src/index.tsx
54
+ var importerPrefix = "@fontmin-plugin/";
55
+ var frameworksConfig = {
56
+ react: {
57
+ prefix: `${importerPrefix}react/`,
58
+ generateModule: (fontVarString) => `
9
59
  import { createElement } from 'react';
10
60
  import FontminText from '@fontmin-plugin/react';
11
61
  const fontConfig = ${fontVarString};
12
62
  export default (props) => createElement(FontminText, { ...fontConfig, ...props });
13
- `,
14
- },
15
- vue: {
16
- prefix: `${importerPrefix}vue/`,
17
- generateModule: (fontVarString) => `
63
+ `
64
+ },
65
+ vue: {
66
+ prefix: `${importerPrefix}vue/`,
67
+ generateModule: (fontVarString) => `
18
68
  import { h } from 'vue';
19
69
  import FontminText from '@fontmin-plugin/vue';
20
70
  const fontConfig = ${fontVarString};
21
71
  export default h(FontminText, { ...fontConfig });
22
- `,
23
- },
72
+ `
73
+ }
24
74
  };
25
- export async function handleImport(groupConfig, request, context) {
26
- // 遍历frameworks配置,检查请求是否匹配任何一个前缀
27
- for (const [_, config] of Object.entries(frameworksConfig)) {
28
- if (request.startsWith(config.prefix)) {
29
- // 从请求路径提取组名
30
- const groupName = request.substring(config.prefix.length);
31
- // 为该路径创建一个虚拟模块文件
32
- // Use absolute path for virtual module
33
- const relativeModulePath = `node_modules/${config.prefix}${groupName}.js`;
34
- const absoluteModulePath = path.resolve(context, relativeModulePath);
35
- if (!groupConfig[groupName]) {
36
- throw new Error(`Font group ${groupName} not found`);
37
- }
38
- const curConfig = groupConfig[groupName];
39
- const fontResult = await createFontStyle(curConfig);
40
- const fontVarString = `{ fontClassName: "${fontResult.fontClassName}", fontBase64: "${fontResult.fontBase64}", fontText: "${curConfig.glyph}" }`;
41
- const moduleContent = config.generateModule(fontVarString);
42
- return {
43
- moduleContent,
44
- absoluteModulePath,
45
- };
46
- }
75
+ async function handleImport(groupConfig, request, context) {
76
+ for (const [_, config] of Object.entries(frameworksConfig)) {
77
+ if (request.startsWith(config.prefix)) {
78
+ const groupName = request.substring(config.prefix.length);
79
+ const relativeModulePath = `node_modules/${config.prefix}${groupName}.js`;
80
+ const absoluteModulePath = path.resolve(context, relativeModulePath);
81
+ if (!groupConfig[groupName]) {
82
+ throw new Error(`Font group ${groupName} not found`);
83
+ }
84
+ const curConfig = groupConfig[groupName];
85
+ const fontResult = await createFontStyle(curConfig);
86
+ const fontVarString = `{ fontClassName: "${fontResult.fontClassName}", fontBase64: "${fontResult.fontBase64}", fontText: "${curConfig.glyph}" }`;
87
+ const moduleContent = config.generateModule(fontVarString);
88
+ return {
89
+ moduleContent,
90
+ absoluteModulePath
91
+ };
47
92
  }
48
- return {
49
- moduleContent: undefined,
50
- absoluteModulePath: undefined,
51
- };
93
+ }
94
+ return {
95
+ moduleContent: void 0,
96
+ absoluteModulePath: void 0
97
+ };
52
98
  }
53
- export const unpluginFactory = (options) => {
54
- const groups = options?.groups || {};
55
- const virtualModules = new Map();
56
- return {
57
- name: 'fontmin-plugin',
58
- enforce: 'pre',
59
- async resolveId(source, importer) {
60
- const { moduleContent, absoluteModulePath } = await handleImport(groups, source, importer ? process.cwd() : '');
61
- if (absoluteModulePath) {
62
- virtualModules.set(source, moduleContent || '');
63
- return source;
64
- }
65
- return null;
66
- },
67
- load(id) {
68
- if (virtualModules.has(id)) {
69
- return virtualModules.get(id);
70
- }
71
- return null;
72
- },
73
- loadInclude(id) {
74
- return id.startsWith(importerPrefix);
75
- },
76
- };
99
+ var unpluginFactory = (options) => {
100
+ const groups = options?.groups || {};
101
+ const virtualModules = /* @__PURE__ */ new Map();
102
+ return {
103
+ name: "fontmin-plugin",
104
+ enforce: "pre",
105
+ async resolveId(source, importer) {
106
+ const { moduleContent, absoluteModulePath } = await handleImport(
107
+ groups,
108
+ source,
109
+ importer ? process.cwd() : ""
110
+ );
111
+ if (absoluteModulePath) {
112
+ virtualModules.set(source, moduleContent || "");
113
+ return source;
114
+ }
115
+ return null;
116
+ },
117
+ load(id) {
118
+ if (virtualModules.has(id)) {
119
+ return virtualModules.get(id);
120
+ }
121
+ return null;
122
+ },
123
+ loadInclude(id) {
124
+ return id.startsWith(importerPrefix);
125
+ }
126
+ };
127
+ };
128
+ var unplugin = createUnplugin(unpluginFactory);
129
+ var index_default = unplugin;
130
+ export {
131
+ index_default as default,
132
+ frameworksConfig,
133
+ handleImport,
134
+ unplugin,
135
+ unpluginFactory
77
136
  };
78
- export const unplugin = createUnplugin(unpluginFactory);
79
- export default unplugin;
package/package.json CHANGED
@@ -1,25 +1,30 @@
1
1
  {
2
2
  "name": "@fontmin-plugin/plugin",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
+ "main": "./dist/index.cjs",
5
6
  "module": "./dist/index.js",
6
7
  "types": "./dist/index.d.ts",
7
8
  "files": ["dist"],
8
9
  "exports": {
9
10
  ".": {
11
+ "types": "./dist/index.d.ts",
10
12
  "import": "./dist/index.js",
11
- "require": "./dist/index.js"
13
+ "require": "./dist/index.cjs"
12
14
  }
13
15
  },
14
16
  "scripts": {
15
- "build": "tsc"
17
+ "build": "tsup src/index.tsx --format esm,cjs --dts --clean",
18
+ "publish": "bun run build && bun publish --access public"
16
19
  },
17
20
  "dependencies": {
18
- "@fontmin-plugin/core": "workspace:*",
19
- "unplugin": "^2.3.4"
21
+ "unplugin": "^2.3.4",
22
+ "fontmin": "^1.0.0",
23
+ "radash": "^7.0.0"
20
24
  },
21
25
  "devDependencies": {
22
- "@types/bun": "latest"
26
+ "@types/bun": "latest",
27
+ "tsup": "^8.0.2"
23
28
  },
24
29
  "peerDependencies": {
25
30
  "typescript": "^5.0.0"