Skip to content

Commit

Permalink
feat: added virtual id import
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed May 24, 2021
1 parent 0e586b2 commit a86cb57
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default () => {
- Introduce the registration script in src/main.ts

```ts
import 'vite-plugin-svg-icons/register';
import 'virtual:svg-icons-register';
```

Here the svg sprite map has been generated
Expand Down
2 changes: 1 addition & 1 deletion README.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default () => {
- 在 src/main.ts 内引入注册脚本

```ts
import 'vite-plugin-svg-icons/register';
import 'virtual:svg-icons-register';
```

到这里 svg 雪碧图已经生成
Expand Down
1 change: 0 additions & 1 deletion client.ts

This file was deleted.

2 changes: 1 addition & 1 deletion example/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApp } from 'vue';
import App from './App.vue';

import 'vite-plugin-svg-icons/register';
import 'virtual:svg-icons-register';

import allKeys from 'virtual:svg-icons-names';

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"author": "Vben",
"files": [
"dist",
"register.ts",
"client.ts"
"register.d.ts",
"client.d.ts"
],
"scripts": {
"dev": "npm run build -- --watch",
Expand Down
1 change: 0 additions & 1 deletion register.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const SVG_ICONS_NAME = 'vite-plugin-svg-icons/register';
export const SVG_ICONS_NAME = ['vite-plugin-svg-icons/register', 'virtual:svg-icons-register'];
export const SVG_ICONS_CLIENT = ['vite-plugin-svg-icons/client', 'virtual:svg-icons-names'];

export const SVG_DOM_ID = '__svg__icons__dom__' + new Date().getTime() + '__';
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ export default (opt: ViteSvgIconsPlugin): Plugin => {
debug('resolvedConfig:', resolvedConfig);
},
resolveId(importee) {
if (importee === SVG_ICONS_NAME || SVG_ICONS_CLIENT.includes(importee)) {
if (SVG_ICONS_NAME.includes(importee) || SVG_ICONS_CLIENT.includes(importee)) {
return importee;
}
return null;
},

async load(id) {
if (!isBuild) return null;
const isRegister = id.endsWith(SVG_ICONS_NAME);
const isRegister = SVG_ICONS_NAME.some((item) => id.endsWith(item));
const isClient = SVG_ICONS_CLIENT.some((item) => id.endsWith(item));
const { code, idSet } = await createModuleCode(
cache,
Expand All @@ -96,17 +96,17 @@ export default (opt: ViteSvgIconsPlugin): Plugin => {
middlewares.use(cors({ origin: '*' }));
middlewares.use(async (req, res, next) => {
const url = normalizePath(req.url!);
const registerId = `/@id/${SVG_ICONS_NAME}`;
const registerIds = SVG_ICONS_NAME.map((item) => `/@id/${item}`);
const clientIds = SVG_ICONS_CLIENT.map((item) => `/@id/${item}`);
if ([...clientIds, registerId].some((item) => url.endsWith(item))) {
if ([...clientIds, ...registerIds].some((item) => url.endsWith(item))) {
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('Cache-Control', 'no-cache');
const { code, idSet } = await createModuleCode(
cache,
svgoOptions as OptimizeOptions,
options
);
const content = url.endsWith(registerId) ? code : idSet;
const content = registerIds.some((item) => url.endsWith(item)) ? code : idSet;

res.setHeader('Etag', getEtag(content, { weak: true }));
res.statusCode = 200;
Expand Down

0 comments on commit a86cb57

Please sign in to comment.