Skip to content

Commit

Permalink
test(vite): rspack 插件测试
Browse files Browse the repository at this point in the history
  • Loading branch information
KonghaYao committed Jun 5, 2024
1 parent 6cf32c3 commit ac9fb68
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
25 changes: 25 additions & 0 deletions packages/vite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,31 @@ export const App = () => {
};
```

#### Optimization for individual partitions

Sometimes, we need to package fonts based on different page dimensions, so we can use keys to identify the scope of using scanFiles.

```js
// This will match subset-1
import { css } from '../../demo/public/SmileySans-Oblique.ttf?subsets&key=subset-1';
```

```js
import { defineConfig } from 'vite';
import viteFont from 'vite-plugin-font';
export default defineConfig({
plugins: [
viteFont({
scanFiles: {
// ?subsets will match default
'default': ['src/**/*.{json,js,jsx,ts,tsx,vue}'],
'subset-1': ['example/**/*.{json,js,jsx,ts,tsx,vue}']
},
}),
],
});
```

## Typescript support

The source code includes the `src/font.d.ts` file, which you can add to your `tsconfig.json`.
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export const App = () => {

#### 单独分区优化

有些时候,我们需要根据不同的页面维度来进行字体分包,所以可以使用 key 来标识使用 scanFiles 的范围。

```js
// 这个将会匹配到 subset-1
import { css } from '../../demo/public/SmileySans-Oblique.ttf?subsets&key=subset-1';
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.10.3",
"version": "1.0.0",
"name": "vite-plugin-font",
"description": "An automatic Web Font optimization plugin that supports many platforms such as Vite, Next, Nuxt, and more.",
"main": "./dist/vite.js",
Expand Down
12 changes: 10 additions & 2 deletions packages/vite/rspack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
export default {
entry: [

module.exports = {
'./example/index.ts',
'./example/subset1.ts',
'./example/total.ts'
],
output: {
path: './build/rspack',
},
module: {
rules: [
{
test: /\.(otf|ttf)/i,
use: [
{
loader: './src/webpack.mts',
loader: './dist/webpack.mjs',
options: {
scanFiles: {
'default': ['src/**/*.{json,js,jsx,ts,tsx,vue}'],
Expand Down
16 changes: 15 additions & 1 deletion packages/vite/test/build.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ await new Promise((res, rej) => {

assert.ok(fs.existsSync('./node_modules/.vite/.subsets/2f32afea01e31e457a7be1dabf93e228/SmileySans-Oblique_ttf/reporter.json'))
assert.ok(fs.existsSync('./node_modules/.vite/.subsets/1851a5900af7816b43bd1b9aefd9da9f/SmileySans-Oblique_ttf/reporter.json'))
assert.ok(fs.existsSync('./node_modules/.vite/SmileySans-Oblique_ttf/reporter.json'))
assert.ok(fs.existsSync('./node_modules/.vite/SmileySans-Oblique_ttf/reporter.json'))

await new Promise((res, rej) => {
exec('pnpm rspack build', { cwd: process.cwd() }, (err, stdout, stderr) => {
if (err) {
rej(err);
return;
}
res(`stdout: ${stdout}`);
});
})

assert.ok(fs.existsSync('./node_modules/.cache/.font/.subsets/2f32afea01e31e457a7be1dabf93e228/SmileySans-Oblique_ttf/reporter.json'))
assert.ok(fs.existsSync('./node_modules/.cache/.font/.subsets/1851a5900af7816b43bd1b9aefd9da9f/SmileySans-Oblique_ttf/reporter.json'))
assert.ok(fs.existsSync('./node_modules/.cache/.font/SmileySans-Oblique_ttf/reporter.json'))

0 comments on commit ac9fb68

Please sign in to comment.