@fontmin-plugin/plugin 0.1.0
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.d.ts +20 -0
- package/dist/index.js +79 -0
- package/package.json +27 -0
package/dist/index.d.ts
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
import type { FontConfig } from '@fontmin-plugin/core';
|
2
|
+
import type { UnpluginFactory } from 'unplugin';
|
3
|
+
export interface Options {
|
4
|
+
groups?: Record<string, FontConfig>;
|
5
|
+
}
|
6
|
+
interface FrameworkConfig {
|
7
|
+
prefix: string;
|
8
|
+
generateModule: (fontVarString: string) => string;
|
9
|
+
}
|
10
|
+
export declare const frameworksConfig: Record<'react' | 'vue', FrameworkConfig>;
|
11
|
+
export declare function handleImport(groupConfig: Record<string, FontConfig>, request: string, context: string): Promise<{
|
12
|
+
moduleContent: string;
|
13
|
+
absoluteModulePath: string;
|
14
|
+
} | {
|
15
|
+
moduleContent: undefined;
|
16
|
+
absoluteModulePath: undefined;
|
17
|
+
}>;
|
18
|
+
export declare const unpluginFactory: UnpluginFactory<Options | undefined>;
|
19
|
+
export declare const unplugin: import("unplugin").UnpluginInstance<Options | undefined, boolean>;
|
20
|
+
export default unplugin;
|
package/dist/index.js
ADDED
@@ -0,0 +1,79 @@
|
|
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) => `
|
9
|
+
import { createElement } from 'react';
|
10
|
+
import FontminText from '@fontmin-plugin/react';
|
11
|
+
const fontConfig = ${fontVarString};
|
12
|
+
export default (props) => createElement(FontminText, { ...fontConfig, ...props });
|
13
|
+
`,
|
14
|
+
},
|
15
|
+
vue: {
|
16
|
+
prefix: `${importerPrefix}vue/`,
|
17
|
+
generateModule: (fontVarString) => `
|
18
|
+
import { h } from 'vue';
|
19
|
+
import FontminText from '@fontmin-plugin/vue';
|
20
|
+
const fontConfig = ${fontVarString};
|
21
|
+
export default h(FontminText, { ...fontConfig });
|
22
|
+
`,
|
23
|
+
},
|
24
|
+
};
|
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
|
+
}
|
47
|
+
}
|
48
|
+
return {
|
49
|
+
moduleContent: undefined,
|
50
|
+
absoluteModulePath: undefined,
|
51
|
+
};
|
52
|
+
}
|
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
|
+
};
|
77
|
+
};
|
78
|
+
export const unplugin = createUnplugin(unpluginFactory);
|
79
|
+
export default unplugin;
|
package/package.json
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
{
|
2
|
+
"name": "@fontmin-plugin/plugin",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"type": "module",
|
5
|
+
"module": "./dist/index.js",
|
6
|
+
"types": "./dist/index.d.ts",
|
7
|
+
"files": ["dist"],
|
8
|
+
"exports": {
|
9
|
+
".": {
|
10
|
+
"import": "./dist/index.js",
|
11
|
+
"require": "./dist/index.js"
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"scripts": {
|
15
|
+
"build": "tsc"
|
16
|
+
},
|
17
|
+
"dependencies": {
|
18
|
+
"@fontmin-plugin/core": "workspace:*",
|
19
|
+
"unplugin": "^2.3.4"
|
20
|
+
},
|
21
|
+
"devDependencies": {
|
22
|
+
"@types/bun": "latest"
|
23
|
+
},
|
24
|
+
"peerDependencies": {
|
25
|
+
"typescript": "^5.0.0"
|
26
|
+
}
|
27
|
+
}
|