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 *;`,
|
},
|
},
|
},
|
}
|
})
|