forked from ModelEarth/feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite-plugin-copy.js
37 lines (31 loc) · 1.28 KB
/
vite-plugin-copy.js
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
// vite-plugin-copy.js
import { writeFileSync, mkdirSync } from 'fs';
import path from 'path';
export default function copyAssets() {
return {
name: 'copy-assets',
generateBundle(outputOptions, bundle) {
const outDir = outputOptions.dir || 'dist';
for (const [key, value] of Object.entries(bundle)) {
if (value.type === 'asset' || value.type === 'chunk') {
const originalPath = path.join(outDir, value.fileName);
// Check if the file matches the pattern index-<hash>.js or index-<hash>.css
const isIndexJsFile = /^index-[a-f0-9]{8}\.js$/.test(value.fileName);
const isIndexCssFile = /^index-[a-f0-9]{8}\.css$/.test(value.fileName);
if (isIndexJsFile || isIndexCssFile) {
const fixedFileName = isIndexJsFile ? 'feedplayer.js' : 'feedplayer.css';
const fixedFilePath = path.join(outDir, fixedFileName);
try {
// Ensure directory exists
mkdirSync(path.dirname(fixedFilePath), { recursive: true });
// Copy file with fixed name
writeFileSync(fixedFilePath, value.source);
} catch (error) {
console.error(`Error copying asset: ${error}`);
}
}
}
}
},
};
}