vite-plugin-lucide-preprocess 1.1.1 → 1.2.0

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 CHANGED
@@ -11,8 +11,8 @@ A [Vite](https://vitejs.dev) plugin to replace imports for [Lucide icons](https:
11
11
 
12
12
  ### Motivation
13
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
+ If you want to import Lucide icons in your project, you can use two methods since
15
+ [this PR on Lucide](https://github.com/lucide-icons/lucide/pull/1707) (at least for `lucide-svelte`):
16
16
 
17
17
  ```js
18
18
  import { IconName } from "lucide-svelte";
@@ -21,8 +21,10 @@ 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
- 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.
24
+ it is not tree-shakable.
25
+ In fact, a single import from `"lucide-<framework>"` will import all icons, not ideal for Vite dev
26
+ server performance and build time.
27
+ [More information here](https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess/issues/11#issuecomment-2445209558).
26
28
 
27
29
  **This plugin will replace the first method with the second one, so you can keep the convenience of the first method
28
30
  while benefiting from tree-shaking optimizations.** “Correct” imports will be kept as-is.
@@ -33,8 +35,8 @@ while benefiting from tree-shaking optimizations.** “Correct” imports will b
33
35
 
34
36
  ### Credits and opinion
35
37
 
36
- I got the idea
37
- from [Phosphor Icons' optimizer](https://github.com/haruaki07/phosphor-svelte#import-optimizer-experimental).
38
+ I got the idea from [Phosphor Icons'
39
+ optimizer](https://github.com/haruaki07/phosphor-svelte#import-optimizer-experimental).
38
40
  However, this plugin is written from scratch and is not based on the Phosphor one (even though the technique is
39
41
  essentially the same).
40
42
 
@@ -42,7 +44,7 @@ As it is a Vite plugin and not a Svelte one, it is more generic and can be used
42
44
  Additionally, it doesn't suffer from the limitations of the Svelte plugin, preventing the Phosphor
43
45
  optimizer from working without disabling the native Vite optimizations of `phosphor-svelte`.
44
46
 
45
- In my opinion, this should be a feature embedded in Lucide itself (`lucide-svelte/preprocessor`),
47
+ This should subjectively be a feature embedded in Lucide itself (`lucide-svelte/preprocessor`),
46
48
  but I don't know if it will ever be implemented.
47
49
  [I opened an issue](https://github.com/lucide-icons/lucide/issues/2295) on the `lucide-icons` repository to discuss
48
50
  this.
@@ -54,10 +56,9 @@ It helps support the renamed and deprecated icons by automatically updating the
54
56
 
55
57
  If you use a recently deprecated icon and can't make your build work, please wait for a few hours for the plugin
56
58
  to update itself.
57
- If the issue persists, please open an issue on this repository.
59
+ If the problem persists, please open an issue in this repository.
58
60
 
59
- More information about the auto-updater can be found
60
- in https://github.com/WarningImHack3r/vite-plugin-lucide-preprocess/pull/6.
61
+ More information about the auto-updater can be found in WarningImHack3r/vite-plugin-lucide-preprocess#6.
61
62
 
62
63
  ## Installation
63
64
 
@@ -81,6 +82,19 @@ export default defineConfig({
81
82
  > [!WARNING]
82
83
  > Make sure to add the plugin **before** any other preprocessors in the `plugins` array.
83
84
 
85
+ ## Options
86
+
87
+ The plugin optionally accepts an object with the following options:
88
+
89
+ - `importMode` (default: `"esm"`): The import mode for CommonJS-compatible frameworks.
90
+
91
+ - `"esm"`: Import the icons as ES modules.
92
+ - `"cjs"`: Import the icons as CommonJS modules.
93
+
94
+ This option is useful if you use a CommonJS-compatible framework like React or Vue.
95
+ It will automatically replace the imports with the correct syntax depending on whether
96
+ your framework supports CJS or not.
97
+
84
98
  ## License
85
99
 
86
100
  MIT
package/dist/plugin.d.ts CHANGED
@@ -13,10 +13,24 @@ export function rawModulesToLists(rawModules: string): [string[], string[]];
13
13
  * @return {string} The dashed icon component name, or the renamed one if it exists
14
14
  */
15
15
  export function iconCompToDashed(component: string): string;
16
+ /**
17
+ * Returns the import path for a specific Lucide framework
18
+ * @param framework {string} The Lucide framework
19
+ * @param options {Options} The plugin options
20
+ * @return {string} The import path for the Lucide framework
21
+ */
22
+ export function frameworkImportPath(framework: string, options: Options): string;
16
23
  /**
17
24
  * The Vite plugin that preprocesses imports to optimize Lucide icons
25
+ * @param options {Options} The plugin options
18
26
  * @return {import("vite").Plugin}
19
27
  */
20
- export function plugin(): import("vite").Plugin;
28
+ export function plugin(options?: Options): import("vite").Plugin;
21
29
  export const importsMatcher: RegExp;
30
+ export type Options = {
31
+ /**
32
+ * The import type for React
33
+ */
34
+ importMode?: "cjs" | "esm" | undefined;
35
+ };
22
36
  //# sourceMappingURL=plugin.d.ts.map
@@ -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,CAY/B;AAED;;;;;GAKG;AACH,4CAHqB,MAAM,GACf,MAAM,CAgBjB;AAID;;;GAGG;AACH,0BAFY,OAAO,MAAM,EAAE,MAAM,CAwChC;AA5CD,oCAAiG"}
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,CAY/B;AAED;;;;;GAKG;AACH,4CAHqB,MAAM,GACf,MAAM,CAgBjB;AAED;;;;;GAKG;AACH,+CAJqB,MAAM,WACR,OAAO,GACd,MAAM,CAajB;AAiBD;;;;GAIG;AACH,iCAHmB,OAAO,GACd,OAAO,MAAM,EAAE,MAAM,CA4ChC;AA9DD,oCAAiG"}
package/dist/plugin.js CHANGED
@@ -38,12 +38,43 @@ export function iconCompToDashed(component) {
38
38
  .replace(/-icon$/, "");
39
39
  return renamedReplacementsModules[computedDashed] ?? computedDashed;
40
40
  }
41
+ /**
42
+ * Returns the import path for a specific Lucide framework
43
+ * @param framework {string} The Lucide framework
44
+ * @param options {Options} The plugin options
45
+ * @return {string} The import path for the Lucide framework
46
+ */
47
+ export function frameworkImportPath(framework, options) {
48
+ switch (framework) {
49
+ case "react":
50
+ case "vue":
51
+ case "vue-next":
52
+ case "solid":
53
+ case "preact":
54
+ return `/dist/${options.importMode}/icons/`;
55
+ default:
56
+ return "/icons/";
57
+ }
58
+ }
41
59
  export const importsMatcher = /^(\s*)import\s+\{([^}]*)}\s+from\s+(["'])lucide-(.*?)["'](.*)$/gm;
60
+ /**
61
+ * @typedef {Object} Options
62
+ * @property {"cjs" | "esm"} [importMode="esm"] The import type for React
63
+ */
64
+ /**
65
+ * The default plugin options
66
+ * @type {Options}
67
+ */
68
+ const defaultOptions = {
69
+ importMode: "esm"
70
+ };
42
71
  /**
43
72
  * The Vite plugin that preprocesses imports to optimize Lucide icons
73
+ * @param options {Options} The plugin options
44
74
  * @return {import("vite").Plugin}
45
75
  */
46
- export function plugin() {
76
+ export function plugin(options = defaultOptions) {
77
+ const mergedOptions = { ...defaultOptions, ...options };
47
78
  return {
48
79
  name: "vite-plugin-lucide-preprocess",
49
80
  enforce: "pre",
@@ -64,7 +95,7 @@ export function plugin() {
64
95
  (_, initialSpacing, modulesStr, quote, framework, lineEnding) => {
65
96
  const [modules, types] = rawModulesToLists(modulesStr);
66
97
  const moduleImports = modules
67
- .map(m => `${initialSpacing}import ${m} from ${quote}lucide-${framework}/icons/${iconCompToDashed(m)}${quote}${lineEnding.trimEnd()}`)
98
+ .map(m => `${initialSpacing}import ${m} from ${quote}lucide-${framework}${frameworkImportPath(framework, mergedOptions)}${iconCompToDashed(m)}${quote}${lineEnding.trimEnd()}`)
68
99
  .join(initialSpacing.includes("\n") ? "" : "\n");
69
100
  const typesImport = `${initialSpacing}import type { ${types.join(", ")} } from ${quote}lucide-${framework}${quote}${lineEnding.trimEnd()}`;
70
101
  return [
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.js"],"names":[],"mappings":"AAAA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AC,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.1.1",
3
+ "version": "1.2.0",
4
4
  "author": {
5
5
  "url": "https://github.com/WarningImHack3r",
6
6
  "name": "WarningImHack3r"
@@ -31,12 +31,12 @@
31
31
  "vite": "^5"
32
32
  },
33
33
  "devDependencies": {
34
- "lucide-svelte": "^0.452.0",
34
+ "lucide-svelte": "^0.454.0",
35
35
  "prettier": "^3.3.3",
36
- "publint": "^0.2.11",
36
+ "publint": "^0.2.12",
37
37
  "typescript": "^5.6.3",
38
- "vite": "^5.4.8",
39
- "vitest": "^2.1.2"
38
+ "vite": "^5.4.10",
39
+ "vitest": "^2.1.4"
40
40
  },
41
41
  "main": "./dist/index.js",
42
42
  "types": "./dist/index.d.ts",