'use strict'; const vite = require('vite'); require('picocolors'); const DEFAULT_EXTENSION = [".js", ".cjs", ".mjs", ".ts", ".tsx", ".jsx", ".vue"]; const PLUGIN_NAME = "vite:canyon"; function resolveFilename(id) { const [filename] = id.split("?vue"); return filename; } function shouldInstrument(filename) { return DEFAULT_EXTENSION.some((ext) => filename.endsWith(ext)); } function instrumentedData(args) { const canyon = { // gitlab流水线自带 projectID: args.projectID || process.env["CI_PROJECT_ID"] || "", commitSha: args.commitSha || process.env["CI_COMMIT_SHA"] || "", sha: args.commitSha || process.env["CI_COMMIT_SHA"] || "", branch: args.branch || process.env["CI_COMMIT_BRANCH"] || process.env["CI_COMMIT_REF_NAME"] || "", // 自己配置 dsn: args.dsn || process.env["DSN"] || "", reporter: args.reporter || process.env["REPORTER"] || "", // 可选 compareTarget: args.compareTarget, // 自动获取 instrumentCwd: args.instrumentCwd || process.cwd() }; return `(new Function("return this")()).__canyon__ = ${JSON.stringify(canyon)}`; } function canyonPlugin(opts = {}) { vite.createLogger("info", { prefix: "vite-plugin-canyon" }); const canyonStr = instrumentedData(opts); return { name: PLUGIN_NAME, enforce: "post", transform(srcCode, id, options) { const newCode = `${canyonStr} ${srcCode}`; const filename = resolveFilename(id); if (shouldInstrument(filename)) { return { code: newCode, map: null }; } } }; } module.exports = canyonPlugin;