Merge pull request #3500 from SillyTavern/webpack-cache-dataroot Move webpack cache to data root

bb64e9b5c5975e0e0503d1c5c76acd52e8d40632

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
5 files changed, +86 -14Showing whitespace changes
docker/build-lib.js+1 -1
@@ -1,4 +1,4 @@
11import getWebpackServeMiddleware from '../src/middleware/webpack-serve.js';
22
33const middleware = getWebpackServeMiddleware();
44await middleware.runWebpackCompiler(true);
package-lock.json+35 -4
@@ -43,6 +43,7 @@
4343 "ip-matching": "^2.1.2",
4444 "ip-regex": "^5.0.0",
4545 "ipaddr.js": "^2.0.1",
46+ "is-docker": "^3.0.0",
4647 "jimp": "^0.22.10",
4748 "localforage": "^1.10.0",
4849 "lodash": "^4.17.21",
@@ -4649,15 +4650,15 @@
46494650 "license": "MIT"
46504651 },
46514652 "node_modules/is-docker": {
46524653 "version": "23.20.10",
46534654 "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-23.20.10.tgz",
46544655 "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQeljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==",
46554656 "license": "MIT",
46564657 "bin": {
46574658 "is-docker": "cli.js"
46584659 },
46594660 "engines": {
4660- "node": ">=8"
4661+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
46614662 },
46624663 "funding": {
46634664 "url": "https://github.com/sponsors/sindresorhus"
@@ -4734,6 +4735,21 @@
47344735 "node": ">=8"
47354736 }
47364737 },
4738+ "node_modules/is-wsl/node_modules/is-docker": {
4739+ "version": "2.2.1",
4740+ "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
4741+ "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
4742+ "license": "MIT",
4743+ "bin": {
4744+ "is-docker": "cli.js"
4745+ },
4746+ "engines": {
4747+ "node": ">=8"
4748+ },
4749+ "funding": {
4750+ "url": "https://github.com/sponsors/sindresorhus"
4751+ }
4752+ },
47374753 "node_modules/isarray": {
47384754 "version": "1.0.0",
47394755 "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
@@ -5518,6 +5534,21 @@
55185534 "url": "https://github.com/sponsors/sindresorhus"
55195535 }
55205536 },
5537+ "node_modules/open/node_modules/is-docker": {
5538+ "version": "2.2.1",
5539+ "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz",
5540+ "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==",
5541+ "license": "MIT",
5542+ "bin": {
5543+ "is-docker": "cli.js"
5544+ },
5545+ "engines": {
5546+ "node": ">=8"
5547+ },
5548+ "funding": {
5549+ "url": "https://github.com/sponsors/sindresorhus"
5550+ }
5551+ },
55215552 "node_modules/openai": {
55225553 "version": "4.17.4",
55235554 "resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz",
package.json+1 -0
@@ -33,6 +33,7 @@
3333 "ip-matching": "^2.1.2",
3434 "ip-regex": "^5.0.0",
3535 "ipaddr.js": "^2.0.1",
36+ "is-docker": "^3.0.0",
3637 "jimp": "^0.22.10",
3738 "localforage": "^1.10.0",
3839 "lodash": "^4.17.21",
src/middleware/webpack-serve.js+8 -5
@@ -1,11 +1,8 @@
11import path from 'node:path';
22import webpack from 'webpack';
33import { publicLibConfig }getPublicLibConfig from '../../webpack.config.js';
44
55export default function getWebpackServeMiddleware() {
6- const outputPath = publicLibConfig.output?.path;
7- const outputFile = publicLibConfig.output?.filename;
8-
96 /**
107 * A very spartan recreation of webpack-dev-middleware.
118 * @param {import('express').Request} req Request object.
@@ -14,6 +11,10 @@ export default function getWebpackServeMiddleware() {
1411 * @type {import('express').RequestHandler}
1512 */
1613 function devMiddleware(req, res, next) {
14+ const publicLibConfig = getPublicLibConfig();
15+ const outputPath = publicLibConfig.output?.path;
16+ const outputFile = publicLibConfig.output?.filename;
17+
1718 if (req.method === 'GET' && path.parse(req.path).base === outputFile) {
1819 return res.sendFile(outputFile, { root: outputPath });
1920 }
@@ -23,9 +24,11 @@ export default function getWebpackServeMiddleware() {
2324
2425 /**
2526 * Wait until Webpack is done compiling.
27+ * @param {boolean} [forceDist=false] Whether to force the use the /dist folder.
2628 * @returns {Promise<void>}
2729 */
2830 devMiddleware.runWebpackCompiler = (forceDist = false) => {
31+ const publicLibConfig = getPublicLibConfig(forceDist);
2932 const compiler = webpack(publicLibConfig);
3033
3134 return new Promise((resolve) => {
webpack.config.js+41 -4
@@ -1,13 +1,49 @@
11import process from 'node:process';
22import path from 'node:path';
3+import isDocker from 'is-docker';
34
4-/** @type {import('webpack').Configuration} */
5+/**
5-export const publicLibConfig = {
6+ * Get the Webpack configuration for the public/lib.js file.
7+ * 1. Docker has got cache and the output file pre-baked.
8+ * 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories.
9+ * @param {boolean} forceDist Whether to force the use the /dist folder.
10+ * @returns {import('webpack').Configuration}
11+ * @throws {Error} If the DATA_ROOT variable is not set.
12+ * */
13+export default function getPublicLibConfig(forceDist = false) {
14+ function getCacheDirectory() {
15+ if (forceDist || isDocker()) {
16+ return path.resolve(process.cwd(), 'dist/webpack');
17+ }
18+
19+ if (typeof globalThis.DATA_ROOT === 'string') {
20+ return path.resolve(globalThis.DATA_ROOT, '_webpack', 'cache');
21+ }
22+
23+ throw new Error('DATA_ROOT variable is not set.');
24+ }
25+
26+ function getOutputDirectory() {
27+ if (forceDist || isDocker()) {
28+ return path.resolve(process.cwd(), 'dist');
29+ }
30+
31+ if (typeof globalThis.DATA_ROOT === 'string') {
32+ return path.resolve(globalThis.DATA_ROOT, '_webpack', 'output');
33+ }
34+
35+ throw new Error('DATA_ROOT variable is not set.');
36+ }
37+
38+ const cacheDirectory = getCacheDirectory();
39+ const outputDirectory = getOutputDirectory();
40+
41+ return {
642 mode: 'production',
743 entry: './public/lib.js',
844 cache: {
945 type: 'filesystem',
10- cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'),
46+ cacheDirectory: cacheDirectory,
1147 store: 'pack',
1248 compression: 'gzip',
1349 },
@@ -28,8 +64,9 @@ export const publicLibConfig = {
2864 hints: false,
2965 },
3066 output: {
31- path: path.resolve(process.cwd(), 'dist'),
67+ path: outputDirectory,
3268 filename: 'lib.js',
3369 libraryTarget: 'module',
3470 },
3571 };
72+}