Lädt...


🔧 Why Vite is the best? Advanced Features of Vite


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Learn about advanced features of Vite and how it can revolutionize your frontend development.

ite is revolutionizing the way we approach frontend development with its exceptional speed and efficiency. This blog explores the more sophisticated applications of Vite, highlighting its potential beyond mere app creation.

Pre-bundling Modules with Vite

A key highlight of Vite is its method of managing node_modules during development. By pre-bundling dependencies using esbuild, Vite significantly lightens the browser's workload, thus speeding up development.

Optimizing Pre-bundling:

Vite automatically pre-bundles dependencies, but you can fine-tune this process in vite.config.js. For instance:

// vite.config.js
export default {
  optimizeDeps: {
    exclude: ["dependency-to-exclude"],
    include: ["dependency-to-force-bundle"],
  },
};

This configuration allows you to optimize your project's performance by managing dependencies more effectively.

Developing Libraries with Vite

Vite is also highly efficient for library development, offering quick Hot Module Replacement (HMR) and an easy setup process.

Steps for Library Creation with Vite:

  1. Start a new project using a library-focused template.
  2. Set up your vite.config.js to define build options and output formats:
   export default {
     build: {
       lib: {
         entry: "src/mylib.js",
         name: "MyLib",
         formats: ["es", "umd"],
       },
     },
   };
  1. Benefit from Vite's rapid development feedback loop.
  2. Use vite build --mode lib to prepare your library for release.

Boosting SEO and Performance with SSR and SSG

For SEO and performance, SSR and SSG are essential. Vite natively supports these for frameworks like Vue and React.

Implementing SSR/SSG in Vite:

  • Activate SSR in Vite for server-side rendering, improving load times.
  • Utilize VitePress or external plugins for SSG, taking advantage of static site benefits and SEO improvements.

Expanding Functionality with Vite Plugins

Vite's extensive plugin ecosystem allows for the expansion of its core capabilities, including framework support and CSS preprocessing.

Plugin Installation:

  1. Add the desired plugin via npm.
  2. Include it in your vite.config.js:
   import vue from "@vitejs/plugin-vue";

   // vite.config.js
   export default {
     plugins: [vue()],
   };

Incorporating Pre-Processors

Vite seamlessly supports pre-processors like Sass, Less, and Stylus. Installing Sass, for example, is straightforward:

$ npm install -D sass

Then, refactor your App.jsx to use a new Counter component.

src/components/Counter.jsx

import { useState } from "react";
import styles from "./counter.module.scss";

const Counter = () => {
  const [count, setCount] = useState(0);

  return (
    <div className={styles.counter}>
      <button onClick={() => setCount((count) => count + 1)}>
        count is: {count}
      </button>
    </div>
  );
};

export default Counter;

To use Sass, simply create a .scss file and import it into your component. This also demonstrates the use of CSS modules.

src/components/counter.module.scss

.counter {
  background-color: bisque;
}

And update your App.jsx.

src/App.jsx

import "./App.css";
import Counter from "./components/Counter";

function App() {
  return (
    <div className="App">
      <Counter />
    </div>
  );
}

export default App;

Simplifying Imports with Absolute Paths in Vite

Avoiding complex relative paths is a boon, and Vite makes it easy to use absolute imports via a simple vite.config.js tweak.

vite.config.js

import path from "path";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

Also, inform your code editor of this configuration through jsconfig.json or tsconfig.json for TypeScript.

src/App.jsx then becomes simpler:

import "./App.css";
import Counter from "@/components/Counter";

function App() {
  return (
    <div className="App">
      <Counter />
    </div>
  );
}

export default App;

Managing Environmental Variables

Vite handles environmental variables differently, requiring a VITE_ prefix for client-side exposure.

.env

VITE_MESSAGE = "Hello Vite!";

Vite exposes these variables

via import.meta.env rather than process.env.

src/App.jsx

import "./App.css";
import Counter from "./components/Counter.jsx";

function App() {
  return (
    <div className="App">
      <Counter />
      {import.meta.env.VITE_MESSAGE}
    </div>
  );
}

export default App;

Is Vite the Future of Frontend Development?

Vite represents a leap forward in improving the developer experience, scalability, and performance of web applications. Its modern approach positions it as a compelling choice for future projects, underscoring the importance of selecting the right tool for your project's needs and team dynamics.

...

🔧 Why Vite is the best? Advanced Features of Vite


📈 43.32 Punkte
🔧 Programmierung

🔧 KISS Principle: Giữ Mọi Thứ Đơn Giản Nhất Có Thể


📈 32.65 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 32.65 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 32.65 Punkte
🔧 Programmierung

🔧 Laravel Mix vs Vite: Why did Laravel Transitioned to Vite


📈 27.49 Punkte
🔧 Programmierung

🔧 Make Your Vite Project LLM-Friendly with vite-plugin-llms


📈 22.9 Punkte
🔧 Programmierung

📰 Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


📈 22.9 Punkte
📰 IT Nachrichten

🔧 Webentwicklung: Build-Tool Vite.js 5.1 experimentiert mit Vite Runtime API


📈 22.9 Punkte
🔧 Programmierung

🐧 25 Best CDN Providers 2019 (sorted by best ent, best small biz, best budget and best free CDNs)


📈 19.02 Punkte
🐧 Linux Tipps

🔧 Understanding Environment Variables in Vite: Why VITE_ Prefix is Important


📈 16.04 Punkte
🔧 Programmierung

🔧 Why and How to Migrate Your React App from CRA to Vite


📈 16.04 Punkte
🔧 Programmierung

🔧 Why I Chose Vite.js for My React Projects


📈 16.04 Punkte
🔧 Programmierung

🔧 Why Vite.js Is Trending Everywhere


📈 16.04 Punkte
🔧 Programmierung

🔧 Vite vs Webpack: Which One and Why for Your Next React App (The Battle of Bundlers)


📈 16.04 Punkte
🔧 Programmierung

🔧 Why I choose CRA ( Create-react-app) over Vite for this React project ?


📈 16.04 Punkte
🔧 Programmierung

🔧 Best Vite Plugins to Supercharge Your Development Workflow


📈 15.25 Punkte
🔧 Programmierung

🔧 Best Practices for Using TypeScript in React with Vite


📈 15.25 Punkte
🔧 Programmierung

🕵️ Medium CVE-2017-17640: Advanced world database project Advanced world database


📈 13.67 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2017-17603: Advanced real estate script project Advanced real estate script


📈 13.67 Punkte
🕵️ Sicherheitslücken

📰 Advanced systems require advanced systems engineers


📈 13.67 Punkte
📰 IT Security Nachrichten

💾 New versions of Advanced ACT! Password Recovery and Advanced MailBox Password Recovery now available.


📈 13.67 Punkte
💾 IT Security Tools

💾 New versions of Advanced ZIP Password Recovery and Advanced Archive Password Recovery


📈 13.67 Punkte
💾 IT Security Tools

💾 Updates: Advanced Instant Messengers Password Recovery, Advanced Sage Password Recovery


📈 13.67 Punkte
💾 IT Security Tools

💾 Advanced PDF Password Recovery and Advanced Office Password Recovery updates


📈 13.67 Punkte
💾 IT Security Tools

🕵️ Medium CVE-2020-35598: Advanced comment system project Advanced comment system


📈 13.67 Punkte
🕵️ Sicherheitslücken

🕵️ Medium CVE-2020-12104: Wp-advanced-search project Wp-advanced-search


📈 13.67 Punkte
🕵️ Sicherheitslücken

🔧 Introduction to Language Integrated Query (LINQ) | C# Advanced [1 of 8] | C# Advanced


📈 13.67 Punkte
🔧 Programmierung

📰 Why Apple's best new AI features from WWDC will be boring (and I'm glad)


📈 13.59 Punkte
📰 IT Nachrichten

📰 I hope Apple's best AI features at WWDC will be boringly awesome. Here's why you should, too


📈 13.59 Punkte
📰 IT Nachrichten

🍏 Best macOS Catalina features: Why you should upgrade


📈 13.59 Punkte
🍏 iOS / Mac OS

📰 9 Best Features Why You Need A Windows 10 Operating System


📈 13.59 Punkte
📰 IT Security Nachrichten

matomo