UNPKG

vite-plugin-easy-player-pro

Version:
72 lines (71 loc) 2.18 kB
// src/index.ts import fs from "fs-extra"; import path from "path"; import serveStatic from "serve-static"; import { normalizePath } from "vite"; function vitePluginEasyPlayerPro(options = {}) { const { easyPlayerBuildRootPath = "node_modules/easy-player-pro/lib", easyPlayerBuildDir = "build" } = options; let EASY_PLAYER_PATH = "assets/easy-player-pro/"; let EASY_PLAYER_URL = EASY_PLAYER_PATH; let outDir = "dist"; let base = "/"; let isBuild = false; return { name: "vite-plugin-easy-player-pro", apply: () => true, config(c, { command }) { var _a; isBuild = command === "build"; if (c.base !== void 0) { base = c.base; if (base === "") base = "./"; } if ((_a = c.build) == null ? void 0 : _a.outDir) { outDir = c.build.outDir; } EASY_PLAYER_URL = path.posix.join(base, EASY_PLAYER_PATH); const userConfig = {}; if (!isBuild) { userConfig.define = { EASY_PLAYER_PATH: JSON.stringify(EASY_PLAYER_PATH), EASY_PLAYER_URL: JSON.stringify(EASY_PLAYER_URL) }; } return userConfig; }, configureServer({ middlewares }) { const easyPlayerPath = path.join(easyPlayerBuildRootPath, easyPlayerBuildDir); middlewares.use(path.posix.join("/", base, EASY_PLAYER_PATH), serveStatic(easyPlayerPath, { setHeaders: (res, path2, stat) => { res.setHeader("Access-Control-Allow-Origin", "*"); } })); }, async closeBundle() { if (isBuild) { try { await fs.copy(path.join(easyPlayerBuildRootPath, easyPlayerBuildDir), path.join(outDir, EASY_PLAYER_PATH)); } catch (err) { console.error("copy failed", err); } } }, transformIndexHtml() { const tags = []; tags.push({ tag: "script", attrs: { src: isBuild ? normalizePath(path.join(EASY_PLAYER_URL, "EasyPlayer-pro.js")) : normalizePath(path.join(easyPlayerBuildRootPath, easyPlayerBuildDir, "EasyPlayer-pro.js")) } }); return tags; } }; } export { vitePluginEasyPlayerPro as default };