定制 dietr/wp-vite-integration 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dietr/wp-vite-integration

最新稳定版本:v0.2.0

Composer 安装命令:

composer require dietr/wp-vite-integration

包简介

Add vite integration to your WordPress theme

README 文档

README

Handles Vite integration for WordPress theme development. Inspired by https://github.com/nystudio107/craft-vite.

Installation

Via composer as mu-plugin:

composer require dietr/wp-vite-integration

Add this to your composer.json:

{
    "require": {
        "dietr/wp-vite-integration": "^0.2.0"
    }
}

Then run:

composer install

functions.php

// Vite integration
add_action('after_setup_theme', function() {
  $GLOBALS['vite_integration']->setConfig([
      'env_file' => get_template_directory() . '/../../../.env',
      'manifest_path' => get_template_directory() . '/dist/.vite/manifest.json',
      'manifest_uri' => get_template_directory_uri() . '/dist',
      'entry_point' => '/resources/js/index.js',
      'vite_port' => '5173'
  ]);
});

Vite dev server toggle

Add this script in the root: viteDevServerToggle.js

/**
 * toggle USE_VITE_DEV_SERVER environment variable based on which npm script is running
 * */
import fs from 'fs';
import os from 'os';

export function setEnvValue(key, value) {
  // read file from hdd & split if from a linebreak to a array
  const ENV_VARS = fs.readFileSync("./.env", "utf8").split(os.EOL);

  // find the env we want based on the key
  const target = ENV_VARS.indexOf(ENV_VARS.find((line) => {
    return line.match(new RegExp(key));
  }));

  // replace the key/value with the new value
  ENV_VARS.splice(target, 1, `${key}=${value}`);

  // write everything back to the file system
  fs.writeFileSync("./.env", ENV_VARS.join(os.EOL));
}

if (process.env.NODE_ENV !== 'test') {  // Avoid running in test environment
    if (process.argv.length > 2) {
        const enable = process.argv[2];
        setEnvValue("USE_VITE_DEV_SERVER", enable);
    }
}

Setup your build and dev task in your package.json

  "build": "node viteDevServerToggle.js 0 && npx vite build",
  "dev": "node viteDevServerToggle.js 1 && npx vite",

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-12