Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Preset for using Preact with the vite bundler

License

NotificationsYou must be signed in to change notification settings

preactjs/preset-vite

Repository files navigation

An all in one preset for writing Preact apps with thevite bundler.

Features:

  • Sets up Hot Module Replacement viaprefresh
  • EnablesPreact Devtools bridge during development
  • Aliases React topreact/compat

If you want to look into testing Preact with Vitest Browser modecheck out vitest-browser-preact

Installation

First intall the preset package from npm:

npm install --save-dev @preact/preset-vite# oryarn add -D @preact/preset-vite

Enhance your vite config with the Preact preset plugin in yourvite.config.ts orvite.config.js:

import{defineConfig}from"vite";importpreactfrom"@preact/preset-vite";exportdefaultdefineConfig({plugins:[preact()]});

Options

Options can be passed to our preset plugin via the first argument:

exportdefaultdefineConfig({plugins:[preact({devtoolsInProd:true})]});

Available options

OptionTypeDefaultDescription
devtoolsInProdbooleanfalseInject devtools bridge in production bundle instead of only in development mode
devToolsEnabledbooleantrueInject devtools bridge
prefreshEnabledbooleantrueInjectPrefresh for HMR
reactAliasesEnabledbooleantrueAliasesreact,react-dom topreact/compat
babelobjectSeeBabel configuration
prerenderobjectSeePrerendering configuration

Babel configuration

Thebabel option lets you add plugins, presets, andother configuration to the Babel transformation performed on each JSX/TSX file.

preact({babel:{presets:[...],// Your plugins run before any built-in transform (eg: Fast Refresh)plugins:[...],// Use .babelrc filesbabelrc:true,// Use babel.config.js filesconfigFile:true,}})

Prerendering configuration

OptionTypeDefaultDescription
enabledbooleanfalseEnables prerendering
renderTargetstring"body"Query selector for where to insert prerender result in your HTML template
prerenderScriptstringundefinedAbsolute path to script containing exportedprerender() function. If not provided, will try to find the prerender script in the scripts listed in your HTML entrypoint
additionalPrerenderRoutesstring[]undefinedPrerendering will crawl your site automatically, but you'd like to prerender some pages that may not be found (such as a/404 page), use this option to specify them
previewMiddlewareEnabledbooleanfalseVite's preview server as of v5 will not use our prerendered HTML documents automatically. This option enables a middleware that will correct this, allowing you to test the result of prerendering locally
previewMiddlewareFallbackstring/index.htmlFallback path to be used when an HTML document cannot be found via the preview middleware, e.g.,/404 or/not-found will be used when the user requests/some-path-that-does-not-exist

To prerender your app, you'll need to do these things:

  1. Enable prerendering in the plugin options
  2. Specify your render target, if you want the HTML to be inserted anywhere other than thedocument.body. This location likely should matchrender(), i.e.,render(<App />, document.querySelector('#app')) ->'#app'
  3. Create and export aprerender() function from a script. You could add this to your app entrypoint or create a completely separate file for it, either will work. See below for a usage example
  4. Specify where yourprerender() function is by either a) adding aprerender attribute to the script tag that contains it in your entry HTML (<script prerender src="./my-prerender-script.js">) or b) use theprerenderScript plugin option to specify the location with an absolute path

The plugin simply calls the prerender function you provide so it's up to you to determine how your app should be prerendered. You'll likely want to usepreact-render-to-string, or a wrapper around it such aspreact-iso'sprerender, but whatever you choose, the minimum you'll need to return is an object containing anhtml property with your HTML string which will then be inserted according to yourrenderTarget.

Yourprerender() function can be asynchronous, so feel free to make HTTP requests to retrieve data (fetch(...)), read files from disk (fs.readFile(...)), or similar things to set up your app.

For a full example implementation, see our demo

import{prerenderasssr}from'preact-iso';functionApp(){return<h1>Hello World!</h1>}exportasyncfunctionprerender(data){const{ html,links:discoveredLinks}=ssr(<App/>);return{        html,// Optionally add additional links that should be// prerendered (if they haven't already been)links:newSet([...discoveredLinks,'/foo','/bar']),// Optional data to serialize into a script tag for use on the client://   <script type="application/json">{"url":"/"}</script>data:{url:data.url},// Optionally configure and add elements to the `<head>` of// the prerendered HTML documenthead:{// Sets the "lang" attribute: `<html lang="en">`lang:'en',// Sets the title for the current page: `<title>My cool page</title>`title:'My cool page',// Sets any additional elements you want injected into the `<head>`://   <link rel="stylesheet" href="foo.css">//   <meta property="og:title" content="Social media title">elements:newSet([{type:'link',props:{rel:'stylesheet',href:'foo.css'}},{type:'meta',props:{property:'og:title',content:'Social media title'}}])}};}

License

MIT, seethe license file.

About

Preset for using Preact with the vite bundler

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

  •  

Contributors22


[8]ページ先頭

©2009-2025 Movatter.jp