forked from SillyTavern/SillyTavern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
124 lines (111 loc) · 4.1 KB
/
Copy pathwebpack.config.js
File metadata and controls
124 lines (111 loc) · 4.1 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import process from 'node:process';
import path from 'node:path';
import fs from 'node:fs';
import crypto from 'node:crypto';
import isDocker from 'is-docker';
import webpack from 'webpack';
import { serverDirectory } from './src/server-directory.js';
import { getVersion, color } from './src/util.js';
/**
* Generate a cache version string based on the application version, Git revision, and Webpack version.
* @returns {string} The cache version string.
*/
function getWebpackCacheVersion() {
return crypto.createHash('shake256', { outputLength: 8 })
.update(JSON.stringify([appVersion.pkgVersion, appVersion.gitRevision, webpack.version]))
.digest('hex');
}
/**
* Prune old Webpack cache directories that do not match the current cache version.
* @param {string} webpackRoot The root directory where Webpack caches are stored.
* @param {string} currentCacheVersion The current cache version to keep.
*/
function pruneWebpackCache(webpackRoot, currentCacheVersion) {
try {
if (!fs.existsSync(webpackRoot)) {
return;
}
const cacheDirectories = fs.readdirSync(webpackRoot, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
for (const dir of cacheDirectories) {
const dirPath = path.join(webpackRoot, dir);
if (dir !== currentCacheVersion) {
try {
fs.rmSync(dirPath, { recursive: true, force: true });
console.debug(`Removed outdated cache directory: ${color.yellow(dir)}`);
} catch (error) {
console.error(`Failed to remove Webpack cache directory: ${color.red(dir)}`, error);
}
}
}
} catch (error) {
console.error('Failed to read Webpack cache directories for pruning.', error);
}
}
const appVersion = await getVersion();
/**
* Get the Webpack configuration for the public/lib.js file.
* 1. Docker has got cache and the output file pre-baked.
* 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories.
* @param {object} options Configuration options.
* @param {boolean} [options.forceDist=false] Whether to force the use the /dist folder.
* @param {boolean} [options.pruneCache=false] Whether to prune old cache directories.
* @returns {import('webpack').Configuration}
* @throws {Error} If the DATA_ROOT variable is not set.
* */
export default function getPublicLibConfig({ forceDist = false, pruneCache = false } = {}) {
function getWebpackRoot() {
if (forceDist || isDocker()) {
return path.resolve(process.cwd(), 'dist', '_webpack');
}
if (typeof globalThis.DATA_ROOT === 'string') {
return path.resolve(globalThis.DATA_ROOT, '_webpack');
}
throw new Error('DATA_ROOT variable is not set.');
}
function getCacheDirectory() {
return path.join(webpackRoot, cacheVersion, 'cache');
}
function getOutputDirectory() {
return path.join(webpackRoot, cacheVersion, 'output');
}
const webpackRoot = getWebpackRoot();
const cacheVersion = getWebpackCacheVersion();
const cacheDirectory = getCacheDirectory();
const outputDirectory = getOutputDirectory();
if (pruneCache) {
pruneWebpackCache(webpackRoot, cacheVersion);
}
return {
mode: 'production',
entry: path.join(serverDirectory, 'public/lib.js'),
cache: {
type: 'filesystem',
cacheDirectory: cacheDirectory,
store: 'pack',
compression: 'gzip',
},
devtool: false,
watch: false,
module: {},
stats: {
preset: 'minimal',
assets: false,
modules: false,
colors: true,
timings: true,
},
experiments: {
outputModule: true,
},
performance: {
hints: false,
},
output: {
path: outputDirectory,
filename: 'lib.js',
libraryTarget: 'module',
},
};
}