• Stars
    star
    100
  • Rank 340,703 (Top 7 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vite integration for WordPress plugins and themes development.

Vite for WordPress

Vite integration for WordPress plugins and themes development.

Usage

Let's assume we have this plugin files structure:

my-plugin/
├ js/
| â”” src/
|   â”” main.ts
├ package.json
├ plugin.php
â”” vite.config.js

JavaScript

Add JS dependencies:

npm add -D vite @kucrut/vite-for-wp

Create vite.config.js:

import create_config from '@kucrut/vite-for-wp';

export default create_config( 'js/src/main.ts', 'js/dist' );

If you have multiple entrypoints to build, pass an object as the first parameter:

// vite.config.js
import create_config from '@kucrut/vite-for-wp';

export default create_config(
	{
		main: 'js/src/main.ts',
		extra: 'js/src/extra.ts',
	},
	'js/dist',
);

Pass a configuration object as the third parameter if you need to add plugins, use https, etc:

// vite.config.js
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import create_config from '@kucrut/vite-for-wp';
import react from '@vitejs/plugin-react';

export default create_config( 'js/src/main.ts', 'js/dist', {
	plugins: [ react() ],
	server: {
		host: 'mydomain.com',
		https: {
			cert: readFileSync( 'path/to/cert.pem' ),
			key: readFileSync( 'path/to/key.pem' ),
		},
	},
} );

Lastly, add dev and build scripts to your package.json:

{
	"scripts": {
		"build": "vite build",
		"dev": "vite"
	}
}

PHP

Add the composer dependency:

composer require kucrut/vite-for-wp

If your plugin/theme doesn't use composer, feel free to copy the main file and require it.

Enqueue the script:

<?php

use Kucrut\Vite;

add_action( 'wp_enqueue_scripts', function (): void {
	Vite\enqueue_asset(
		__DIR__ . 'js/dist',
		'js/src/main.ts',
		[
			'handle' => 'my-script-handle',
			'dependencies' => [ 'wp-components', 'some-registered-script-handle' ], // Optional script dependencies. Defaults to empty array.
			'css-dependencies' => [ 'wp-components', 'some-registered-style-handle' ], // Optional style dependencies. Defaults to empty array.
			'css-media' => 'all', // Optional.
			'css-only' => false, // Optional. Set to true to only load style assets in production mode.
			'in-footer' => true, // Optional. Defaults to false.
		]
	);
} );

Note that each entrypoint needs to be enqueued separately, ie. if you have multiple entrypoints, you'll need to call Vite\enqueue_asset() for each and every one of them.

To only register the asset, use Vite\register_asset(). It accepts same parameters as Vite\enqueue_asset() and returns an array of scripts and styles handles that you can enqueue later using wp_enqueue_script() and wp_enqueue_style(). Please note that style assets are only registered in production mode because in development mode, they will be automatically loaded by Vite.

You can now run npm run dev when developing your plugin/theme and run npm run build to build the production assets.

Notes

External Dependencies

If your JS package depends on one or more WordPress modules (eg. @wordpress/i18n), you can define them as externals with the help of rollup-plugin-external-globals.

npm add -D rollup-plugin-external-globals
// vite.config.js
import { wp_globals } from '@kucrut/vite-for-wp/utils';
import create_config from '@kucrut/vite-for-wp';
import external_globals from 'rollup-plugin-external-globals';

export default create_config( 'js/src/main.ts', 'js/dist', {
	plugins: [
		external_globals( {
			...wp_globals(),
			'some-registered-script-handle': 'GlobalVar',
		} ),
	],
} );

Note that you will need to add them to the dependencies array when enqueueing the script (see example above).

Example plugins

Limitations

Currently, this package doesn't provide HMR support for building editor blocks yet.

License

GPL v2

More Repositories

1

ttrss-reader

Tiny Tiny RSS Reader
JavaScript
50
star
2

minnie

React-based WordPress Client
JavaScript
17
star
3

kc-settings

Easily create plugin/theme settings page, custom fields metaboxes, term meta and user meta settings.
JavaScript
15
star
4

catatan

Simple post editor for WordPress
Svelte
8
star
5

wp-theme-polos

A minimalist black on white WordPress theme
PHP
7
star
6

mu-plugins

Small but important plugins for WordPress sites
PHP
7
star
7

wp-bridge

Extra WordPress REST API Endpoints and Functionalities
PHP
6
star
8

kc-multilingual

Make WordPress speak your language... the easy way!
PHP
6
star
9

vite-for-wp-example-react

A plugin to demonstrate Vite for WP integration (React)
JavaScript
6
star
10

wp-widget-attributes

Add custom attributes (classes and ID) to your widgets, the easy way!
PHP
5
star
11

docker-deluge

Deluge on Alpine Linux
Shell
4
star
12

vite-for-wp-example-vanilla-js

A plugin to demonstrate Vite for WP integration (Vanilla JS)
PHP
2
star
13

kc-media-enhancements

Enhance WordPress media/attachment management
PHP
2
star
14

Tempelan

Simple and clean minisite template
JavaScript
2
star
15

wp-bridge-post-formats

Expose Post Formats in WP REST API
PHP
2
star
16

kc-cbc

Filter contents based on visitor's country by using custom taxonomy.
PHP
2
star
17

wp-settings-meta-boxes

A helper class to easily create custom meta boxes on plugin/theme settings page.
PHP
2
star
18

wp-bridge-menus

WP REST API endpoint for menus
PHP
1
star
19

site-stack-scripts

Composer scripts for site stacks
PHP
1
star
20

kc-pta-filter

Filter post type archive index by taxonomy term
PHP
1
star
21

wp-theme-child

Simple WordPress child theme for development purposes
PHP
1
star
22

kc-paginator

A continuation of the Paginator plugin originally developed by Dhuz.
PHP
1
star