Git Product home page Git Product logo

vite-plugin-entries-build-cache's Introduction

vite-plugin-entries-build-cache

English | 简体中文

vite-plugin-entries-build-cache is a Vite plugin that provides a cache mechanism to filter the input package entry based on the verification of project file changes to speed up the multi-entry build process.

Note: This plugin is only applicable to multi-entry projects and only takes effect when build.emptyOutDir in the vite configuration needs to be false.

Installation

npm i vite-plugin-entries-build-cache -D
yarn add vite-plugin-entries-build-cache -D

Usage

Register it as a Vite plugin:

// vite.config.js
import entriesBuildCache from 'vite-plugin-entries-build-cache'
import { resolve } from 'path'

export default {
  plugins: [
    entriesBuildCache({
      entryRootPath: resolve(process.cwd(), './src/pages')
    })
  ]
}

Configuration

Parameter Name: Type Default Value Description
entryRootPath: string - The parent folder path of the multi-entry folder
rootPath?: string process.cwd() Project root path
exclude?: string[] [] Additional exclude files, .gitignore data will always be used
debug?: boolean false Whether to show debug logs

Attention: Correctly configuring the exclude parameter can effectively reduce the file diff time. Because the plugin is diffing all files under rootPath, not relying on git, so if there are a large number of files under rootPath and these files will not affect the entry files, such as package-lock.json, then these files can be configured to exclude to reduce diff time.

Example

Project Structure

├── src
│   ├── components
│   ├── pages
│   │   ├── index
│   │   │   ├── index.html
│   │   │   ├── main.ts
│   │   │   └── index.vue
│   └── └── about
│           ├── index.html
│           ├── main.ts
│           └── index.vue
├── .gitignore
├── vite.config.js
├── package-lock.json
└── package.json

Vite Config

// vite.config.js
import entriesBuildCache from 'vite-plugin-entries-build-cache'
import { defineConfig } from 'vite'
import { resolve } from 'path'

export default defineConfig({
  plugins: [
    entriesBuildCache({
      entryRootPath: resolve(process.cwd(), './src/pages'), // The parent folder path of the multi-entry folder. The plugin will filter the input entry based on the file changes under this path
      rootPath: process.cwd(), // Project root path
      exclude: ['.gitignore', 'package-lock.json']
    })
  ],
  build: {
    emptyOutDir: false, // This configuration needs to be false
    rollupOptions: {
      input: {
        index: resolve(process.cwd(), './src/pages/index/index.html'),
        about: resolve(process.cwd(), './src/pages/about/index.html')
      }
    }
  }
})

Modify Code

  1. When editing the file under entryRootPath that is ./src/pages, the filter mechanism will be triggered:
modified:   src/pages/about/index.html

When building, only the entry under the changed directory will be packaged, that is, the about entry, which is equivalent to:

input: {
  about: resolve(process.cwd(), './src/pages/about/index.html')
}
  1. When editing the file in rootPath, but not in entryRootPath, the filter mechanism will not be triggered:
modified:   vite.config.js

When building, all entry files will be packaged, because the plugin cannot determine which entry files will be affected by the changes:

input: {
  index: resolve(process.cwd(), './src/pages/index/index.html'),
  about: resolve(process.cwd(), './src/pages/about/index.html')
}

vite-plugin-entries-build-cache's People

Contributors

simmzl avatar

Stargazers

 avatar  avatar  avatar  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.