zs
2025-05-16 0999b36321bac9e303b547b55c35b91d1546f1c4
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { defineConfig, loadEnv, createLogger } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'
import slash from 'slash'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin'
import VueTypeImports from 'vite-plugin-vue-type-imports'
import topLevelAwait from 'vite-plugin-top-level-await'
import svgIcon from 'vite-plugin-svgicon'
import VitePluginDevelopmentFilter from './script/plugins/vite-plugin-development-filter'
import { vitePluginForArco } from '@arco-plugins/vite-vue'
import { ChildProcess } from 'child_process'
const kill = require('kill-port')
const crossSpawn = require('cross-spawn')
 
const tag = 'information-debugger'
const prodTag = 'information-prod'
const isWin = process.platform === 'win32'
const appPath = slash(path.resolve(process.cwd(), 'app'))
 
const runGoServer = () => {
  const child: ChildProcess = crossSpawn(
    isWin
      ? '..\\node_modules\\lmes-create-widget\\www'
      : '../node_modules/lmes-create-widget/www',
    {
      stdio: 'inherit',
      cwd: appPath,
      shell: true,
      env: {
        ENV: 'production',
      },
    }
  )
  return child
}
/**
 * 杀死goLang服务,8800端口
 */
const killGoServer = async (port: number) => {
  try {
    console.log('检测到 Ctrl+C,进程即将退出...')
    await kill(port)
    process.exit(0)
  } catch (error) {
    process.exit(0)
  }
}
const run = () => {
  try {
    runGoServer()
    let lastPressTime = Date.now()
    process.on('SIGINT', async () => {
      const currentTime = Date.now()
      if (lastPressTime && currentTime - lastPressTime < 2000) {
        killGoServer(8800)
      } else {
        lastPressTime = Date.now()
      }
    })
    process.on('SIGTERM', () => {
      console.log('进程被 kill 终止...')
      killGoServer(8800)
    })
  } catch (error) {
    console.error(error)
  }
}
run()
// @ts-ignore
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, __dirname)
  console.info('[env]', env)
  const vueBaseConfig = {
    template: {
      compilerOptions: {
        isCustomElement: (id) => [tag].includes(id),
      },
    },
  }
  return {
    base: '/',
    define: {
      'process.env.VITE_STATIC_URL': '""',
    },
    plugins: [
      VitePluginDevelopmentFilter({ prodTag, tag }),
      vueJsx(vueBaseConfig.template.compilerOptions),
      vue({
        ...vueBaseConfig,
        // reactivityTransform: path.resolve(__dirname, 'src'),
      }),
      svgIcon({
        include: [slash(path.resolve('./src/assets/svg-icon/*.svg'))],
      }),
      VueTypeImports(),
 
      Components({
        include: [/\.vue$/, /\.vue\?vue/, /\.md$/, /\.tsx/, /\.jsx/],
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
      }),
      {
        name: 'fixHMR',
        handleHotUpdate({ modules, file }) {
          if (file.match(/\.(js|ts|css)$/)) return modules
 
          modules.map((m) => {
            m.importers = new Set()
          })
        },
      },
      // lazyImport({
      //   resolvers: [
      //     VxeResolver({
      //       libraryName: 'vxe-table',
      //     }),
      //   ],
      // }),
      optimizeLodashImports(),
      topLevelAwait({
        promiseExportName: '__tla',
        promiseImportName: (i) => `__tla_${i}`,
      }),
      vitePluginForArco({
        style: 'css',
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        components: path.resolve(__dirname, './src/components'),
        sdk: path.resolve(__dirname, 'src/cms/sdk.es.js'),
      },
    },
    server: {
      host: '0.0.0.0',
      headers: {
        'Access-Control-Allow-Origin': '*',
      },
      origin: `//localhost:${env.VITE_PORT}`,
      // open: true,
      port: env.VITE_PORT,
      cors: true,
      proxy: {
        // 使用本地多语言文件时开启
        '/api/v1/multiplelanguage/static': {
          target: 'http://localhost:8001/language/Common.en-US.json',
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(/^\/api\/v1\/multiplelanguage\/static/, ''),
        },
        '/api': {
          target: env.VITE_API_URL,
          changeOrigin: true,
        },
        '/flows': {
          target: env.VITE_API_URL,
          changeOrigin: true,
        },
        '/hubs': {
          target: env.VITE_API_URL,
          changeOrigin: true,
          ws: true,
        },
        '/resources': {
          target: env.VITE_STATIC_URL,
          changeOrigin: true,
          ws: true,
        },
        '/projectApi': {
          target: env.VITE_PROJECT_API_URL,
          changeOrigin: true,
          ws: true,
        },
      },
    },
    build: {
      outDir: 'wwwroot',
      minify: 'esbuild',
      sourcemap: true,
      chunkSizeWarningLimit: 1500,
      target: 'ES2022',
      rollupOptions: {
        output: {
          entryFileNames: `index.js`,
        },
      },
    },
    esbuild: {
      drop: mode !== 'development' ? ['debugger'] : [],
      keepNames: true,
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
            @use "@/assets/styles/element.scss" as *;
          `,
        },
        less: {
          modifyVars: {
            'arcoblue-6': '#f85959',
            '--primary-6': '#5a84ff',
          },
          javascriptEnabled: true,
        },
      },
    },
  }
})