fix
This commit is contained in:
@@ -3,6 +3,7 @@ import vue from '@vitejs/plugin-vue'
|
||||
import copy from "rollup-plugin-copy";
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as child_process from 'child_process';
|
||||
import Components from 'unplugin-vue-components/vite';
|
||||
import RSA from 'node-rsa';
|
||||
|
||||
@@ -63,9 +64,59 @@ function replaceKucTagsPlugin() {
|
||||
};
|
||||
}
|
||||
|
||||
// Function to build IIFE versions of desktop and mobile scripts
|
||||
function buildIIFEVersions() {
|
||||
return {
|
||||
name: 'iife-builder',
|
||||
buildStart() {
|
||||
console.log('Building IIFE versions...');
|
||||
|
||||
try {
|
||||
// Build desktop IIFE
|
||||
console.log('Building desktop IIFE...');
|
||||
child_process.execSync('npx vite build --config vite.desktop.config.ts', { stdio: 'inherit' });
|
||||
|
||||
// Build mobile IIFE
|
||||
console.log('Building mobile IIFE...');
|
||||
child_process.execSync('npx vite build --config vite.mobile.config.ts', { stdio: 'inherit' });
|
||||
|
||||
console.log('IIFE builds completed successfully.');
|
||||
} catch (error) {
|
||||
console.error('Error building IIFE versions:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
generateBundle(options, bundle) {
|
||||
// Copy and overwrite the main dist/src/js files with IIFE versions
|
||||
const desktopIIFEPath = path.resolve(__dirname, 'dist-temp/desktop/desktop.js');
|
||||
const mobileIIFEPath = path.resolve(__dirname, 'dist-temp/mobile/mobile.js');
|
||||
const destDir = path.resolve(__dirname, 'dist/src/js');
|
||||
|
||||
if (fs.existsSync(desktopIIFEPath)) {
|
||||
fs.mkdirSync(destDir, { recursive: true });
|
||||
fs.copyFileSync(desktopIIFEPath, path.join(destDir, 'desktop.js'));
|
||||
console.log('Copied desktop.js to dist/src/js/');
|
||||
}
|
||||
|
||||
if (fs.existsSync(mobileIIFEPath)) {
|
||||
fs.mkdirSync(destDir, { recursive: true });
|
||||
fs.copyFileSync(mobileIIFEPath, path.join(destDir, 'mobile.js'));
|
||||
console.log('Copied mobile.js to dist/src/js/');
|
||||
}
|
||||
|
||||
// Clean up dist-temp directory
|
||||
if (fs.existsSync(path.resolve(__dirname, 'dist-temp'))) {
|
||||
fs.rmSync(path.resolve(__dirname, 'dist-temp'), { recursive: true, force: true });
|
||||
console.log('Cleaned up dist-temp directory');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
buildIIFEVersions(),
|
||||
vue({
|
||||
template: {
|
||||
compilerOptions: {
|
||||
@@ -77,6 +128,7 @@ export default defineConfig({
|
||||
copy({
|
||||
targets: [
|
||||
{ src: 'dist/index.html', dest: 'dist/src/html', rename: 'config.html' },
|
||||
{ src: 'src/manifest.json', dest: 'dist/src' },
|
||||
{ src: 'src/assets/*.js', dest: 'dist/src/js' },
|
||||
{ src: 'src/assets/*.png', dest: 'dist/src/image' },
|
||||
{ src: 'src/css/*', dest: 'dist/src/css' },
|
||||
@@ -84,40 +136,6 @@ export default defineConfig({
|
||||
],
|
||||
hook: 'writeBundle' // 指定在何时复制文件
|
||||
}),
|
||||
{
|
||||
name: 'manifest-updater',
|
||||
writeBundle() {
|
||||
try {
|
||||
const manifestPath = path.resolve(__dirname, 'dist/src/manifest.json');
|
||||
const destDir = path.dirname(manifestPath);
|
||||
fs.mkdirSync(destDir, { recursive: true });
|
||||
fs.copyFileSync(path.resolve(__dirname, 'src/manifest.json'), manifestPath);
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
|
||||
const viteManifestPath = path.resolve(__dirname, 'dist/.vite/manifest.json');
|
||||
if (!fs.existsSync(viteManifestPath)) return;
|
||||
|
||||
const viteManifest = JSON.parse(fs.readFileSync(viteManifestPath, 'utf-8'));
|
||||
|
||||
// 收集所有生成的 chunk 文件
|
||||
const chunkFiles: string[] = [];
|
||||
Object.values(viteManifest).forEach((value: any) => {
|
||||
if (value.file && value.file.endsWith('.chunk.js')) {
|
||||
chunkFiles.push('js/' + path.basename(value.file));
|
||||
}
|
||||
});
|
||||
|
||||
// 更新 desktop 和 mobile 的 js 数组,包含 chunk 文件
|
||||
if (chunkFiles.length > 0) {
|
||||
manifest.desktop.js = ['js/kuc.min.js', 'js/desktop.js', ...chunkFiles];
|
||||
manifest.mobile.js = ['js/kuc.min.js', 'js/mobile.js', ...chunkFiles];
|
||||
}
|
||||
|
||||
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
|
||||
} catch (error) {
|
||||
console.error('Error updating manifest:', error);
|
||||
}
|
||||
}
|
||||
},
|
||||
replaceKucTagsPlugin()
|
||||
],
|
||||
resolve: {
|
||||
@@ -129,19 +147,15 @@ export default defineConfig({
|
||||
rollupOptions: {
|
||||
input: {
|
||||
config: path.resolve(__dirname, 'index.html'),
|
||||
desktop: path.resolve(__dirname, 'src/js/desktop.ts'),
|
||||
mobile: path.resolve(__dirname, 'src/js/mobile.ts'),
|
||||
},
|
||||
output: {
|
||||
entryFileNames: (chunkInfo) => {
|
||||
return 'src/js/[name].js'; // 默认处理为 JS 文件
|
||||
},
|
||||
chunkFileNames: 'src/js/[name]-[hash].chunk.js',
|
||||
assetFileNames: 'src/[ext]/[name].[ext]',
|
||||
},
|
||||
},
|
||||
sourcemap: 'inline',
|
||||
manifest: true,
|
||||
},
|
||||
define: {
|
||||
KUC_VERSION: JSON.stringify(dottedVersion),
|
||||
|
||||
Reference in New Issue
Block a user