Git Product home page Git Product logo

vite-entry-shaking-lazyload's Introduction

vite-entry-shaking-lazy-load

Mimic tree-shaking behaviour when importing code from an entry file in development & serve mode.

Warning This plugin is experimental, bugs might be expected and some edge cases might not be covered.

Note The main execution logic of this plugin only applies to development mode because it addresses an issue which is specific to development mode.

Features

  • I have a files in src/charts/index.ts
export {Chart1} from "./Chart1.tsx"
export {Chart2} from "./Chart2.tsx"
export {Chart3} from "./Chart3.tsx"

In App.tsx, i used Chart1

import {Chart1} from "./charts"

const App = () => {
    return <Chart1 / >
}

Although only using Chart1, but Chart2 and Chart3 are also loaded

This plugin was written to solve that problem

  • Plugin support lazy load & @loadable/components With lazy load react
import {lazy} from "React";

const Chart1 = lazy(() => import("./charts").then(m => ({default: m.Chart1})))
const App = () => {
    return <Chart1 / >
}

With @loadable/components

const Chart1 = loadable(() => import("./components/chart"), {
    resolveComponent: cpn => cpn.Chart1
})
const App = () => {
    return <Chart1 / >
}

Work well

Install

# Using npm
npm i -D vite-entry-shaking-lazy-load
# Using Yarn
yarn add -D vite-entry-shaking-lazy-load
# Using pnpm
pnpm add -D vite-entry-shaking-lazy-load

Usage

Setup the plugin in your Vite configuration file.

import {resolve} from 'path';
import {defineConfig} from 'vite';
import EntryShakingPlugin from 'vite-entry-shaking-lazy-load';

const plugins = [
    EntryShakingPlugin({
        targets: [resolve(__dirname, 'src/charts')],
    })]

export default defineConfig(async () => ({
    plugins
}));

if your project uses alias import, you should run with alias resolve

import {resolve} from 'path';
import {defineConfig} from 'vite';
import EntryShakingPlugin from 'vite-entry-shaking-lazy-load';

const plugins = [
    EntryShakingPlugin({
        targets: [resolve(__dirname, 'src/charts')],
    })]

export default defineConfig(async () => ({
    resolve: {
        alias: [{
            // examples
            find: "@component", replacement: path.resolve(__dirname, "./src/components")
        }]
    },
    plugins
}));

vite-entry-shaking-lazyload's People

Contributors

nguyenbatranvan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.