UNPKG

mixone

Version:

MixOne is a Node scaffolding tool implemented based on Vite, used for compiling HTML5, JavasCript, Vue, React and other codes. It supports packaging Web applications with multiple HTML entry points (BS architecture) and desktop installation packages (CS a

103 lines (96 loc) 2.88 kB
import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue2'; import path from 'path'; import fs from 'fs'; import copy from 'rollup-plugin-copy'; const args = process.argv.slice(2); const outDirArg = args.find(arg => arg.startsWith('--outDir=')); const output = outDirArg ? outDirArg.split('=')[1] : './dist'; // 读取 package.json 获取版本号 const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8')); const versionName = `v${packageJson.version}`; // 版本注入插件 function injectVersionPlugin () { return { name: 'inject-version', transformIndexHtml (html) { // 在 head 标签结束前插入版本号脚本 if (output.endsWith('web-dist')) { return html.replace( '</head>', ` <script>const versionName='${versionName}';</script>\n<script src="./browser-adap.js"></script>\n</head>` ); } return html.replace( '</head>', ` <script>const versionName='${versionName}';</script>\n</head>` ); }, }; } /** * 获取目录下所有HTML文件的绝对路径,并组织成对象 * @param {string} dirPath - 要扫描的目录路径,默认为当前目录 * @returns {Object} - 包含HTML文件路径的对象 */ function getHtmlFiles (dirPath = path.resolve(__dirname)) { // 确保目录存在 if (!fs.existsSync(dirPath)) { console.error(`目录不存在: ${dirPath}`); return {}; } // 读取目录中的所有文件 const files = fs.readdirSync(dirPath); // 过滤出HTML文件并创建结果对象 const result = {}; files.forEach((file) => { // 检查文件是否以.html结尾 if (file.endsWith('.html')) { // 获取文件的绝对路径 const absolutePath = path.resolve(dirPath, file); // 如果是index.html,键名为main,否则使用不带扩展名的文件名 if (file === 'index.html') { result.main = absolutePath; } else { // 去除.html扩展名 const fileName = file.slice(0, -5); // 移除'.html' result[fileName] = absolutePath; } } }); return result; } export default defineConfig({ base: './', plugins: [vue({ template: { compilerOptions: { isCustomElement: (tag) => tag === 'webview' } } }), injectVersionPlugin(), copy({ verbose: false, hook: 'closeBundle', targets: [ { src: 'browser-adap.js', dest: output } // 将文件从源复制到目标目录 ] })], build: { target: 'es2015', rollupOptions: { input: getHtmlFiles(), output: { dir: output }, }, terserOptions: { compress: { drop_console: true, drop_debugger: true, }, }, }, });