@leancodepl/vite-plugin-favicon 9.5.2
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/README.md +84 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +82 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -0
- package/index.esm.js +80 -0
- package/package.json +51 -0
- package/src/index.d.ts +1 -0
- package/src/lib/index.d.ts +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# @leancodepl/vite-plugin-favicon
|
|
2
|
+
|
|
3
|
+
A Typescript library for generating favicon formats and sizes from a source logo and injecting the HTML tags into the
|
|
4
|
+
build output. Extracted from [vite favicon plugin](https://github.com/josh-hemphill/vite-plugin-favicon) and adjusted to
|
|
5
|
+
be dev mode compatible.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @leancodepl/vite-plugin-favicon
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
### `ViteFaviconsPlugin(options)`
|
|
16
|
+
|
|
17
|
+
Creates a Vite plugin for generating favicons from a source logo.
|
|
18
|
+
|
|
19
|
+
**Parameters:**
|
|
20
|
+
|
|
21
|
+
- `options` - Configuration options for favicon generation
|
|
22
|
+
|
|
23
|
+
**Returns:** Vite plugin instance
|
|
24
|
+
|
|
25
|
+
### `ViteFaviconsPluginOptions`
|
|
26
|
+
|
|
27
|
+
Configuration options for the Vite favicon plugin.
|
|
28
|
+
|
|
29
|
+
**Properties:**
|
|
30
|
+
|
|
31
|
+
- `logo?: string` - Source logo path for favicon generation (default: "assets/logo.png")
|
|
32
|
+
- `inject?: boolean` - Whether to inject HTML links and metadata (default: true)
|
|
33
|
+
- `favicons?: Partial<FaviconOptions>`
|
|
34
|
+
- `outputPath?: string`
|
|
35
|
+
|
|
36
|
+
## Usage Examples
|
|
37
|
+
|
|
38
|
+
### Basic Configuration
|
|
39
|
+
|
|
40
|
+
```javascript
|
|
41
|
+
// vite.config.js
|
|
42
|
+
import { ViteFaviconsPlugin } from "@leancodepl/vite-plugin-favicon"
|
|
43
|
+
|
|
44
|
+
export default {
|
|
45
|
+
plugins: [ViteFaviconsPlugin()],
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Custom Logo Path
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
// vite.config.js
|
|
53
|
+
import { ViteFaviconsPlugin } from "@leancodepl/vite-plugin-favicon"
|
|
54
|
+
|
|
55
|
+
export default {
|
|
56
|
+
plugins: [
|
|
57
|
+
ViteFaviconsPlugin({
|
|
58
|
+
logo: "src/assets/logo.png",
|
|
59
|
+
}),
|
|
60
|
+
],
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Advanced Favicon Configuration
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
// vite.config.js
|
|
68
|
+
import { ViteFaviconsPlugin } from "@leancodepl/vite-plugin-favicon"
|
|
69
|
+
|
|
70
|
+
export default {
|
|
71
|
+
plugins: [
|
|
72
|
+
ViteFaviconsPlugin({
|
|
73
|
+
logo: "src/assets/logo.png",
|
|
74
|
+
favicons: {
|
|
75
|
+
appName: "My Application",
|
|
76
|
+
appShortName: "MyApp",
|
|
77
|
+
themeColor: "#ffffff",
|
|
78
|
+
background: "#000000",
|
|
79
|
+
display: "standalone",
|
|
80
|
+
},
|
|
81
|
+
}),
|
|
82
|
+
],
|
|
83
|
+
}
|
|
84
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var favicons = require('favicons');
|
|
4
|
+
var parse5 = require('parse5');
|
|
5
|
+
var path = require('path');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a Vite plugin for generating favicons from a source logo.
|
|
9
|
+
*
|
|
10
|
+
* Generates various favicon formats and sizes, then injects the appropriate
|
|
11
|
+
* HTML tags into the build output. Supports watching for logo changes during development.
|
|
12
|
+
*
|
|
13
|
+
* @param options - Configuration options for favicon generation
|
|
14
|
+
* @returns Vite plugin instance
|
|
15
|
+
* @example
|
|
16
|
+
* ```javascript
|
|
17
|
+
* // vite.config.js
|
|
18
|
+
* import { ViteFaviconsPlugin } from '@leancodepl/vite-plugin-favicon'
|
|
19
|
+
*
|
|
20
|
+
* export default {
|
|
21
|
+
* plugins: [
|
|
22
|
+
* ViteFaviconsPlugin({
|
|
23
|
+
* logo: 'src/assets/logo.png',
|
|
24
|
+
* favicons: {
|
|
25
|
+
* appName: 'My App',
|
|
26
|
+
* appShortName: 'App',
|
|
27
|
+
* themeColor: '#ffffff'
|
|
28
|
+
* }
|
|
29
|
+
* })
|
|
30
|
+
* ]
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function ViteFaviconsPlugin(options = {}) {
|
|
35
|
+
options.inject ??= true;
|
|
36
|
+
const logoPath = path.resolve(options.logo ?? path.join("assets", "logo.png"));
|
|
37
|
+
let faviconResponse = undefined;
|
|
38
|
+
const rebuildFavicons = async ctx => {
|
|
39
|
+
ctx.addWatchFile(logoPath);
|
|
40
|
+
faviconResponse = await favicons(logoPath, options.favicons);
|
|
41
|
+
for (const {
|
|
42
|
+
name,
|
|
43
|
+
contents
|
|
44
|
+
} of [...faviconResponse.files, ...faviconResponse.images]) {
|
|
45
|
+
ctx.emitFile({
|
|
46
|
+
type: "asset",
|
|
47
|
+
name,
|
|
48
|
+
source: contents
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
name: "vite-plugin-favicon",
|
|
54
|
+
apply: "build",
|
|
55
|
+
async buildStart() {
|
|
56
|
+
await rebuildFavicons(this);
|
|
57
|
+
},
|
|
58
|
+
transformIndexHtml(_, ctx) {
|
|
59
|
+
if (!options.inject || !faviconResponse) return;
|
|
60
|
+
const tags = [];
|
|
61
|
+
const assets = Object.values(ctx.bundle ?? {});
|
|
62
|
+
for (const tag of faviconResponse.html) {
|
|
63
|
+
const node = parse5.parseFragment(tag).childNodes[0];
|
|
64
|
+
if ("attrs" in node) {
|
|
65
|
+
tags.push({
|
|
66
|
+
tag: node.tagName,
|
|
67
|
+
attrs: node.attrs.reduce((acc, v) => {
|
|
68
|
+
const name = assets.find(({
|
|
69
|
+
name
|
|
70
|
+
}) => name === v.value.slice(1))?.fileName;
|
|
71
|
+
acc[v.name] = name ? `/${name}` : v.value;
|
|
72
|
+
return acc;
|
|
73
|
+
}, {})
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return tags;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
exports.ViteFaviconsPlugin = ViteFaviconsPlugin;
|
package/index.cjs.mjs
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import favicons from 'favicons';
|
|
2
|
+
import { parseFragment } from 'parse5';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Vite plugin for generating favicons from a source logo.
|
|
7
|
+
*
|
|
8
|
+
* Generates various favicon formats and sizes, then injects the appropriate
|
|
9
|
+
* HTML tags into the build output. Supports watching for logo changes during development.
|
|
10
|
+
*
|
|
11
|
+
* @param options - Configuration options for favicon generation
|
|
12
|
+
* @returns Vite plugin instance
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* // vite.config.js
|
|
16
|
+
* import { ViteFaviconsPlugin } from '@leancodepl/vite-plugin-favicon'
|
|
17
|
+
*
|
|
18
|
+
* export default {
|
|
19
|
+
* plugins: [
|
|
20
|
+
* ViteFaviconsPlugin({
|
|
21
|
+
* logo: 'src/assets/logo.png',
|
|
22
|
+
* favicons: {
|
|
23
|
+
* appName: 'My App',
|
|
24
|
+
* appShortName: 'App',
|
|
25
|
+
* themeColor: '#ffffff'
|
|
26
|
+
* }
|
|
27
|
+
* })
|
|
28
|
+
* ]
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function ViteFaviconsPlugin(options = {}) {
|
|
33
|
+
options.inject ??= true;
|
|
34
|
+
const logoPath = path.resolve(options.logo ?? path.join("assets", "logo.png"));
|
|
35
|
+
let faviconResponse = undefined;
|
|
36
|
+
const rebuildFavicons = async ctx => {
|
|
37
|
+
ctx.addWatchFile(logoPath);
|
|
38
|
+
faviconResponse = await favicons(logoPath, options.favicons);
|
|
39
|
+
for (const {
|
|
40
|
+
name,
|
|
41
|
+
contents
|
|
42
|
+
} of [...faviconResponse.files, ...faviconResponse.images]) {
|
|
43
|
+
ctx.emitFile({
|
|
44
|
+
type: "asset",
|
|
45
|
+
name,
|
|
46
|
+
source: contents
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
name: "vite-plugin-favicon",
|
|
52
|
+
apply: "build",
|
|
53
|
+
async buildStart() {
|
|
54
|
+
await rebuildFavicons(this);
|
|
55
|
+
},
|
|
56
|
+
transformIndexHtml(_, ctx) {
|
|
57
|
+
if (!options.inject || !faviconResponse) return;
|
|
58
|
+
const tags = [];
|
|
59
|
+
const assets = Object.values(ctx.bundle ?? {});
|
|
60
|
+
for (const tag of faviconResponse.html) {
|
|
61
|
+
const node = parseFragment(tag).childNodes[0];
|
|
62
|
+
if ("attrs" in node) {
|
|
63
|
+
tags.push({
|
|
64
|
+
tag: node.tagName,
|
|
65
|
+
attrs: node.attrs.reduce((acc, v) => {
|
|
66
|
+
const name = assets.find(({
|
|
67
|
+
name
|
|
68
|
+
}) => name === v.value.slice(1))?.fileName;
|
|
69
|
+
acc[v.name] = name ? `/${name}` : v.value;
|
|
70
|
+
return acc;
|
|
71
|
+
}, {})
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return tags;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { ViteFaviconsPlugin };
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@leancodepl/vite-plugin-favicon",
|
|
3
|
+
"version": "9.5.2",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public",
|
|
7
|
+
"registry": "https://registry.npmjs.org/"
|
|
8
|
+
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18.0.0"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/leancodepl/js_corelibrary.git",
|
|
15
|
+
"directory": "packages/vite-plugin-favicon"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/leancodepl/js_corelibrary",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/leancodepl/js_corelibrary/issues"
|
|
20
|
+
},
|
|
21
|
+
"description": "Vite plugin for generating favicons",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"vite",
|
|
24
|
+
"favicon",
|
|
25
|
+
"leancode"
|
|
26
|
+
],
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "LeanCode",
|
|
29
|
+
"url": "https://leancode.co"
|
|
30
|
+
},
|
|
31
|
+
"sideEffects": false,
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"vite": ">=4.0.0"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"favicons": "^7.2.0",
|
|
37
|
+
"parse5": "^8.0.0"
|
|
38
|
+
},
|
|
39
|
+
"exports": {
|
|
40
|
+
"./package.json": "./package.json",
|
|
41
|
+
".": {
|
|
42
|
+
"module": "./index.esm.js",
|
|
43
|
+
"types": "./index.d.ts",
|
|
44
|
+
"import": "./index.cjs.mjs",
|
|
45
|
+
"default": "./index.cjs.js"
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"module": "./index.esm.js",
|
|
49
|
+
"main": "./index.cjs.js",
|
|
50
|
+
"types": "./index.d.ts"
|
|
51
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./lib";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { FaviconOptions } from "favicons";
|
|
2
|
+
import type { Plugin } from "vite";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the Vite favicon plugin.
|
|
5
|
+
*/
|
|
6
|
+
export type ViteFaviconsPluginOptions = {
|
|
7
|
+
/** Source logo path for favicon generation.
|
|
8
|
+
* @default "assets/logo.png"
|
|
9
|
+
*/
|
|
10
|
+
logo?: string;
|
|
11
|
+
/** Whether to inject HTML links and metadata.
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
inject?: boolean;
|
|
15
|
+
/** Favicons configuration options.
|
|
16
|
+
* See [favicons documentation](https://github.com/itgalaxy/favicons) for details.
|
|
17
|
+
*/
|
|
18
|
+
favicons?: Partial<FaviconOptions>;
|
|
19
|
+
/** Project root directory for metadata loading.
|
|
20
|
+
* @default process.cwd()
|
|
21
|
+
*/
|
|
22
|
+
outputPath?: string;
|
|
23
|
+
};
|
|
24
|
+
type FaviconsPluginArgs = Partial<ViteFaviconsPluginOptions>;
|
|
25
|
+
/**
|
|
26
|
+
* Creates a Vite plugin for generating favicons from a source logo.
|
|
27
|
+
*
|
|
28
|
+
* Generates various favicon formats and sizes, then injects the appropriate
|
|
29
|
+
* HTML tags into the build output. Supports watching for logo changes during development.
|
|
30
|
+
*
|
|
31
|
+
* @param options - Configuration options for favicon generation
|
|
32
|
+
* @returns Vite plugin instance
|
|
33
|
+
* @example
|
|
34
|
+
* ```javascript
|
|
35
|
+
* // vite.config.js
|
|
36
|
+
* import { ViteFaviconsPlugin } from '@leancodepl/vite-plugin-favicon'
|
|
37
|
+
*
|
|
38
|
+
* export default {
|
|
39
|
+
* plugins: [
|
|
40
|
+
* ViteFaviconsPlugin({
|
|
41
|
+
* logo: 'src/assets/logo.png',
|
|
42
|
+
* favicons: {
|
|
43
|
+
* appName: 'My App',
|
|
44
|
+
* appShortName: 'App',
|
|
45
|
+
* themeColor: '#ffffff'
|
|
46
|
+
* }
|
|
47
|
+
* })
|
|
48
|
+
* ]
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function ViteFaviconsPlugin(options?: FaviconsPluginArgs): Plugin;
|
|
53
|
+
export {};
|