From 6652b5edde278e9a8d31640473ff0e3e4a261268 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周六, 03 5月 2025 17:41:12 +0800
Subject: [PATCH] 22
---
HIAWms/web/script/plugins/vite-plugin-widget-provider.ts | 69 ++++++++++++++++++++++++++++++++++
1 files changed, 69 insertions(+), 0 deletions(-)
diff --git a/HIAWms/web/script/plugins/vite-plugin-widget-provider.ts b/HIAWms/web/script/plugins/vite-plugin-widget-provider.ts
new file mode 100644
index 0000000..88b58c7
--- /dev/null
+++ b/HIAWms/web/script/plugins/vite-plugin-widget-provider.ts
@@ -0,0 +1,69 @@
+import path from 'path'
+const isWin = process.platform === 'win32'
+const fileRegex = /\.(ts)$/
+const basePath = path.resolve(process.cwd(), './src/widgets')
+// @ts-ignore
+const filePath = isWin ? basePath.replaceAll('\\', '/') : basePath
+const regex = new RegExp(`${filePath}/([^/]*)/index.ts`)
+/**
+ * 鎻愬彇鍏抽敭瀛楃
+ * @param {*} code
+ * @returns
+ */
+const parseCode = (code) => {
+ const importRegion = code.match(/import[^]*?("|')(.*?)\1;/g)
+ const exportDefaultRegion = code.match(/export default [\s\S]*?;/)
+ const canvasView = code.match(/canvasView: ([^,]+),/)
+ const canvasViewValue = canvasView ? canvasView[1] : ''
+
+ const imports = importRegion ? importRegion.join('\n') : ''
+ const exportDefault = exportDefaultRegion ? exportDefaultRegion[0] : ''
+
+ return { imports, exportDefault, canvasView: canvasView[0], canvasViewValue }
+}
+
+/**
+ * 鍚堝苟澶勭悊浠g爜
+ * @param {*} param0
+ * @param {*} originCode
+ */
+const mergeCodeString = (
+ { imports, exportDefault, canvasView, canvasViewValue },
+ originCode
+) => {
+ const code = `${imports}\n${exportDefault}`
+ if (
+ canvasView.includes('provider(') &&
+ originCode.includes('provider/index')
+ ) {
+ return code
+ }
+
+ const providerCode = 'import { provider } from "@/provider/index";'
+
+ const exportDefaultCode = exportDefault.replace(
+ canvasView,
+ `canvasView: provider(${canvasViewValue}),`
+ )
+ return `${imports}\n${providerCode}\n${exportDefaultCode}`
+}
+
+export default function VitePluginWidgetProvider(): any {
+ return {
+ name: 'vite-plugin-widget-provider',
+ apply: 'build',
+
+ transform(code, id) {
+ if (fileRegex.test(id)) {
+ if (regex.test(id)) {
+ const codeData = parseCode(code)
+ const transformCode = mergeCodeString(codeData, code)
+ return {
+ code: transformCode,
+ map: null, // 濡傛灉鍙灏嗘彁渚� source map
+ }
+ }
+ }
+ },
+ }
+}
--
Gitblit v1.9.3