vite-plugin-lucide-preprocess 1.0.0 → 1.0.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 +28 -6
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -40
- package/dist/index.js.map +1 -1
- package/dist/plugin.d.ts +16 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +58 -0
- package/dist/plugin.js.map +1 -0
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -9,21 +9,43 @@ A [Vite](https://vitejs.dev) plugin to replace imports for [Lucide icons](https:
|
|
|
9
9
|
|
|
10
10
|
## What is this?
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
### Motivation
|
|
13
|
+
|
|
14
|
+
If you want to import Lucide icons in your project, you can use two methods
|
|
15
|
+
since [this PR on Lucide](https://github.com/lucide-icons/lucide/pull/1707) (at least for `lucide-svelte`):
|
|
14
16
|
|
|
15
17
|
```js
|
|
16
18
|
import { IconName } from "lucide-svelte";
|
|
17
|
-
// or
|
|
19
|
+
// or
|
|
18
20
|
import IconName from "lucide-svelte/icons/icon-name";
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
While the first method is more convenient (and may be the one used by auto-import features in your editor),
|
|
23
|
+
While the first method is more convenient (and may be the one used by auto-import features in your editor),
|
|
24
|
+
it is not tree-shakable.
|
|
25
|
+
In fact, a single import from `"lucide-<framework>"` will import all icons, not ideal for performance and bundle size.
|
|
22
26
|
|
|
23
|
-
This plugin will replace the first method with the second one, so you can keep the convenience of the first method
|
|
27
|
+
**This plugin will replace the first method with the second one, so you can keep the convenience of the first method
|
|
28
|
+
while benefiting from tree-shaking optimizations.** “Correct” imports will be kept as-is.
|
|
24
29
|
|
|
25
30
|
> [!NOTE]
|
|
26
|
-
>
|
|
31
|
+
> The plugin is primarily intended for `lucide-svelte`, but it should work with
|
|
32
|
+
> any Lucide package using Vite if they follow the same structure.
|
|
33
|
+
|
|
34
|
+
### Credits and opinion
|
|
35
|
+
|
|
36
|
+
I got the idea
|
|
37
|
+
from [Phosphor Icons' optimizer](https://github.com/haruaki07/phosphor-svelte#import-optimizer-experimental).
|
|
38
|
+
However, this plugin is written from scratch and is not based on the Phosphor one (even though the technique is
|
|
39
|
+
essentially the same).
|
|
40
|
+
|
|
41
|
+
As it is a Vite plugin and not a Svelte one, it is more generic and can be used with any framework.
|
|
42
|
+
Additionally, it doesn't suffer from the limitations of the Svelte plugin, preventing the Phosphor
|
|
43
|
+
optimizer from working without disabling the native Vite optimizations of `phosphor-svelte`.
|
|
44
|
+
|
|
45
|
+
In my opinion, this should be a feature embedded in Lucide itself (`lucide-svelte/preprocessor`),
|
|
46
|
+
but I don't know if it will ever be implemented.
|
|
47
|
+
[I opened an issue](https://github.com/lucide-icons/lucide/issues/2295) on the `lucide-icons` repository to discuss
|
|
48
|
+
this.
|
|
27
49
|
|
|
28
50
|
## Installation
|
|
29
51
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @return {import("vite").Plugin}
|
|
4
|
-
*/
|
|
5
|
-
export default function _default(): import("vite").Plugin;
|
|
1
|
+
export default plugin;
|
|
2
|
+
import { plugin } from "./plugin.js";
|
|
6
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":";uBAAuB,aAAa"}
|
package/dist/index.js
CHANGED
|
@@ -1,41 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* The Vite plugin that preprocesses imports to optimize Lucide icons
|
|
4
|
-
* @return {import("vite").Plugin}
|
|
5
|
-
*/
|
|
6
|
-
export default function () {
|
|
7
|
-
return {
|
|
8
|
-
name: "vite-plugin-lucide-preprocess",
|
|
9
|
-
enforce: "pre",
|
|
10
|
-
transform: (code, path) => {
|
|
11
|
-
if (ignoredPaths.some(p => path.includes(p))) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
code: code.replace(/^(\s*)import\s+\{([^}]*)}\s+from\s+(["'])lucide-(.*?)["'](.*)$/gm,
|
|
16
|
-
/**
|
|
17
|
-
* @param _ The whole matched string
|
|
18
|
-
* @param initialSpacing {string} The spacing before the matched import statement
|
|
19
|
-
* @param modulesStr {string} The imported icons as a raw string
|
|
20
|
-
* @param quote {string} The quote used in the import statement (either ' or ")
|
|
21
|
-
* @param framework {string} The framework the icons are imported for
|
|
22
|
-
* @param lineEnding {string} The end of the line of the matched import statement (often a semicolon)
|
|
23
|
-
* @return The optimized import statement(s)
|
|
24
|
-
*/
|
|
25
|
-
(_, initialSpacing, modulesStr, quote, framework, lineEnding) => {
|
|
26
|
-
const modules = modulesStr
|
|
27
|
-
.split(",")
|
|
28
|
-
.map(m => m.trim())
|
|
29
|
-
.filter(m => !m.startsWith("type"));
|
|
30
|
-
return modules
|
|
31
|
-
.map(m => {
|
|
32
|
-
const dashedModule = m.replace(/[A-Z]/g, (match, offset) => (offset > 0 ? "-" : "") + match.toLowerCase());
|
|
33
|
-
return `${initialSpacing}import ${m} from ${quote}lucide-${framework}/icons/${dashedModule}${quote}${lineEnding.trimEnd()}`;
|
|
34
|
-
})
|
|
35
|
-
.join("\n");
|
|
36
|
-
})
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
}
|
|
1
|
+
import { plugin } from "./plugin.js";
|
|
2
|
+
export default plugin;
|
|
41
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,eAAe,MAAM,CAAC"}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a raw string of imported icons to a list of module names
|
|
3
|
+
* @param rawModules {string} The raw string of imported icons
|
|
4
|
+
* @return {string[]} The list of imported icon
|
|
5
|
+
*/
|
|
6
|
+
export function rawModulesToModulesList(rawModules: string): string[];
|
|
7
|
+
/**
|
|
8
|
+
* Converts a PascalCase icon component name to a dashed string
|
|
9
|
+
* (e.g. `IconComp` to `icon-comp`)
|
|
10
|
+
* @param component {string} The PascalCase icon component name
|
|
11
|
+
* @return {string} The dashed icon component name
|
|
12
|
+
*/
|
|
13
|
+
export function iconCompToDashed(component: string): string;
|
|
14
|
+
export const importsMatcher: RegExp;
|
|
15
|
+
export function plugin(): import("vite").Plugin;
|
|
16
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,oDAHsB,MAAM,GAChB,MAAM,EAAE,CAOnB;AAED;;;;;GAKG;AACH,4CAHqB,MAAM,GACf,MAAM,CAWjB;AAED,oCAAiG;AAM1F,0BAFK,OAAO,MAAM,EAAE,MAAM,CAgC/B"}
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const ignoredPaths = ["/node_modules/", "/.svelte-kit/"];
|
|
2
|
+
/**
|
|
3
|
+
* Converts a raw string of imported icons to a list of module names
|
|
4
|
+
* @param rawModules {string} The raw string of imported icons
|
|
5
|
+
* @return {string[]} The list of imported icon
|
|
6
|
+
*/
|
|
7
|
+
export function rawModulesToModulesList(rawModules) {
|
|
8
|
+
return rawModules
|
|
9
|
+
.split(",")
|
|
10
|
+
.map(m => m.trim())
|
|
11
|
+
.filter(m => !!m && !m.startsWith("type"));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Converts a PascalCase icon component name to a dashed string
|
|
15
|
+
* (e.g. `IconComp` to `icon-comp`)
|
|
16
|
+
* @param component {string} The PascalCase icon component name
|
|
17
|
+
* @return {string} The dashed icon component name
|
|
18
|
+
*/
|
|
19
|
+
export function iconCompToDashed(component) {
|
|
20
|
+
return (component
|
|
21
|
+
.replace(/[A-Z\d]/g, (match, offset) => (offset > 0 ? "-" : "") + match.toLowerCase())
|
|
22
|
+
// fix for NxN icons
|
|
23
|
+
.replace(/(\d)x-(\d)/, (_, a, b) => `${a}x${b}`)
|
|
24
|
+
// exception for ClockNN
|
|
25
|
+
.replace(/clock-(\d)-(\d)/, (_, a, b) => `clock-${a}${b}`));
|
|
26
|
+
}
|
|
27
|
+
export const importsMatcher = /^(\s*)import\s+\{([^}]*)}\s+from\s+(["'])lucide-(.*?)["'](.*)$/gm;
|
|
28
|
+
/**
|
|
29
|
+
* The Vite plugin that preprocesses imports to optimize Lucide icons
|
|
30
|
+
* @return {import("vite").Plugin}
|
|
31
|
+
*/
|
|
32
|
+
export const plugin = () => ({
|
|
33
|
+
name: "vite-plugin-lucide-preprocess",
|
|
34
|
+
enforce: "pre",
|
|
35
|
+
transform: (code, path) => {
|
|
36
|
+
if (ignoredPaths.some(p => path.includes(p))) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
code: code.replace(importsMatcher,
|
|
41
|
+
/**
|
|
42
|
+
* @param _ The whole matched string
|
|
43
|
+
* @param initialSpacing {string} The spacing before the matched import statement
|
|
44
|
+
* @param modulesStr {string} The imported icons as a raw string
|
|
45
|
+
* @param quote {string} The quote used in the import statement (either ' or ")
|
|
46
|
+
* @param framework {string} The framework the icons are imported for
|
|
47
|
+
* @param lineEnding {string} The end of the line of the matched import statement (often a semicolon)
|
|
48
|
+
* @return The optimized import statement(s)
|
|
49
|
+
*/
|
|
50
|
+
(_, initialSpacing, modulesStr, quote, framework, lineEnding) => {
|
|
51
|
+
return rawModulesToModulesList(modulesStr)
|
|
52
|
+
.map(m => `${initialSpacing}import ${m} from ${quote}lucide-${framework}/icons/${iconCompToDashed(m)}${quote}${lineEnding.trimEnd()}`)
|
|
53
|
+
.join("\n");
|
|
54
|
+
})
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;AAEzD;;;;GAIG;AACH,MAAM,UAAU,uBAAuB,CAAC,UAAU;IACjD,OAAO,UAAU;SACf,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;SAClB,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAS;IACzC,OAAO,CACN,SAAS;SACP,OAAO,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,WAAW,EAAE,CAAC;QACtF,oBAAoB;SACnB,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QAChD,wBAAwB;SACvB,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAAC,CAC3D,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,kEAAkE,CAAC;AAEjG;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,CAAC;IAC5B,IAAI,EAAE,+BAA+B;IACrC,OAAO,EAAE,KAAK;IACd,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE;QACzB,IAAI,YAAY,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAC9C,OAAO;QACR,CAAC;QACD,OAAO;YACN,IAAI,EAAE,IAAI,CAAC,OAAO,CACjB,cAAc;YACd;;;;;;;;eAQG;YACH,CAAC,CAAC,EAAE,cAAc,EAAE,UAAU,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE;gBAC/D,OAAO,uBAAuB,CAAC,UAAU,CAAC;qBACxC,GAAG,CACH,CAAC,CAAC,EAAE,CACH,GAAG,cAAc,UAAU,CAAC,SAAS,KAAK,UAAU,SAAS,UAAU,gBAAgB,CAAC,CAAC,CAAC,GAAG,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAC5H;qBACA,IAAI,CAAC,IAAI,CAAC,CAAC;YACd,CAAC,CACD;SACD,CAAC;IACH,CAAC;CACD,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-lucide-preprocess",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"author": {
|
|
5
5
|
"url": "https://github.com/WarningImHack3r",
|
|
6
6
|
"name": "WarningImHack3r"
|
|
@@ -31,10 +31,12 @@
|
|
|
31
31
|
"vite": "^5"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
+
"lucide-svelte": "^0.427.0",
|
|
34
35
|
"prettier": "^3.3.3",
|
|
35
36
|
"publint": "^0.2.9",
|
|
36
37
|
"typescript": "^5.5.4",
|
|
37
|
-
"vite": "^5.4.0"
|
|
38
|
+
"vite": "^5.4.0",
|
|
39
|
+
"vitest": "^2.0.5"
|
|
38
40
|
},
|
|
39
41
|
"main": "./dist/index.js",
|
|
40
42
|
"types": "./dist/index.d.ts",
|
|
@@ -42,6 +44,7 @@
|
|
|
42
44
|
"scripts": {
|
|
43
45
|
"dev": "tsc -w",
|
|
44
46
|
"build": "tsc",
|
|
47
|
+
"test": "vitest",
|
|
45
48
|
"lint": "prettier --check . && publint",
|
|
46
49
|
"format": "prettier --write ."
|
|
47
50
|
}
|