zs
2025-04-29 59233e9d4b8e161caa7084645b756f9f3400e68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import path from 'path'
import vue from '@vitejs/plugin-vue'
import { defineConfig } from 'vite'
import { buildPlugin } from 'vite-plugin-build'
import { globSync } from 'glob'
import { readFileSync, existsSync } from 'fs'
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import VitePluginWidgetProvider from './script/plugins/vite-plugin-widget-provider'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin'
import dayjs from 'dayjs'
import VueTypeImports from 'vite-plugin-vue-type-imports'
const execa = require('execa')
const isWin = process.platform === 'win32'
let getWidgetNames: Array<string> = []
const argvPath: string = './script/.argv'
const isAllBuild = existsSync(argvPath)
 
function getGitHash() {
  return execa('git', ['rev-parse', '--short', 'HEAD'])
}
 
function getGitBranch() {
  return execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'])
}
 
function getGitUserName() {
  return execa('git', ['config', 'user.name'])
}
 
if (isAllBuild) {
  const widgetName = readFileSync(argvPath, { encoding: 'utf8' })
  getWidgetNames.push(widgetName)
} else {
  const widgetsPath = globSync(`./src/widgets/*/index.ts`)
  getWidgetNames = widgetsPath.map((file) => {
    const parts = isWin
      ? path.resolve(file).split('\\')
      : path.resolve(file).split('/')
    return parts[parts.length - 2]
  })
}
 
const library: any = getWidgetNames.map((name) => {
  return {
    outDir: isWin ? 'D:/syc/CMS Editor/host/wwwroot/widgets' : 'dist',
    target: 'ES2022',
    rollupOptions: {
      external: ['vue', 'sdk'],
      output: {
        globals: {
          vue: 'Vue',
          sdk: 'sdk',
        },
      },
    },
    lib: {
      entry: path.join(__dirname, `./src/widgets/${name}/index.ts`),
      name: '__importWidgets',
      formats: ['umd'],
      fileName: () => {
        return isWin ? `${name}\\index.js` : `${name}/index.js`
      },
    },
  }
})
 
//@ts-ignore
export default defineConfig(async ({ mode }) => {
  let commit
  let branch
  let userName
  try {
    commit = await getGitHash()
    branch = await getGitBranch()
    userName = await getGitUserName()
  } catch (e) {
    console.log(e)
  }
 
  return {
    define: {
      'process.env': process.env,
      'window.__BUILD_TIME__': `"${dayjs().format('YYYY-MM-DD HH:mm:ss')}"`,
      'window.__COMMIT__': `"${commit?.stdout}"`,
      'window.__BRANCH__': `"${branch?.stdout}"`,
      'window.__USER_NAME__': `"${userName?.stdout}"`,
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        components: path.resolve(__dirname, './src/components'),
        sdk: path.resolve(__dirname, 'src/cms/sdk.es.js'),
      },
    },
    publicDir: false,
 
    plugins: [
      vueJsx(),
      vue({
        reactivityTransform: true,
      }),
      VueTypeImports(),
      VitePluginWidgetProvider(),
      cssInjectedByJsPlugin(),
      buildPlugin({
        fileBuild: false,
        libBuild: {
          buildOptions: library,
        },
      }),
      Components({
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/, /\.tsx/, /\.jsx/],
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
      }),
      optimizeLodashImports(),
    ],
    esbuild: {
      drop: mode !== 'development' ? ['debugger', 'console'] : [],
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/assets/styles/element.scss" as *;`,
        },
      },
    },
  }
})