-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
145 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
|
||
declare module '@konghayao/_font_' { | ||
import { fontSplit } from '@konghayao/cn-font-split'; | ||
type Reporter = Awaited<ReturnType<typeof fontSplit>>; | ||
export const css: Reporter['css']; | ||
} | ||
|
||
declare module '*.ttf' { | ||
export * from '@konghayao/_font_' | ||
export * from '@konghayao/_font_'; | ||
} | ||
|
||
declare module '*.otf' { | ||
export * from '@konghayao/_font_' | ||
} | ||
export * from '@konghayao/_font_'; | ||
} | ||
declare module '*.ttf?subsets' { | ||
export * from '@konghayao/_font_'; | ||
} | ||
|
||
declare module '*.otf?subsets' { | ||
export * from '@konghayao/_font_'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { BundlePlugin, BundlePluginConfig } from '../index.js'; | ||
import { glob } from 'glob'; | ||
import fs from 'fs'; | ||
import crypto from 'node:crypto'; | ||
export interface SubsetBundlePluginConfig extends BundlePluginConfig { | ||
scanFiles: string | string[]; | ||
ignore?: string | string[]; | ||
whiteList?: string | string[]; | ||
} | ||
export class SubsetBundlePlugin extends BundlePlugin { | ||
subsetConfig: SubsetBundlePluginConfig; | ||
constructor(config: SubsetBundlePluginConfig) { | ||
super(config); | ||
this.subsetConfig = config; | ||
} | ||
|
||
usedSubsets = new Set<number>(); | ||
async createSubsets() { | ||
this.getWhiteListSubsets(); | ||
await this.getScanFiles(); | ||
const subsetsArr = [...this.usedSubsets].sort((a, b) => a - b); | ||
this.subsets = [subsetsArr]; | ||
return crypto | ||
.createHash('md5') | ||
.update(String.fromCharCode(...subsetsArr)) | ||
.digest('hex'); | ||
} | ||
async getScanFiles() { | ||
const files = await glob(this.subsetConfig.scanFiles, { | ||
absolute: true, | ||
nodir: true, | ||
ignore: this.subsetConfig.ignore ?? ['node_modules/**'], | ||
}); | ||
return Promise.all( | ||
files.map((i) => { | ||
return new Promise((res, rej) => { | ||
const stream = fs.createReadStream(i, { | ||
encoding: 'utf8', | ||
}); | ||
stream.on('data', (i: string) => { | ||
i.split('').forEach((char) => | ||
this.usedSubsets.add(char.charCodeAt(0)), | ||
); | ||
}); | ||
stream.on('end', () => { | ||
res(null); | ||
}); | ||
stream.on('error', (err) => { | ||
rej(err); | ||
}); | ||
}); | ||
}), | ||
); | ||
} | ||
getWhiteListSubsets() { | ||
let whiteList = this.subsetConfig.whiteList ?? []; | ||
if (!(whiteList instanceof Array)) { | ||
whiteList = [whiteList]; | ||
} | ||
const set = this.usedSubsets; | ||
whiteList.forEach((str) => | ||
str.split('').forEach((char) => set.add(char.charCodeAt(0))), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Plugin } from 'vite'; | ||
import path from 'path'; | ||
|
||
import { | ||
SubsetBundlePlugin, | ||
SubsetBundlePluginConfig, | ||
} from './SubsetBundlePlugin.js'; | ||
export default function split(config: SubsetBundlePluginConfig): Plugin { | ||
const plugin = new SubsetBundlePlugin(config); | ||
return <Plugin>{ | ||
name: 'vite-plugin-font-subsets', | ||
enforce: 'pre', | ||
|
||
async configResolved(c) { | ||
const hash = await plugin.createSubsets(); | ||
plugin.config.cacheDir = path.resolve( | ||
config.cacheDir ?? path.resolve(c.cacheDir, './.font/'), | ||
hash, | ||
); | ||
console.log( | ||
'vite-plugin-font | cache dir | ' + plugin.config.cacheDir, | ||
); | ||
}, | ||
async load(id, options) { | ||
if (['.otf?subsets', '.ttf?subsets'].some((i) => id.endsWith(i))) { | ||
id = id.split('?')[0]; | ||
await plugin.createBundle(id); | ||
return plugin.createSourceCode(id); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.