vite-plugin-lucide-preprocess 1.2.4 → 1.3.1
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 +24 -22
- package/dist/plugin.d.ts +7 -2
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +13 -6
- package/dist/plugin.js.map +1 -1
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,50 +1,52 @@
|
|
|
1
1
|
# vite-plugin-lucide-preprocess
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-

|
|
6
|
-

|
|
3
|
+
[](https://www.npmjs.com/package/vite-plugin-lucide-preprocess?activeTab=versions)
|
|
4
|
+
[](https://www.npmjs.com/package/vite-plugin-lucide-preprocess)
|
|
5
|
+
[](https://www.npmjs.com/package/vite-plugin-lucide-preprocess)
|
|
6
|
+
[](LICENSE)
|
|
7
7
|
|
|
8
|
-
A [Vite](https://
|
|
8
|
+
A [Vite](https://vite.dev) plugin to replace imports for [Lucide icons](https://lucide.dev).
|
|
9
9
|
|
|
10
10
|
## What is this?
|
|
11
11
|
|
|
12
12
|
### Motivation
|
|
13
13
|
|
|
14
|
-
If you want to import Lucide icons in your project, you can
|
|
15
|
-
|
|
14
|
+
If you want to import Lucide icons in your project, you can (usually — depending on
|
|
15
|
+
the package) use two methods:
|
|
16
16
|
|
|
17
17
|
```js
|
|
18
|
-
import { IconName } from "lucide
|
|
18
|
+
import { IconName } from "@lucide/svelte";
|
|
19
19
|
// or
|
|
20
|
-
import IconName from "lucide
|
|
20
|
+
import IconName from "@lucide/svelte/icons/icon-name";
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
While the first method is more convenient (and may be the one used by auto-import features in your editor),
|
|
24
24
|
it is not tree-shakable.
|
|
25
|
-
In fact, a single import from `"lucide-<framework>"`
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
In fact, a single import from `"lucide-<framework>"` or `"@lucide/<framework>"`
|
|
26
|
+
[will import all icons](https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess/issues/11#issuecomment-2445209558);
|
|
27
|
+
not ideal for Vite dev server performance and build time.
|
|
28
28
|
|
|
29
29
|
**This plugin will replace the first method with the second one, so you can keep the convenience of the first method
|
|
30
30
|
while benefiting from tree-shaking optimizations.** “Correct” imports will be kept as-is.
|
|
31
31
|
|
|
32
32
|
> [!NOTE]
|
|
33
|
-
> The plugin is primarily intended for
|
|
34
|
-
> any Lucide package using Vite
|
|
33
|
+
> The plugin is primarily intended for `@lucide/svelte`, but it works with
|
|
34
|
+
> any Lucide package using Vite.
|
|
35
|
+
> If it doesn't support a Lucide implementation,
|
|
36
|
+
> [please open an issue](https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess).
|
|
35
37
|
|
|
36
38
|
### Credits and opinion
|
|
37
39
|
|
|
38
|
-
I got the idea from [Phosphor Icons'
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
essentially the same).
|
|
40
|
+
I got the idea from [Phosphor Icons' optimizer](https://github.com/haruaki07/phosphor-svelte#import-optimizer).
|
|
41
|
+
However, this plugin is written from scratch and is not based on the Phosphor one, even though the technique is
|
|
42
|
+
essentially the same.
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
Contrary to the experimental version of Phosphor Icons' implementation, this plugin is
|
|
45
|
+
a Vite plugin and not a Svelte one, it is more generic and can be used with any framework.
|
|
46
|
+
Additionally, it doesn't suffer from the limitations of the Svelte plugin, preventing the old Phosphor
|
|
45
47
|
optimizer from working without disabling the native Vite optimizations of `phosphor-svelte`.
|
|
46
48
|
|
|
47
|
-
This should subjectively be a feature embedded in Lucide itself (
|
|
49
|
+
This should subjectively be a feature embedded in Lucide itself (`@lucide/svelte/preprocessor`),
|
|
48
50
|
but I don't know if it will ever be implemented.
|
|
49
51
|
[I opened an issue](https://github.com/lucide-icons/lucide/issues/2295) on the `lucide-icons` repository to discuss
|
|
50
52
|
this.
|
|
@@ -75,7 +77,7 @@ import { defineConfig } from "vite";
|
|
|
75
77
|
import lucidePreprocess from "vite-plugin-lucide-preprocess";
|
|
76
78
|
|
|
77
79
|
export default defineConfig({
|
|
78
|
-
|
|
80
|
+
plugins: [lucidePreprocess() /* , ... */]
|
|
79
81
|
});
|
|
80
82
|
```
|
|
81
83
|
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
+
/** @typedef {{ name: string; importName: string }} Module */
|
|
1
2
|
/**
|
|
2
3
|
* Converts a raw string of imported icons to a list of module names,
|
|
3
4
|
* and a list of TypeScript types
|
|
4
5
|
* @param rawModules {string} The raw string of imported icons
|
|
5
|
-
* @return {[
|
|
6
|
+
* @return {[Module[], string[]]} A list containing a list of imported icons,
|
|
6
7
|
* and a list of TypeScript types
|
|
7
8
|
*/
|
|
8
|
-
export function rawModulesToLists(rawModules: string): [
|
|
9
|
+
export function rawModulesToLists(rawModules: string): [Module[], string[]];
|
|
9
10
|
/**
|
|
10
11
|
* Converts a PascalCase icon component name to a dashed string
|
|
11
12
|
* (e.g. `IconComp` to `icon-comp`)
|
|
@@ -27,6 +28,10 @@ export function frameworkImportPath(framework: string, options: Options): string
|
|
|
27
28
|
*/
|
|
28
29
|
export function plugin(options?: Options): import("vite").Plugin;
|
|
29
30
|
export const importsMatcher: RegExp;
|
|
31
|
+
export type Module = {
|
|
32
|
+
name: string;
|
|
33
|
+
importName: string;
|
|
34
|
+
};
|
|
30
35
|
export type Options = {
|
|
31
36
|
/**
|
|
32
37
|
* The import type for React
|
package/dist/plugin.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAIA;;;;;;GAMG;AACH,8CAJsB,MAAM,GAChB,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAIA,6DAA6D;AAE7D;;;;;;GAMG;AACH,8CAJsB,MAAM,GAChB,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,CAAC,CAiB/B;AAED;;;;;GAKG;AACH,4CAHqB,MAAM,GACf,MAAM,CAgBjB;AAED;;;;;GAKG;AACH,+CAJqB,MAAM,WACR,OAAO,GACd,MAAM,CAYjB;AAkBD;;;;GAIG;AACH,iCAHmB,OAAO,GACd,OAAO,MAAM,EAAE,MAAM,CA6ChC;AAhED,oCACyF;qBAlE3E;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE"}
|
package/dist/plugin.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import renamedReplacementsModules from "./renamedReplacements.js";
|
|
2
2
|
const ignoredPaths = ["/node_modules/", "/.svelte-kit/"];
|
|
3
|
+
/** @typedef {{ name: string; importName: string }} Module */
|
|
3
4
|
/**
|
|
4
5
|
* Converts a raw string of imported icons to a list of module names,
|
|
5
6
|
* and a list of TypeScript types
|
|
6
7
|
* @param rawModules {string} The raw string of imported icons
|
|
7
|
-
* @return {[
|
|
8
|
+
* @return {[Module[], string[]]} A list containing a list of imported icons,
|
|
8
9
|
* and a list of TypeScript types
|
|
9
10
|
*/
|
|
10
11
|
export function rawModulesToLists(rawModules) {
|
|
@@ -13,7 +14,12 @@ export function rawModulesToLists(rawModules) {
|
|
|
13
14
|
.map(m => m.trim())
|
|
14
15
|
.filter(m => !!m);
|
|
15
16
|
return [
|
|
16
|
-
raw
|
|
17
|
+
raw
|
|
18
|
+
.filter(m => !m.startsWith("type "))
|
|
19
|
+
.map(m => {
|
|
20
|
+
const [original, newName] = m.split(" as ");
|
|
21
|
+
return { name: newName ?? original, importName: original };
|
|
22
|
+
}),
|
|
17
23
|
raw.filter(m => m.startsWith("type ")).map(m => m.slice(5))
|
|
18
24
|
];
|
|
19
25
|
}
|
|
@@ -55,7 +61,7 @@ export function frameworkImportPath(framework, options) {
|
|
|
55
61
|
return "/icons/";
|
|
56
62
|
}
|
|
57
63
|
}
|
|
58
|
-
export const importsMatcher = /^(\s*)import\s+\{([^}]*)}\s+from\s+(["'])lucide
|
|
64
|
+
export const importsMatcher = /^(\s*)import\s+\{([^}]*)}\s+from\s+(["'])(lucide-|@lucide\/)(?!lab\b)(\S+)["'](.*)$/gm;
|
|
59
65
|
/**
|
|
60
66
|
* @typedef {Object} Options
|
|
61
67
|
* @property {"cjs" | "esm"} [importMode="esm"] The import type for React
|
|
@@ -87,16 +93,17 @@ export function plugin(options = defaultOptions) {
|
|
|
87
93
|
* @param initialSpacing {string} The spacing before the matched import statement
|
|
88
94
|
* @param modulesStr {string} The imported icons as a raw string
|
|
89
95
|
* @param quote {string} The quote used in the import statement (either ' or ")
|
|
96
|
+
* @param importStyle {string} The method the icons are imported with
|
|
90
97
|
* @param framework {string} The framework the icons are imported for
|
|
91
98
|
* @param lineEnding {string} The end of the line of the matched import statement (often a semicolon)
|
|
92
99
|
* @return The optimized import statement(s)
|
|
93
100
|
*/
|
|
94
|
-
(_, initialSpacing, modulesStr, quote, framework, lineEnding) => {
|
|
101
|
+
(_, initialSpacing, modulesStr, quote, importStyle, framework, lineEnding) => {
|
|
95
102
|
const [modules, types] = rawModulesToLists(modulesStr);
|
|
96
103
|
const moduleImports = modules
|
|
97
|
-
.map(m => `${initialSpacing}import ${m} from ${quote}
|
|
104
|
+
.map(m => `${initialSpacing}import ${m.name} from ${quote}${importStyle}${framework}${frameworkImportPath(framework, mergedOptions)}${iconCompToDashed(m.importName)}${quote}${lineEnding.trimEnd()}`)
|
|
98
105
|
.join(initialSpacing.includes("\n") ? "" : "\n");
|
|
99
|
-
const typesImport = `${initialSpacing}import type { ${types.join(", ")} } from ${quote}
|
|
106
|
+
const typesImport = `${initialSpacing}import type { ${types.join(", ")} } from ${quote}${importStyle}${framework}${quote}${lineEnding.trimEnd()}`;
|
|
100
107
|
return [
|
|
101
108
|
types.length ? typesImport : undefined,
|
|
102
109
|
modules.length ? moduleImports : undefined
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,MAAM,0BAA0B,CAAC;AAElE,MAAM,YAAY,GAAG,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;AAEzD;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAU;IAC3C,MAAM,GAAG,GAAG,UAAU;SACpB,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,CAAC,CAAC;IACnB,OAAO;QACN,GAAG,
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAAA,OAAO,0BAA0B,MAAM,0BAA0B,CAAC;AAElE,MAAM,YAAY,GAAG,CAAC,gBAAgB,EAAE,eAAe,CAAC,CAAC;AAEzD,6DAA6D;AAE7D;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAU;IAC3C,MAAM,GAAG,GAAG,UAAU;SACpB,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,CAAC,CAAC;IACnB,OAAO;QACN,GAAG;aACD,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;aACnC,GAAG,CAAC,CAAC,CAAC,EAAE;YACR,MAAM,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;YAC5C,OAAO,EAAE,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CAAC,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;oBAClD,MAAM,WAAW,GAAG,GAAG,cAAc,iBAAiB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,KAAK,GAAG,WAAW,GAAG,SAAS,GAAG,KAAK,GAAG,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC;oBAClJ,OAAO;wBACN,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS;wBACtC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS;qBAC1C;yBACC,MAAM,CAAC,OAAO,CAAC;yBACf,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBACnD,CAAC,CACD;aACD,CAAC;QACH,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-lucide-preprocess",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"author": {
|
|
5
5
|
"url": "https://github.com/WarningImHack3r",
|
|
6
6
|
"name": "WarningImHack3r"
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess"
|
|
11
|
+
"url": "git+https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess.git"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"vite",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"vite": ">=5"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"lucide-svelte": "^0.
|
|
35
|
-
"prettier": "^3.
|
|
36
|
-
"publint": "^0.3.
|
|
37
|
-
"typescript": "^5.
|
|
38
|
-
"vite": "^6.0.
|
|
39
|
-
"vitest": "^3.0.
|
|
34
|
+
"lucide-svelte": "^0.515.0",
|
|
35
|
+
"prettier": "^3.0.0",
|
|
36
|
+
"publint": "^0.3.0",
|
|
37
|
+
"typescript": "^5.0.0",
|
|
38
|
+
"vite": "^6.0.0",
|
|
39
|
+
"vitest": "^3.0.0"
|
|
40
40
|
},
|
|
41
41
|
"main": "./dist/index.js",
|
|
42
42
|
"types": "./dist/index.d.ts",
|