Vite 中文文档 Vite 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • vite

    • 指引
    • 为什么选 Vite
    • 开始
    • 功能
    • 命令行界面
    • 使用插件
    • 依赖预构建
    • 静态资源处理
    • 构建生产版本
    • 部署静态站点
    • 环境变量和模式
    • 服务端渲染
    • 后端集成
    • 与其他工具比较
    • 故障排除
    • 从 v3 迁移
  • API

  • 配置参考

  • vite

  • API

  • 配置参考

Vite for WordPress


Vite integration for WordPress plugins and themes development.

Usage


Let's assume we have this plugin files structure:

  1. ``` sh
  2. my-plugin/
  3. ├ js/
  4. | └ src/
  5. |   └ main.ts
  6. ├ package.json
  7. ├ plugin.php
  8. └ vite.config.js

  9. ```

JavaScript


Add JS dependencies:

  1. ``` shell
  2. npm add -D vite @kucrut/vite-for-wp
  3. ```

Create vite.config.js :

  1. ``` js
  2. import create_config from '@kucrut/vite-for-wp';

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

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

  1. ``` js
  2. // vite.config.js
  3. import create_config from '@kucrut/vite-for-wp';

  4. export default create_config(
  5. {
  6.   main: 'js/src/main.ts',
  7.   extra: 'js/src/extra.ts',
  8. },
  9. 'js/dist',
  10. );
  11. ```

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

  1. ``` js
  2. // vite.config.js
  3. import { readFileSync } from 'node:fs';
  4. import { resolve } from 'node:path';
  5. import create_config from '@kucrut/vite-for-wp';
  6. import react from '@vitejs/plugin-react';

  7. export default create_config( 'js/src/main.ts', 'js/dist', {
  8. plugins: [ react() ],
  9. server: {
  10.   host: 'mydomain.com',
  11.   https: {
  12.    cert: readFileSync( 'path/to/cert.pem' ),
  13.    key: readFileSync( 'path/to/key.pem' ),
  14.   },
  15. },
  16. } );
  17. ```

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

  1. ``` json
  2. {
  3. "scripts": {
  4.   "build": "vite build",
  5.   "dev": "vite"
  6. }
  7. }
  8. ```

PHP


Add the composer dependency:

  1. ``` shell
  2. composer require kucrut/vite-for-wp
  3. ```

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

Enqueue the script:

  1. ``` php
  2. <?php

  3. use Kucrut\Vite;

  4. add_action( 'wp_enqueue_scripts', function (): void {
  5. Vite\enqueue_asset(
  6.   __DIR__ . 'js/dist',
  7.   'js/src/main.ts',
  8.   [
  9.    'handle' => 'my-script-handle',
  10.    'dependencies' => [ 'wp-components', 'some-registered-script-handle' ], // Optional script dependencies. Defaults to empty array.
  11.    'css-dependencies' => [ 'wp-components', 'some-registered-style-handle' ], // Optional style dependencies. Defaults to empty array.
  12.    'css-media' => 'all', // Optional.
  13.    'css-only' => false, // Optional. Set to true to only load style assets in production mode.
  14.    'in-footer' => true, // Optional. Defaults to false.
  15.   ]
  16. );
  17. } );
  18. ```

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.

  1. ``` shell
  2. npm add -D rollup-plugin-external-globals
  3. ```

  1. ``` js
  2. // vite.config.js
  3. import { wp_globals } from '@kucrut/vite-for-wp/utils';
  4. import create_config from '@kucrut/vite-for-wp';
  5. import external_globals from 'rollup-plugin-external-globals';

  6. export default create_config( 'js/src/main.ts', 'js/dist', {
  7. plugins: [
  8.   external_globals( {
  9.    ...wp_globals(),
  10.    'some-registered-script-handle': 'GlobalVar',
  11.   } ),
  12. ],
  13. } );
  14. ```

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

Example plugins


React: https://github.com/kucrut/vite-for-wp-example-react
Svelte: https://github.com/kucrut/catatan

Limitations


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

License


GPL v2
Last Updated: 2023-05-23 11:11:51