You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否让Vite在开发模式下复制并转换Office.js插件模板文件?

如何在Vite开发模式下完成Office.js插件manifest模板的复制与变量替换

当然可以实现。你遇到的问题是因为多数静态资源处理插件默认只在生产构建阶段触发磁盘写入操作,而Vite开发模式默认基于内存运行,不会主动输出文件到磁盘。下面提供两种可靠的解决方案:

方案1:自定义Vite插件(集成开发流程,支持文件监听)

直接利用Vite的configureServer钩子,在开发服务器启动前处理模板文件,还可以监听模板变化自动重新生成目标文件。

在vite.config.js中添加以下代码:

import { defineConfig } from 'vite';
import fs from 'fs/promises';
import path from 'path';

// 定义需要替换的变量,可根据需求从环境变量或配置文件读取
const templateVariables = {
  APP_DISPLAY_NAME: '我的Office插件',
  APP_ID: 'your-unique-app-id',
  // 添加其他需要替换的变量
};

export default defineConfig({
  plugins: [
    {
      name: 'office-manifest-processor',
      async configureServer(server) {
        const templatePath = path.resolve(__dirname, 'manifest_template.xml');
        const targetPath = path.resolve(__dirname, 'template.xml');

        // 定义模板处理逻辑
        const processTemplate = async () => {
          try {
            // 读取模板文件内容
            let content = await fs.readFile(templatePath, 'utf8');
            // 替换所有变量(假设模板中变量格式为{{VAR_NAME}})
            Object.entries(templateVariables).forEach(([key, value]) => {
              const regex = new RegExp(`{{${key}}}`, 'g');
              content = content.replace(regex, value);
            });
            // 写入目标文件
            await fs.writeFile(targetPath, content, 'utf8');
            console.log('✅ Manifest模板已生成:template.xml');
          } catch (error) {
            console.error('❌ 处理Manifest模板失败:', error);
          }
        };

        // 服务器启动前执行一次模板处理
        await processTemplate();

        // 监听模板文件变化,自动重新处理
        server.watcher.add(templatePath);
        server.watcher.on('change', async (file) => {
          if (file === templatePath) {
            await processTemplate();
          }
        });
      }
    }
  ]
});

这个插件会在npm run dev启动开发服务器前自动处理模板,并且当manifest_template.xml修改时,会实时重新生成template.xml。

方案2:前置脚本(简单直接,无监听)

如果不需要监听模板文件变化,只需要在启动开发服务器前执行一次模板处理,可以直接在package.json的脚本中添加前置处理步骤。

  1. 创建一个单独的处理脚本process-manifest.js:
const fs = require('fs/promises');
const path = require('path');

const templateVariables = {
  APP_DISPLAY_NAME: '我的Office插件',
  APP_ID: 'your-unique-app-id',
};

async function main() {
  const templatePath = path.resolve(__dirname, 'manifest_template.xml');
  const targetPath = path.resolve(__dirname, 'template.xml');

  try {
    let content = await fs.readFile(templatePath, 'utf8');
    Object.entries(templateVariables).forEach(([key, value]) => {
      const regex = new RegExp(`{{${key}}}`, 'g');
      content = content.replace(regex, value);
    });
    await fs.writeFile(targetPath, content, 'utf8');
    console.log('Manifest模板处理完成');
  } catch (error) {
    console.error('处理模板失败:', error);
    process.exit(1); // 处理失败时终止后续Vite启动
  }
}

main();
  1. 修改package.json的dev脚本:
{
  "scripts": {
    "dev": "node process-manifest.js && vite",
    "build": "vite build"
  }
}

这样每次执行npm run dev时,会先运行脚本处理模板,再启动Vite开发服务器。

内容的提问来源于stack exchange,提问作者dotNET

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 10:07:06