22
schangxiang@126.com
2025-05-20 0ab314db5c1b0e87f671c382cc6509c13177ae0c
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
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'
const argvPath: string = './script/.argv'
const isSingleBuild = existsSync(argvPath)
const nodeIndex = process.env.NODE_INDEX
const baseBuildFile = './node_modules/.cache/widgets.json'
let getWidgetNames: Array<string> = []
 
if (isSingleBuild) {
  const widgetName = readFileSync(argvPath, { encoding: 'utf8' })
  getWidgetNames.push(widgetName)
} else {
  // 执行多核命令打包
  if (nodeIndex) {
    const widgetNameMap = require(path.resolve(process.cwd(), baseBuildFile))
    getWidgetNames = widgetNameMap[nodeIndex]
  }
}
 
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 }) => {
  return {
    define: {
      'process.env': process.env,
      'window.__BUILD_TIME__': `"${dayjs().format('YYYY-MM-DD HH:mm:ss')}"`,
    },
    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 *;`,
        },
      },
    },
  }
})