### Start Svelte Development Server Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/www/README.md Run 'npm run dev' to start the development server. Add '-- --open' to automatically open the application in a new browser tab. ```sh npm run dev # or start the server and open the app in a new browser tab npm run dev -- --open ``` -------------------------------- ### Install vite-plugin-transform-lucide-imports Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/README.md Install the plugin as a development dependency using pnpm. ```sh pnpm install vite-plugin-transform-lucide-imports -D ``` -------------------------------- ### Install vite-plugin-transform-lucide-imports Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Installation instructions for the plugin using pnpm, npm, and yarn. ```sh # pnpm pnpm add -D vite-plugin-transform-lucide-imports # npm npm install --save-dev vite-plugin-transform-lucide-imports # yarn yarn add -D vite-plugin-transform-lucide-imports ``` -------------------------------- ### Configure Plugin Options with Extensions and Warning Handler Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Example of providing full Options to the plugin factory, including custom file extensions to process and a detailed warning handler function. This allows fine-grained control over the plugin's behavior. ```typescript import transformLucideImports, { SUPPORTED_EXTENSIONS, type Options, } from "vite-plugin-transform-lucide-imports"; // Full options example const options: Options = { // Override the list of file extensions the plugin will process. // Spread SUPPORTED_EXTENSIONS to keep the defaults and add your own. extensions: [...SUPPORTED_EXTENSIONS, ".vue"], // Custom warning handler. Receives the Warning object and the default handler. onwarn(warning, defaultHandler) { console.warn("[lucide-transform]", warning.message, warning.meta); // Call defaultHandler to also emit via Vite's built-in warning system: // defaultHandler(warning.message); }, }; // vite.config.ts usage: // plugins: [transformLucideImports(options)] ``` -------------------------------- ### Lucide Imports Transformation Example Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Demonstrates the transformation of named Lucide icon imports into individual default imports. This is the core functionality of the plugin, applied to source code before Vite compilation. ```typescript // BEFORE (your source code): import { BarIcon, Foo, Baz as Baz2Icon, type XIcon } from "@lucide/svelte"; // AFTER (what Vite actually compiles): // import type { XIcon } from '@lucide/svelte'; // import BarIcon from '@lucide/svelte/icons/bar'; // import Foo from '@lucide/svelte/icons/foo'; // import Baz2Icon from '@lucide/svelte/icons/baz'; ``` -------------------------------- ### Transform Named Lucide Imports Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/README.md Example demonstrating the transformation of named lucide-react-native imports into default imports and specific icon path imports. ```ts import { LucideFizzIcon, BarIcon, Foo, Baz as Baz2Icon, type XIcon } from "lucide-react-native"; ``` ```ts import type { XIcon } from "lucide-react-native"; import LucideFizzIcon from "lucide-react-native"; import BarIcon from "lucide-react-native/icons/bar"; import Foo from "lucide-react-native/icons/foo"; import Baz2Icon from "lucide-react-native/icons/baz"; ``` -------------------------------- ### Warning type definition and handling Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt The `Warning` type describes objects passed to `warn` callbacks and the `onwarn` plugin option. It includes a human-readable message, an optional original error, and extra metadata. You can customize warning handling, for example, to silence specific warnings for known packages. ```ts import { type Warning } from "vite-plugin-transform-lucide-imports"; // Type definition (for reference): // type Warning = { // message: string; // Human-readable description // error?: unknown; // Original Error when parsing fails // meta?: Record; // Extra context (e.g. packageName, path) // }; // Example: handling warnings in the plugin import { defineConfig } from "vite"; import transformLucideImports from "vite-plugin-transform-lucide-imports"; export default defineConfig({ plugins: [ transformLucideImports({ onwarn(warning: Warning, defaultHandler) { if ( warning.meta?.packageName && ["lucide-react", "lucide-vue-next"].includes( warning.meta.packageName as string ) ) { // Silence noisy "already tree-shaken" warnings for specific packages return; } defaultHandler(warning.message); }, }), ], }); ``` -------------------------------- ### Create a New Svelte Project Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/www/README.md Use 'npx sv create' to initialize a new Svelte project. Specify a directory name to create the project in a subfolder. ```sh npx sv create npx sv create my-app ``` -------------------------------- ### Build Svelte Project for Production Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/www/README.md Execute 'npm run build' to generate a production-ready version of your Svelte application. ```sh npm run build ``` -------------------------------- ### Access and Extend Supported File Extensions Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Demonstrates how to import and log the default SUPPORTED_EXTENSIONS array. It also shows how to use this constant to extend the default list when configuring the plugin, ensuring compatibility with additional file types like Vue. ```typescript import transformLucideImports, { SUPPORTED_EXTENSIONS, } from "vite-plugin-transform-lucide-imports"; console.log(SUPPORTED_EXTENSIONS); // [".ts", ".tsx", ".js", ".jsx", ".mjs", ".svelte"] // Use it to extend the default list without hardcoding it: import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; export default defineConfig({ plugins: [ vue(), transformLucideImports({ extensions: [...SUPPORTED_EXTENSIONS, ".vue"], }), ], }); ``` -------------------------------- ### plugin(options?) Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Registers the transform as a Vite plugin. Call it inside the `plugins` array of your Vite config. It intercepts every file whose extension matches the configured list and rewrites any qualifying Lucide named imports into default imports. ```APIDOC ## Default export — plugin(options?) (Vite plugin factory) Registers the transform as a Vite plugin. Call it inside the `plugins` array of your Vite config. It intercepts every file whose extension matches the configured list and rewrites any qualifying Lucide named imports into default imports. ```ts // vite.config.ts import { defineConfig } from "vite"; import { sveltekit } from "@sveltejs/kit/vite"; import transformLucideImports from "vite-plugin-transform-lucide-imports"; export default defineConfig({ // For SvelteKit / framework plugins: transformLucideImports MUST come AFTER // the framework plugin so that .svelte files are transpiled first. plugins: [ sveltekit(), transformLucideImports({ // Optional: add .vue or any other extension your framework uses // extensions: [...SUPPORTED_EXTENSIONS, ".vue"], // Optional: custom warning handler onwarn(warning, defaultHandler) { if (warning.meta?.packageName === "lucide-react") return; // silence specific warnings defaultHandler(warning.message); }, }), ], }); // --- What the plugin does at runtime --- // BEFORE (your source code): // import { BarIcon, Foo, Baz as Baz2Icon, type XIcon } from "@lucide/svelte"; // AFTER (what Vite actually compiles): // import type { XIcon } from '@lucide/svelte'; // import BarIcon from '@lucide/svelte/icons/bar'; // import Foo from '@lucide/svelte/icons/foo'; // import Baz2Icon from '@lucide/svelte/icons/baz'; ``` ``` -------------------------------- ### Configure Vite with Custom Extensions Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/README.md Extend the plugin to support additional file extensions like '.vue' by spreading the default supported extensions. ```ts import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import transformLucideImports, { SUPPORTED_EXTENSIONS } from "vite-plugin-transform-lucide-imports"; export default defineConfig({ // the plugin MUST be added after the plugin doing the transpilation // you may also want to spread the supported extensions to continue to support other extensions plugins: [vue(), transformLucideImports({ extensions: [...SUPPORTED_EXTENSIONS, ".vue"] })], }); ``` -------------------------------- ### Options type Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Configuration object accepted by the plugin factory. Both fields are optional. ```APIDOC ### `Options` type Configuration object accepted by the plugin factory. Both fields are optional. ```ts import transformLucideImports, { SUPPORTED_EXTENSIONS, type Options, } from "vite-plugin-transform-lucide-imports"; // Full options example const options: Options = { // Override the list of file extensions the plugin will process. // Spread SUPPORTED_EXTENSIONS to keep the defaults and add your own. extensions: [...SUPPORTED_EXTENSIONS, ".vue"], // Custom warning handler. Receives the Warning object and the default handler. onwarn(warning, defaultHandler) { console.warn("[lucide-transform]", warning.message, warning.meta); // Call defaultHandler to also emit via Vite's built-in warning system: // defaultHandler(warning.message); }, }; // vite.config.ts usage: // plugins: [transformLucideImports(options)] ``` ``` -------------------------------- ### Transform Lucide Imports Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Demonstrates how the plugin transforms imports for packages that are not natively tree-shaken and how it handles packages that are already tree-shaken. The `transform` function can optionally accept a `warn` callback. ```ts import { transform } from "vite-plugin-transform-lucide-imports"; // Non-tree-shaken → transformed console.log(transform("import { BarIcon } from 'lucide-svelte';")); // → "\nimport BarIcon from 'lucide-svelte/icons/bar';"; // Already tree-shaken → returned as-is + warning fired const warn = (w: { message: string }) => console.warn(w.message); console.log(transform("import { BarIcon } from 'lucide-react';", { warn })); // warning: "Skipping optimization of lucide-react because lucide-react is already a tree shaken package" // → "import { BarIcon } from 'lucide-react';" (unchanged) ``` -------------------------------- ### Configure Vite with transformLucideImports Source: https://github.com/ieedan/vite-plugin-transform-lucide-imports/blob/main/README.md Integrate the transformLucideImports plugin into your Vite configuration. Ensure it's added after other framework-specific plugins if necessary. ```ts import { defineConfig } from "vite"; import transformLucideImports from "vite-plugin-transform-lucide-imports"; export default defineConfig({ plugins: [/* other framework plugins */, transformLucideImports()], }); ``` -------------------------------- ### SUPPORTED_EXTENSIONS constant Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt An exported array listing the file extensions processed by the plugin when no `extensions` option is provided. ```APIDOC ### `SUPPORTED_EXTENSIONS` constant An exported array listing the file extensions processed by the plugin when no `extensions` option is provided. ```ts import transformLucideImports, { SUPPORTED_EXTENSIONS, } from "vite-plugin-transform-lucide-imports"; console.log(SUPPORTED_EXTENSIONS); // [ ".ts", ".tsx", ".js", ".jsx", ".mjs", ".svelte" ] // Use it to extend the default list without hardcoding it: import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; export default defineConfig({ plugins: [ vue(), transformLucideImports({ extensions: [...SUPPORTED_EXTENSIONS, ".vue"], }), ], }); ``` ``` -------------------------------- ### Configure Vite Plugin for Lucide Imports Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Register the transformLucideImports plugin in your vite.config.ts. Ensure it comes after framework plugins like sveltekit() to process .svelte files correctly. You can optionally configure a custom warning handler. ```typescript import { defineConfig } from "vite"; import { sveltekit } from "@sveltejs/kit/vite"; import transformLucideImports from "vite-plugin-transform-lucide-imports"; export default defineConfig({ // For SvelteKit / framework plugins: transformLucideImports MUST come AFTER // the framework plugin so that .svelte files are transpiled first. plugins: [ sveltekit(), transformLucideImports({ // Optional: add .vue or any other extension your framework uses // extensions: [...SUPPORTED_EXTENSIONS, ".vue"], // Optional: custom warning handler onwarn(warning, defaultHandler) { if (warning.meta?.packageName === "lucide-react") return; // silence specific warnings defaultHandler(warning.message); }, }), ], }); // --- What the plugin does at runtime --- // BEFORE (your source code): // import { BarIcon, Foo, Baz as Baz2Icon, type XIcon } from "@lucide/svelte"; // AFTER (what Vite actually compiles): // // import type { XIcon } from '@lucide/svelte'; // // import BarIcon from '@lucide/svelte/icons/bar'; // // import Foo from '@lucide/svelte/icons/foo'; // // import Baz2Icon from '@lucide/svelte/icons/baz'; ``` -------------------------------- ### Icon name normalization Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt The `normalizeName` function converts PascalCase Lucide icon names to kebab-case path segments, stripping optional `Lucide` prefixes and `Icon` suffixes. This function is used internally by `transform` to generate correct import paths. ```ts import { normalizeName } from "vite-plugin-transform-lucide-imports"; // Strips "Icon" suffix normalizeName("FooBarIcon"); // "foo-bar" normalizeName("FooBar2Icon"); // "foo-bar-2" // Strips "Lucide" prefix normalizeName("LucideBar"); // "bar" normalizeName("LucideBar2"); // "bar-2" // Strips both prefix and suffix normalizeName("LucideFooBarIcon"); // "foo-bar" normalizeName("LucideFooBar2Icon"); // "foo-bar-2" // Plain PascalCase (no prefix/suffix) normalizeName("FooBar"); // "foo-bar" normalizeName("FooBar2"); // "foo-bar-2" // These results map directly to Lucide's sub-path exports: // normalizeName("TrafficLightIcon") → "traffic-light" // → import TrafficLightIcon from 'lucide-svelte/icons/traffic-light'; ``` -------------------------------- ### transform(code, options?) Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt The core AST transformation function. It parses TypeScript/JavaScript source code, identifies Lucide import declarations, and returns the rewritten source string. It can optionally accept a configuration object for custom warning handling. ```APIDOC ## transform(code, options?) ### Description Parses TypeScript/JavaScript source code, walks the AST for Lucide import declarations, and returns the rewritten source string. Returns `undefined` if parsing fails (a warning is emitted via the `warn` callback). ### Parameters #### Path Parameters - **code** (string) - Required - The input code string to transform. - **options** (object) - Optional - Configuration options for the transform function. - **warn** (function) - Optional - A callback function to handle warnings during transformation. It receives a `Warning` object. ### Request Example ```ts import { transform, type Warning } from "vite-plugin-transform-lucide-imports"; const input = `import { BarIcon, Foo, Baz as Baz2Icon, type XIcon } from '@lucide/svelte';`; const output = transform(input); console.log(output); // Example with custom warning handler const result = transform(input, { warn(warning: Warning) { console.warn("Transform warning:", warning.message); }, }); // Example with invalid syntax const bad = transform("import { }", { warn(w) { console.error("Parse error:", w.message); }, }); console.log(bad); // undefined ``` ### Response #### Success Response (string) - Returns the rewritten source string with optimized Lucide imports. #### Failure Response (undefined) - Returns `undefined` if the input code cannot be parsed. A warning will be emitted. ### Response Example ```javascript // For basic usage: // import type { XIcon } from '@lucide/svelte'; // import BarIcon from '@lucide/svelte/icons/bar'; // import Foo from '@lucide/svelte/icons/foo'; // import Baz2Icon from '@lucide/svelte/icons/baz'; ``` ``` -------------------------------- ### normalizeName(name) Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Converts a PascalCase Lucide icon name to the kebab-case path segment used in Lucide's per-icon module paths. This function is used internally by `transform`. ```APIDOC ## normalizeName(name) ### Description Converts a PascalCase Lucide icon name (with optional `Lucide` prefix and/or `Icon` suffix) to the kebab-case path segment used in Lucide's per-icon module paths. This is the same function used internally by `transform` to build the rewritten import paths. ### Parameters #### Path Parameters - **name** (string) - Required - The PascalCase icon name to normalize. ### Request Example ```ts import { normalizeName } from "vite-plugin-transform-lucide-imports"; console.log(normalizeName("FooBarIcon")); // "foo-bar" console.log(normalizeName("LucideBar")); // "bar" console.log(normalizeName("LucideFooBarIcon")); // "foo-bar" console.log(normalizeName("FooBar")); // "foo-bar" ``` ### Response #### Success Response (string) - Returns the kebab-case string representing the icon's path segment. ### Response Example ```javascript // "foo-bar" // "bar" // "foo-bar" // "foo-bar" ``` ``` -------------------------------- ### Core transform function usage Source: https://context7.com/ieedan/vite-plugin-transform-lucide-imports/llms.txt Use the `transform` function to rewrite Lucide icon import declarations in TypeScript/JavaScript source code. It returns `undefined` and emits a warning if parsing fails. Custom warning handlers can be provided. ```ts import { transform, type Warning } from "vite-plugin-transform-lucide-imports"; // --- Basic usage --- const input = `import { BarIcon, Foo, Baz as Baz2Icon, type XIcon } from '@lucide/svelte';`; const output = transform(input); console.log(output); // import type { XIcon } from '@lucide/svelte'; // import BarIcon from '@lucide/svelte/icons/bar'; // import Foo from '@lucide/svelte/icons/foo'; // import Baz2Icon from '@lucide/svelte/icons/baz'; // --- With custom warning handler --- const result = transform(input, { warn(warning: Warning) { console.warn("Transform warning:", warning.message); // warning.meta?.packageName — the Lucide package name (when tree-shaken warning) // warning.error — the original parse error (when syntax is invalid) }, }); // --- Invalid syntax returns undefined and fires warn --- const bad = transform("import { }", { warn(w) { console.error("Parse error:", w.message); // "Could not parse file Error: ..." }, }); console.log(bad); // undefined // --- Tree-shaken packages are skipped with a warning --- const treeShaken = transform("import { BarIcon } from 'lucide-react';", { warn(w) { console.warn(w.message); // "Skipping optimization of lucide-react because lucide-react is already a tree shaken package" }, }); console.log(treeShaken); // "import { BarIcon } from 'lucide-react';" (unchanged) // --- Default imports are left untouched --- const defaultImport = transform( "import TrafficLightIcon from '@lucide/svelte/icons/traffic-light';" ); console.log(defaultImport); // "import TrafficLightIcon from '@lucide/svelte/icons/traffic-light';" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.