@tianqiubiao/vite-plugin-vue-intl
Version:
A vite plugin for Vue intl
2 lines (1 loc) • 7.14 kB
JavaScript
import { fileURLToPath as O } from "node:url"; import { dirname as x, resolve as m } from "node:path"; import N from "sirv"; import U from "vite-plugin-inspect"; import A from "express"; import B from "body-parser"; import p from "fs-extra"; import "merge"; import { build as D } from "vite"; import J from "axios"; import q from "md5"; import F from "@alicloud/pop-core"; const V = typeof __dirname < "u" ? __dirname : x(O(import.meta.url)), y = m(V, "./data.json"); let c = {}; S(); function S() { try { c = p.readJsonSync(y) } catch { p.outputJSONSync(y, {}) } } let k = null; function K() { clearTimeout(k), setTimeout(() => { p.outputJSONSync(y, c), S() }, 500) } const d = { update(t = {}) { return Object.keys(t).forEach(e => { c[e] ? c[e].push(...t[e]) : c[e] = t[e] }), K(), c }, remove() { }, clear() { c = {}, p.writeJsonSync(y, {}) }, data() { return c }, translate(t, e) { } }; function M(t) { const e = [], n = t.node.object, o = t.node.property; if (n?.name === "Intl" && o?.name === "get") { const r = t.parent.arguments[0]?.value; if (t.parentPath?.parent?.property?.name === "d") { const a = t.parentPath.parentPath.parent.arguments[0]?.value; e.push({ code: r, value: a }) } } return e } function z(t) { return Object.prototype.toString.call(t) === "[object Object]" || Object.prototype.toString.call(t) === "[object Array]" } function C(t, e) { const n = []; return Object.keys(t.node).forEach(o => { const r = t.node[o]; z(r) && (r.type === "MemberExpression" && n.push(...e({ node: r, parentPath: t, parent: t.node })), n.push(...C({ node: r, parentPath: t, parent: t.node }, e))) }), n } function G(t = {}) { return Promise.resolve(C({ node: t, parentPath: {}, parent: {} }, M)) } const g = process.cwd(), H = m(g, "./src"), E = m(g, "./vite.config.js"), L = m(g, "./vite.config.ts"); let i = { defaultLang: "zh_CN" }; i.projectPath = H, p.pathExistsSync(E) && (i.configPath = E), p.pathExistsSync(L) && (i.configPath = L); function W(t = {}) { Object.assign(i, t) } async function Y() { return d.clear(), await D({ root: g, base: "/", logLevel: "none", build: { outDir: "./node_modules/@tianqiubiao/vite-plugin-vue-intl/vituredist" }, plugins: [{ name: "vite-plugin-vue-intl-transform", apply: "build", moduleParsed({ id: t, ast: e }) { if (t.includes(i.projectPath) && e.type === "Program") try { console.log(`\u5904\u7406\u6587\u4EF6 ${t.replace(g, "")}`), G(e).then(n => { const [o, r] = t.replace(i.projectPath, "").split("?"); d.update({ [o]: n }) }) } catch (n) { console.log(n) } } }] }), Promise.resolve(d.data()) } function Q(t, e) { return console.log(i), Y() } function h() { return i } function X(t) { const e = h(); return e.syncText ? Z(t, n => e.syncText(n)) : Promise.resolve({}) } function Z(t, e, n = 10) { const o = []; let r = []; return t.forEach((a, s) => { s % n === 0 && s && (o.push(r), r = []), r.push(a) }), o.push(r), j(o, e) } function j(t = [], e, n = [], o = 0) { if (o < t.length) { const r = t[o]; return e(r).then(a => (n.push(a), j(t, e, n, o + 1))) } return Promise.resolve(n) } let $; function tt(t) { const e = h(), { appid: n, secret: o } = e.translate.baidu, r = "abcd"; return J({ url: "https://fanyi-api.baidu.com/api/trans/vip/translate", method: "get", headers: { "Content-Type": "application/x-www-form-urlencoded" }, params: { q: t.text, from: t.target || "auto", appid: n, salt: r, to: t.lang || "en", sign: q(`${n}${t.text}${r}${o}`) } }).then(a => { const { data: s = {} } = a; return (s.trans_result || []).length ? { source: s.trans_result[0].src, text: s.trans_result[0].dst, lang: t.lang || "en" } : { source: t.text, text: "", lang: t.lang || "en" } }) } function et(t) { return at({ FormatType: "text", SourceLanguage: t.target || "auto", TargetLanguage: t.lang || "en", SourceText: t.text, Scene: "title" }).then(e => e.Code == 200 ? { source: t.text, text: e.Data.Translated, lang: t.lang || "en" } : {}) } function nt() { const t = h(); return Promise.resolve({ defaultLang: t.defaultLang, langs: t.langs || [], translate: t.translate || {} }) } const ot = { baidu: tt, ali: et }; function rt(t) { const e = h(); if (!e.translate) return Promise.resolve({}); const n = Object.keys(e.translate); if (!n.length) return Promise.resolve({}); let o = n[0]; if (n.length > 1) { const r = n.find(a => e.translate[a].use); r && (o = r) } return ot[o](t) } function at(t) { try { if ($) return $.request("Translate", t, { method: "POST", formatParams: !1 }); const e = h(), { appid: n, secret: o } = e.translate.ali; return $ = new F({ accessKeyId: n, accessKeySecret: o, endpoint: "https://mt.cn-hangzhou.aliyuncs.com", apiVersion: "2018-10-12" }), $.request("Translate", t, { method: "POST", formatParams: !1 }) } catch (e) { return console.log(e), Promise.resolve({ Code: 400 }) } } const _ = A.Router(); _.use(B.json()); function st(t) { t.setHeader("Content-Type", "application/json;charset=UTF-8") } function l(t, e, n = "post") { _[n](t, (o, r) => { st(r), e(o, a => { r.end(JSON.stringify(a)) }, r) }) } l("/updata", ({ body: t }, e) => { e(d.update(t)) }), l("/clear", ({ body: t }, e) => { e(d.clear(t)) }), l("/transform", ({ body: t }, e) => { e(d.clear(t)) }), l("/findAll", ({ body: t }, e) => { Q(t.path).then(n => { e(n) }) }), l("/i18n", ({ body: t }, e) => { rt(t).then(n => { e(n) }) }), l("/langs", ({ body: t }, e) => { nt().then(n => { e(n) }) }), l("/syncText", ({ body: t }, e) => { X(t).then(n => { e(n) }) }); function ct(t, e, n = {}) { W(n), t.middlewares.use(e, _) } let u = !0; const f = typeof self < "u" ? self : typeof window < "u" ? window : typeof global < "u" ? global : {}; let v = 0; if (f.process && f.process.env && f.process.stdout) { const { FORCE_COLOR: t, NODE_DISABLE_COLORS: e, NO_COLOR: n, TERM: o, COLORTERM: r } = f.process.env; e || n || t === "0" ? u = !1 : t === "1" || t === "2" || t === "3" ? u = !0 : o === "dumb" ? u = !1 : "CI" in f.process.env && ["TRAVIS", "CIRCLECI", "APPVEYOR", "GITLAB_CI", "GITHUB_ACTIONS", "BUILDKITE", "DRONE"].some(a => a in f.process.env) ? u = !0 : u = process.stdout.isTTY, u && (process.platform === "win32" || r && (r === "truecolor" || r === "24bit") ? v = 3 : o && (o.endsWith("-256color") || o.endsWith("256")) ? v = 2 : v = 1) } let w = { enabled: u, supportLevel: v }; function P(t, e, n = 1) { const o = `\x1B[${t}m`, r = `\x1B[${e}m`, a = new RegExp(`\\x1b\\[${e}m`, "g"); return s => w.enabled && w.supportLevel >= n ? o + ("" + s).replace(a, o) + r : "" + s } const I = P(1, 22), it = P(32, 39), lt = P(36, 39), ut = typeof __dirname < "u" ? __dirname : x(O(import.meta.url)), pt = m(ut, "./client"); function dt(t) { const e = U({ silent: !0 }); function n(o) { const r = o.config.base || "/"; ct(o, `${r}__intl__`, t), o.middlewares.use(`${r}__intl__`, N(pt, { single: !0, dev: !0 })); const a = o.printUrls, s = b => lt(b.replace(/:(\d+)\//, (T, R) => `:${I(R)}/`)); o.printUrls = () => { const b = o.resolvedUrls; a(); for (const T of b?.local) console.log(` ${it("\u279C")} ${I("Vue Intl")}: ${s(`${T}__intl__/`)}`) } } return [e, { name: "vite-plugin-vue-intl", apply: "serve", configResolved(o) { }, configureServer(o) { n(o) } }] } export { dt as default };