Git Product home page Git Product logo

vite-plugin-warmup's Introduction

vite-plugin-warmup

Warm up Vite's transform cache as soon as the server initializes.

Requires Vite >=4.3. Does not work with middlewareMode.

For Vite 5, use server.warmup instead.

Why

On-demand nature

Vite at it's core is an on-demand file server. When a request comes in, it will transform the file and serve it. This means we only do the work that is requested, keeping the dev server fast.

However, sometimes we know in advance which files will be requested when we start our development cycle. Instead of Vite idling before we open up the page, we can start transforming the files beforehand so when it gets requested, it's cached and can be served immediately.

Deep module graph

Take this module graph where the left file would import the right file:

foo.js -> bar.vue -> baz.js -> qux.json

The imported ids can only be known after the file is transformed, so if bar.vue takes some time to transform, baz.js has to wait for it's turn, and so on. This causes an internal waterfall the deeper the imports are.

By warming up baz.js, or any files that you know is hot path in your app, they'll be cached and can be served immediately.

Usage

Setup

Install vite-plugin-warmup:

npm install vite-plugin-warmup

Use the Vite plugin:

// vite.config.js
import { defineConfig } from 'vite'
import { warmup } from 'vite-plugin-warmup'

export default defineConfig({
  plugins: [
    warmup({
      // warm up the files and its imported JS modules recursively
      clientFiles: ['./**/*.html', './src/components/*.jsx']
    })
  ]
})

The plugin accepts clientFiles and ssrFiles options. As the name suggests, the files specified would be warmed up for the client and server transforms respectively.

The files can be direct file names or glob patterns using fast-glob.

NOTE: It's recommended to not turn off server.preTransformRequests in the Vite config, as that prevents transforming the module graph recursively.

Which files should I warm up?

If you're using Vite SPA with a index.html file, add that to the clientFiles option and you're good to go!

warmup({ clientFiles: ['./**/*.html'] })

You can also run DEBUG="vite:transform" vite to see the files that are being transformed. With vite-plugin-warmup, you should see these logs before you load the page.

Logs that appear after the page load are URLs that didn't get warmed up, if so, you can add more of them into the clientFiles option. Note that only actual files in the file system are supported.

vite:transform 28.72ms /@vite/client +1ms
vite:transform 62.95ms /src/components/BigComponent.jsx +1ms
warmup({ clientFiles: ['./**/*.html', './src/components/BigComponent.jsx'] })

If you're using SSR, you can use the ssrFiles option instead. As the Vite logs doesn't differentiate between client and server transforms, make sure the files added are safe to be transformed in the client or server respectively.

NOTE: Make sure to read the Why section to understand which files to add to not overload the Vite server on startup.

Metaframeworks

Some metaframeworks don't load the files through Vite directly, so this plugin might not work for them. To be sure which files are loaded by Vite, you can start the metaframework with the DEBUG="vite:transform" flag and follow the steps above.

vite-plugin-warmup also exports a warmupFile function you can use to warm up specific files (absolute paths only). If you need more control, you can reuse the warmupFile implementation in index.js.

Attribution

Sponsors

Sponsors

License

MIT

vite-plugin-warmup's People

Contributors

bluwy avatar dependabot[bot] avatar dreierf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

vite-plugin-warmup's Issues

Error [ERR_REQUIRE_ESM]: require()

When I added plugin to vite I get an error:

Error [ERR_REQUIRE_ESM]: require() of ES Module master/node_modules/vite-plugin-warmup/index.js from master/vite.config.ts not supported.
Instead change the require of index.js in master/vite.config.ts to a dynamic import() which is available in all CommonJS modules.

Infrastructure:
bin/vite present?: true
vite_ruby: 3.3.0
vite_rails: 3.0.14
rails: 7.0.4.2
node: v16.20.0
npm: 8.19.4
yarn: 1.22.19
pnpm:
ruby: ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]

installed packages:
master
├─┬ @modyfi/[email protected]
│ └── [email protected] deduped
├─┬ @vitejs/[email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected] deduped
├─┬ [email protected]
│ └── [email protected] deduped
└── [email protected]

配合unocss

会不会发生unocss样式没有生成,预热找不到

Thanks

Since the repository has no discussions enabled, I figured I create an issue for thanking you.

With your plugin I could improve the performance of my local styleguide quite a bit, to make switching HTML pages more snappy.

All that was necessary is to add the generated HTML files to the warmup.

Thanks a lot.

Have a nice weekend :)

create a new vue app by vite , dev cmd error

pnpm dev

failed to load config from /Users/sanchen/Common/my-vue-app/vite.config.ts
error when starting dev server:
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/sanchen/Common/my-vue-app/node_modules/.pnpm/[email protected][email protected]/node_modules/vite-plugin-warmup/index.js from /Users/sanchen/Common/my-vue-app/vite.config.ts not supported.
Instead change the require of index.js in /Users/sanchen/Common/my-vue-app/vite.config.ts to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/sanchen/Common/my-vue-app/vite.config.ts:33:44)
    at Object.require.extensions.<computed> [as .ts] (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/chunks/dep-85dbaaa7.js:68710:20)
    at loadConfigFromBundledFile (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/chunks/dep-85dbaaa7.js:68718:17)
    at loadConfigFromFile (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/chunks/dep-85dbaaa7.js:68637:32)
    at async resolveConfig (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/chunks/dep-85dbaaa7.js:68185:28)
    at async createServer (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/chunks/dep-85dbaaa7.js:66699:20)
    at async CAC.<anonymous> (/Users/sanchen/.config/yarn/global/node_modules/vite/dist/node/cli.js:687:24)
 ERROR  Command failed with exit code 1.

device: Mac mini 2018
system: macOS Ventura 13.2
pnpm: 6.14.6
node: v16.9.1

package.json

{
  "name": "my-vue-app",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "dev": "vite",
    "build": "run-p type-check build-only",
    "preview": "vite preview",
    "test:unit": "vitest",
    "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e'",
    "test:e2e:dev": "start-server-and-test 'vite dev --port 4173' http://localhost:4173 'cypress open --e2e'",
    "build-only": "vite build",
    "type-check": "vue-tsc --noEmit -p tsconfig.vitest.json --composite false",
    "lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
    "format": "prettier --write src/"
  },
  "dependencies": {
    "pinia": "^2.0.32",
    "vue": "^3.2.47",
    "vue-router": "^4.1.6"
  },
  "devDependencies": {
    "@rushstack/eslint-patch": "^1.2.0",
    "@types/jsdom": "^21.1.0",
    "@types/node": "^18.14.2",
    "@vitejs/plugin-vue": "^4.0.0",
    "@vitejs/plugin-vue-jsx": "^3.0.0",
    "@vue/eslint-config-prettier": "^7.1.0",
    "@vue/eslint-config-typescript": "^11.0.2",
    "@vue/test-utils": "^2.3.0",
    "@vue/tsconfig": "^0.1.3",
    "cypress": "^12.7.0",
    "eslint": "^8.34.0",
    "eslint-plugin-cypress": "^2.12.1",
    "eslint-plugin-vue": "^9.9.0",
    "jsdom": "^21.1.0",
    "npm-run-all": "^4.1.5",
    "prettier": "^2.8.4",
    "start-server-and-test": "^2.0.0",
    "typescript": "~4.8.4",
    "vite": "^4.3.1",
    "vite-plugin-warmup": "^0.0.3",
    "vitest": "^0.29.1",
    "vue-tsc": "^1.2.0"
  }
}

vite.config.ts

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { warmup } from 'vite-plugin-warmup'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    warmup({
      // warm up the files and its imported JS modules recursively
      clientFiles: ['./**/*.html', './src/components/*.jsx']
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})

Peer dependency does not include vite 5

When installing the plugin together with vite 5 pnpm complains, because the peer dependency range specified by the warmup plugin does not include vite 5. It seems to work fine though.

Could you please officially update the version range to include vite 5?

 WARN  Issues with peer dependencies found
.
└─┬ vite-plugin-warmup 0.1.0
  └── ✕ unmet peer vite@^4.3.1: found 5.0.0

Support middleware mode

When server: { middlewareMode: true } is set, server.httpServer is null, so this plugin no-ops.

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.