UNPKG

@digitalacorn/vite-plugin-svg-icons

Version:
63 lines 8.83 kB
{ "name": "@digitalacorn/vite-plugin-svg-icons", "version": "3.0.0-pre.2", "description": "Vite Plugin for fast creating SVG sprites.", "main": "dist/index.cjs", "module": "dist/index.mjs", "types": "dist/index.d.ts", "exports": { ".": { "require": "./dist/index.cjs", "import": "./dist/index.mjs", "types": "./dist/index.d.ts" } }, "license": "MIT", "author": "Vben", "files": [ "dist", "client.d.ts" ], "keywords": [ "vite", "vite-plugin", "svg", "sprite", "svgo", "vben" ], "repository": { "type": "git", "url": "https://github.com/digitalacorn/vite-plugin-svg-icons", "directory": "packages/core" }, "bugs": { "url": "https://github.com/digitalacorn/vite-plugin-svg-i cons/issues" }, "homepage": "https://github.com/digitalacorn/vite-plugin-svg-icons/tree/master/#readme", "dependencies": { "@types/svgo": "^2.6.1", "cors": "^2.8.5", "debug": "^4.3.3", "etag": "^1.8.1", "fs-extra": "^10.0.0", "pathe": "^0.2.0", "svg-baker": "1.7.0", "svgo": "^2.8.0" }, "peerDependencies": { "vite": ">=2.9.16" }, "devDependencies": { "@types/cors": "^2.8.12", "@types/debug": "^4.1.7", "@types/etag": "^1.8.1", "@types/fs-extra": "^9.0.13", "@types/node": "^17.0.13" }, "scripts": { "dev": "pnpm unbuild --stub", "build": "pnpm unbuild" }, "readme": "# @digitalacorn/vite-plugin-svg-icons\n\nUsed to generate svg sprite map.\n\n> <br/>Please Note: this is fork of [vite-plugin-svg-icons](https://www.npmjs.com/package/vite-plugin-svg-icons) <br/><br/> Initially created so I can proceed using the feature in this [pull request](https://github.com/vbenjs/vite-plugin-svg-icons/pull/68).\n> <br/><br/>There has been no sign of activity from the maintainer of the above.\n> <br/><br/>\n\n## Summary of additions:\n\n- Adds the ability to disable the replacement of all stroke colours with currentColour.\n- Version 3+ Also this package now includes significant increments to modues used, removing vulnerabilities, and without more test coverage could introduce breaking changes from the 2.X version.\n\n## Feature\n\n- **Preloading** All icons are generated when the project is running, and you only need to operate dom once.\n- **High performance** Built-in cache, it will be regenerated only when the file is modified.\n\n## Installation (yarn or npm)\n\n**node version:** >=12.0.0\n\n**vite version:** >=2.0.0\n\n```bash\nyarn add vite-plugin-svg-icons -D\n# or\nnpm i vite-plugin-svg-icons -D\n# or\npnpm install vite-plugin-svg-icons -D\n```\n\n## Usage\n\n- Configuration plugin in vite.config.ts\n\n```ts\nimport { createSvgIconsPlugin } from 'vite-plugin-svg-icons'\nimport path from 'path'\n\nexport default () => {\n return {\n plugins: [\n createSvgIconsPlugin({\n // Specify the icon folder to be cached\n iconDirs: [path.resolve(process.cwd(), 'src/icons')],\n // Specify symbolId format\n symbolId: 'icon-[dir]-[name]',\n\n /**\n * custom insert position\n * @default: body-last\n */\n inject?: 'body-last' | 'body-first'\n\n /**\n * custom dom id\n * @default: __svg__icons__dom__\n */\n customDomId: '__svg__icons__dom__',\n\n /**\n * option to perform a replacement of stroke colors with currentColor\n * @default:true\n */\n replaceStrokeWithCurrentColor: true\n }),\n ],\n }\n}\n\n```\n\n- Introduce the registration script in src/main.ts\n\n```ts\nimport 'virtual:svg-icons-register'\n```\n\nHere the svg sprite map has been generated\n\n## How to use in components\n\n### **Vue way**\n\n`/src/components/SvgIcon.vue`\n\n```vue\n<template>\n <svg aria-hidden=\"true\">\n <use :href=\"symbolId\" :fill=\"color\" />\n </svg>\n</template>\n\n<script>\nimport { defineComponent, computed } from 'vue'\n\nexport default defineComponent({\n name: 'SvgIcon',\n props: {\n prefix: {\n type: String,\n default: 'icon',\n },\n name: {\n type: String,\n required: true,\n },\n color: {\n type: String,\n default: '#333',\n },\n },\n setup(props) {\n const symbolId = computed(() => `#${props.prefix}-${props.name}`)\n return { symbolId }\n },\n})\n</script>\n```\n\n#### **Icons Directory Structure**\n\n```bash\n# src/icons\n\n- icon1.svg\n- icon2.svg\n- icon3.svg\n- dir/icon1.svg\n```\n\n`/src/App.vue`\n\n```vue\n<template>\n <div>\n <SvgIcon name=\"icon1\"></SvgIcon>\n <SvgIcon name=\"icon2\"></SvgIcon>\n <SvgIcon name=\"icon3\"></SvgIcon>\n <SvgIcon name=\"dir-icon1\"></SvgIcon>\n </div>\n</template>\n\n<script>\nimport { defineComponent, computed } from 'vue'\n\nimport SvgIcon from './components/SvgIcon.vue'\nexport default defineComponent({\n name: 'App',\n components: { SvgIcon },\n})\n</script>\n```\n\n### **React way**\n\n`/src/components/SvgIcon.jsx`\n\n```jsx\nexport default function SvgIcon({\n name,\n prefix = 'icon',\n color = '#333',\n ...props\n}) {\n const symbolId = `#${prefix}-${name}`\n\n return (\n <svg {...props} aria-hidden=\"true\">\n <use href={symbolId} fill={color} />\n </svg>\n )\n}\n```\n\n#### **Icons Directory Structure**\n\n```bash\n# src/icons\n\n- icon1.svg\n- icon2.svg\n- icon3.svg\n- dir/icon1.svg\n```\n\n`/src/App.jsx`\n\n```jsx\nimport SvgIcon from './components/SvgIcon'\n\nexport default function App() {\n return (\n <>\n <SvgIcon name=\"icon1\"></SvgIcon>\n <SvgIcon name=\"icon1\"></SvgIcon>\n <SvgIcon name=\"icon1\"></SvgIcon>\n <SvgIcon name=\"dir-icon1\"></SvgIcon>\n </>\n )\n}\n```\n\n### Get all SymbolId\n\n```ts\nimport ids from 'virtual:svg-icons-names'\n// => ['icon-icon1','icon-icon2','icon-icon3']\n```\n\n### Options\n\n| Parameter | Type | Default | Description |\n| ----------------------------- | ---------------------- | --------------------- | ------------------------------------------------------------------------------------- |\n| iconDirs | `string[]` | - | Need to generate the icon folder of the Sprite image |\n| symbolId | `string` | `icon-[dir]-[name]` | svg symbolId format, see the description below |\n| svgoOptions | `boolean|SvgoOptions` | `true` | svg compression configuration, can be an object[Options](https://github.com/svg/svgo) |\n| inject | `string` | `body-last` | svgDom default insertion position, optional `body-first` |\n| customDomId | `string` | `__svg__icons__dom__` | Customize the ID of the svgDom insert node |\n| replaceStrokeWithCurrentColor | `boolean` | `true` | Whether to perform a replacement of stroke colors with currentColor |\n\n**symbolId**\n\n`icon-[dir]-[name]`\n\n**[name]:**\n\nsvg file name\n\n**[dir]**\n\nThe svg of the plug-in will not generate hash to distinguish, but distinguish it by folder.\n\nIf the folder corresponding to `iconDirs` contains this other folder\n\nexample:\n\nThen the generated SymbolId is written in the comment\n\n```bash\n# src/icons\n- icon1.svg # icon-icon1\n- icon2.svg # icon-icon2\n- icon3.svg # icon-icon3\n- dir/icon1.svg # icon-dir-icon1\n- dir/dir2/icon1.svg # icon-dir-dir2-icon1\n```\n\n## Typescript Support\n\nIf using `Typescript`, you can add in `tsconfig.json`\n\n```json\n// tsconfig.json\n{\n \"compilerOptions\": {\n \"types\": [\"vite-plugin-svg-icons/client\"]\n }\n}\n```\n\n**Note**\n\nAlthough the use of folders to distinguish between them can largely avoid the problem of duplicate names, there will also be svgs with multiple folders and the same file name in `iconDirs`.\n\nThis needs to be avoided by the developer himself\n\n## Example\n\n**Run**\n\n```bash\n\npnpm install\ncd ./packages/playground/basic\npnpm run dev\npnpm run build\n\n```\n\n## Sample project\n\n[Vben Admin](https://github.com/anncwb/vue-vben-admin)\n\n## License\n\n[MIT © Vben-2020](./LICENSE)\n\n> This package may be deprecated if the PR is accepted and other updates are implemented by the current maintainer of the package from which it is forked. Unless you need thw specific updates, security improvements or features it is recommended that you use the original package [vite-plugin-svg-icons](https://www.npmjs.com/package/vite-plugin-svg-icons).\n" }