Merge pull request #3500 from SillyTavern/webpack-cache-dataroot

Move webpack cache to data root
This commit is contained in:
Cohee
2025-02-19 21:53:58 +02:00
committed by GitHub
5 changed files with 114 additions and 42 deletions

View File

@@ -1,4 +1,4 @@
import getWebpackServeMiddleware from '../src/middleware/webpack-serve.js'; import getWebpackServeMiddleware from '../src/middleware/webpack-serve.js';
const middleware = getWebpackServeMiddleware(); const middleware = getWebpackServeMiddleware();
await middleware.runWebpackCompiler(); await middleware.runWebpackCompiler(true);

39
package-lock.json generated
View File

@@ -43,6 +43,7 @@
"ip-matching": "^2.1.2", "ip-matching": "^2.1.2",
"ip-regex": "^5.0.0", "ip-regex": "^5.0.0",
"ipaddr.js": "^2.0.1", "ipaddr.js": "^2.0.1",
"is-docker": "^3.0.0",
"jimp": "^0.22.10", "jimp": "^0.22.10",
"localforage": "^1.10.0", "localforage": "^1.10.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
@@ -4649,15 +4650,15 @@
"license": "MIT" "license": "MIT"
}, },
"node_modules/is-docker": { "node_modules/is-docker": {
"version": "2.2.1", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz", "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-3.0.0.tgz",
"integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==", "integrity": "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==",
"license": "MIT", "license": "MIT",
"bin": { "bin": {
"is-docker": "cli.js" "is-docker": "cli.js"
}, },
"engines": { "engines": {
"node": ">=8" "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
}, },
"funding": { "funding": {
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
@@ -4734,6 +4735,21 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/is-wsl/node_modules/is-docker": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
"integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
"license": "MIT",
"bin": {
"is-docker": "cli.js"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/isarray": { "node_modules/isarray": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
@@ -5518,6 +5534,21 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/open/node_modules/is-docker": {
"version": "2.2.1",
"resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
"integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
"license": "MIT",
"bin": {
"is-docker": "cli.js"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/openai": { "node_modules/openai": {
"version": "4.17.4", "version": "4.17.4",
"resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz", "resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz",

View File

@@ -33,6 +33,7 @@
"ip-matching": "^2.1.2", "ip-matching": "^2.1.2",
"ip-regex": "^5.0.0", "ip-regex": "^5.0.0",
"ipaddr.js": "^2.0.1", "ipaddr.js": "^2.0.1",
"is-docker": "^3.0.0",
"jimp": "^0.22.10", "jimp": "^0.22.10",
"localforage": "^1.10.0", "localforage": "^1.10.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",

View File

@@ -1,11 +1,8 @@
import path from 'node:path'; import path from 'node:path';
import webpack from 'webpack'; import webpack from 'webpack';
import { publicLibConfig } from '../../webpack.config.js'; import getPublicLibConfig from '../../webpack.config.js';
export default function getWebpackServeMiddleware() { export default function getWebpackServeMiddleware() {
const outputPath = publicLibConfig.output?.path;
const outputFile = publicLibConfig.output?.filename;
/** /**
* A very spartan recreation of webpack-dev-middleware. * A very spartan recreation of webpack-dev-middleware.
* @param {import('express').Request} req Request object. * @param {import('express').Request} req Request object.
@@ -14,6 +11,10 @@ export default function getWebpackServeMiddleware() {
* @type {import('express').RequestHandler} * @type {import('express').RequestHandler}
*/ */
function devMiddleware(req, res, next) { function devMiddleware(req, res, next) {
const publicLibConfig = getPublicLibConfig();
const outputPath = publicLibConfig.output?.path;
const outputFile = publicLibConfig.output?.filename;
if (req.method === 'GET' && path.parse(req.path).base === outputFile) { if (req.method === 'GET' && path.parse(req.path).base === outputFile) {
return res.sendFile(outputFile, { root: outputPath }); return res.sendFile(outputFile, { root: outputPath });
} }
@@ -23,9 +24,11 @@ export default function getWebpackServeMiddleware() {
/** /**
* Wait until Webpack is done compiling. * Wait until Webpack is done compiling.
* @param {boolean} [forceDist=false] Whether to force the use the /dist folder.
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
devMiddleware.runWebpackCompiler = () => { devMiddleware.runWebpackCompiler = (forceDist = false) => {
const publicLibConfig = getPublicLibConfig(forceDist);
const compiler = webpack(publicLibConfig); const compiler = webpack(publicLibConfig);
return new Promise((resolve) => { return new Promise((resolve) => {

View File

@@ -1,35 +1,72 @@
import process from 'node:process'; import process from 'node:process';
import path from 'node:path'; import path from 'node:path';
import isDocker from 'is-docker';
/** @type {import('webpack').Configuration} */ /**
export const publicLibConfig = { * Get the Webpack configuration for the public/lib.js file.
mode: 'production', * 1. Docker has got cache and the output file pre-baked.
entry: './public/lib.js', * 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories.
cache: { * @param {boolean} forceDist Whether to force the use the /dist folder.
type: 'filesystem', * @returns {import('webpack').Configuration}
cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'), * @throws {Error} If the DATA_ROOT variable is not set.
store: 'pack', * */
compression: 'gzip', export default function getPublicLibConfig(forceDist = false) {
}, function getCacheDirectory() {
devtool: false, if (forceDist || isDocker()) {
watch: false, return path.resolve(process.cwd(), 'dist/webpack');
module: {}, }
stats: {
preset: 'minimal', if (typeof globalThis.DATA_ROOT === 'string') {
assets: false, return path.resolve(globalThis.DATA_ROOT, '_webpack', 'cache');
modules: false, }
colors: true,
timings: true, throw new Error('DATA_ROOT variable is not set.');
}, }
experiments: {
outputModule: true, function getOutputDirectory() {
}, if (forceDist || isDocker()) {
performance: { return path.resolve(process.cwd(), 'dist');
hints: false, }
},
output: { if (typeof globalThis.DATA_ROOT === 'string') {
path: path.resolve(process.cwd(), 'dist'), return path.resolve(globalThis.DATA_ROOT, '_webpack', 'output');
filename: 'lib.js', }
libraryTarget: 'module',
}, throw new Error('DATA_ROOT variable is not set.');
}; }
const cacheDirectory = getCacheDirectory();
const outputDirectory = getOutputDirectory();
return {
mode: 'production',
entry: './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',
},
};
}