-
Notifications
You must be signed in to change notification settings - Fork 8
/
custom-vite-plugins.js
51 lines (46 loc) · 1.68 KB
/
custom-vite-plugins.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import fs from 'fs';
const gallerySingleFile = process.env.gallerySingleFile;
const wallpaperSingleFile = process.env.wallpaperSingleFile;
const buildSingleFile = gallerySingleFile || wallpaperSingleFile;
export const customFilePathsJSON = {
name: 'Custom file paths json',
writeBundle(opts, bundle) {
if ( buildSingleFile ) return;
const files = [];
for (const [key, o] of Object.entries(bundle)) {
files.push( o.fileName );
}
const contents = 'window.chunksFilePaths = ' + JSON.stringify(files.sort(), null, 2) + ';';
fs.writeFileSync('./dist/file-paths.js', contents);
}
};
// Single file builds are done first and the regular build is done last. When a signle file build is
// generated, it's moved to the project root with a different name. And during the regular build,
// all single file html files are moved back in done last,
export const customSingleFileGallery = {
name: 'Custom single file gallery',
// Before build
generateBundle(ops, bundle) {
},
// After build
writeBundle(opts, bundle) {
// Preserving single file builds in the root folder until the entire build process finishes...
if ( buildSingleFile ) {
try {
fs.renameSync('./dist/gallery.html', './single-file-gallery.html');
} catch(e) {}
try {
fs.renameSync('./dist/animated-wallpaper.html', './single-file-animated-wallpaper.html');
} catch(e) {}
}
// Regular build → move single file builds back to the dist folder...
else {
try {
fs.renameSync('./single-file-gallery.html', './dist/single-file-gallery.html' );
} catch(e) {}
try {
fs.renameSync('./single-file-animated-wallpaper.html', './dist/single-file-animated-wallpaper.html' );
} catch(e) {}
}
}
};