Merge pull request #3500 from SillyTavern/webpack-cache-dataroot Move webpack cache to data root
Signed| @@ -1,4 +1,4 @@ | |||
| 1 | import getWebpackServeMiddleware from '../src/middleware/webpack-serve.js'; | 1 | import getWebpackServeMiddleware from '../src/middleware/webpack-serve.js'; |
| 2 | 2 | ||
| 3 | const middleware = getWebpackServeMiddleware(); | 3 | const middleware = getWebpackServeMiddleware(); |
| 4 | await middleware.runWebpackCompiler(); | 4 | await middleware.runWebpackCompiler(true); |
| @@ -43,6 +43,7 @@ | |||
| 43 | "ip-matching": "^2.1.2", | 43 | "ip-matching": "^2.1.2", |
| 44 | "ip-regex": "^5.0.0", | 44 | "ip-regex": "^5.0.0", |
| 45 | "ipaddr.js": "^2.0.1", | 45 | "ipaddr.js": "^2.0.1", |
| 46 | "is-docker": "^3.0.0", | ||
| 46 | "jimp": "^0.22.10", | 47 | "jimp": "^0.22.10", |
| 47 | "localforage": "^1.10.0", | 48 | "localforage": "^1.10.0", |
| 48 | "lodash": "^4.17.21", | 49 | "lodash": "^4.17.21", |
| @@ -4649,15 +4650,15 @@ | |||
| 4649 | "license": "MIT" | 4650 | "license": "MIT" |
| 4650 | }, | 4651 | }, |
| 4651 | "node_modules/is-docker": { | 4652 | "node_modules/is-docker": { |
| 4652 | "version": "2.2.1", | 4653 | "version": "3.0.0", |
| 4653 | "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-2.2.1.tgz", | 4654 | "resolved": "https://registry.npmjs.org/is-docker/-/is-docker-3.0.0.tgz", |
| 4654 | "integrity": "sha512-F+i2BKsFrH66iaUFc0woD8sLy8getkwTwtOBjvs56Cx4CgJDeKQeqfz8wAYiSb8JOprWhHH5p77PbmYCvvUuXQ==", | 4655 | "integrity": "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ==", |
| 4655 | "license": "MIT", | 4656 | "license": "MIT", |
| 4656 | "bin": { | 4657 | "bin": { |
| 4657 | "is-docker": "cli.js" | 4658 | "is-docker": "cli.js" |
| 4658 | }, | 4659 | }, |
| 4659 | "engines": { | 4660 | "engines": { |
| 4660 | "node": ">=8" | 4661 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" |
| 4661 | }, | 4662 | }, |
| 4662 | "funding": { | 4663 | "funding": { |
| 4663 | "url": "https://github.com/sponsors/sindresorhus" | 4664 | "url": "https://github.com/sponsors/sindresorhus" |
| @@ -4734,6 +4735,21 @@ | |||
| 4734 | "node": ">=8" | 4735 | "node": ">=8" |
| 4735 | } | 4736 | } |
| 4736 | }, | 4737 | }, |
| 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 | }, | ||
| 4737 | "node_modules/isarray": { | 4753 | "node_modules/isarray": { |
| 4738 | "version": "1.0.0", | 4754 | "version": "1.0.0", |
| 4739 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", | 4755 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", |
| @@ -5518,6 +5534,21 @@ | |||
| 5518 | "url": "https://github.com/sponsors/sindresorhus" | 5534 | "url": "https://github.com/sponsors/sindresorhus" |
| 5519 | } | 5535 | } |
| 5520 | }, | 5536 | }, |
| 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 | }, | ||
| 5521 | "node_modules/openai": { | 5552 | "node_modules/openai": { |
| 5522 | "version": "4.17.4", | 5553 | "version": "4.17.4", |
| 5523 | "resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz", | 5554 | "resolved": "https://registry.npmjs.org/openai/-/openai-4.17.4.tgz", |
| @@ -33,6 +33,7 @@ | |||
| 33 | "ip-matching": "^2.1.2", | 33 | "ip-matching": "^2.1.2", |
| 34 | "ip-regex": "^5.0.0", | 34 | "ip-regex": "^5.0.0", |
| 35 | "ipaddr.js": "^2.0.1", | 35 | "ipaddr.js": "^2.0.1", |
| 36 | "is-docker": "^3.0.0", | ||
| 36 | "jimp": "^0.22.10", | 37 | "jimp": "^0.22.10", |
| 37 | "localforage": "^1.10.0", | 38 | "localforage": "^1.10.0", |
| 38 | "lodash": "^4.17.21", | 39 | "lodash": "^4.17.21", |
| @@ -1,11 +1,8 @@ | |||
| 1 | import path from 'node:path'; | 1 | import path from 'node:path'; |
| 2 | import webpack from 'webpack'; | 2 | import webpack from 'webpack'; |
| 3 | import { publicLibConfig } from '../../webpack.config.js'; | 3 | import getPublicLibConfig from '../../webpack.config.js'; |
| 4 | 4 | ||
| 5 | export default function getWebpackServeMiddleware() { | 5 | export default function getWebpackServeMiddleware() { |
| 6 | const outputPath = publicLibConfig.output?.path; | ||
| 7 | const outputFile = publicLibConfig.output?.filename; | ||
| 8 | |||
| 9 | /** | 6 | /** |
| 10 | * A very spartan recreation of webpack-dev-middleware. | 7 | * A very spartan recreation of webpack-dev-middleware. |
| 11 | * @param {import('express').Request} req Request object. | 8 | * @param {import('express').Request} req Request object. |
| @@ -14,6 +11,10 @@ export default function getWebpackServeMiddleware() { | |||
| 14 | * @type {import('express').RequestHandler} | 11 | * @type {import('express').RequestHandler} |
| 15 | */ | 12 | */ |
| 16 | function devMiddleware(req, res, next) { | 13 | function devMiddleware(req, res, next) { |
| 14 | const publicLibConfig = getPublicLibConfig(); | ||
| 15 | const outputPath = publicLibConfig.output?.path; | ||
| 16 | const outputFile = publicLibConfig.output?.filename; | ||
| 17 | |||
| 17 | if (req.method === 'GET' && path.parse(req.path).base === outputFile) { | 18 | if (req.method === 'GET' && path.parse(req.path).base === outputFile) { |
| 18 | return res.sendFile(outputFile, { root: outputPath }); | 19 | return res.sendFile(outputFile, { root: outputPath }); |
| 19 | } | 20 | } |
| @@ -23,9 +24,11 @@ export default function getWebpackServeMiddleware() { | |||
| 23 | 24 | ||
| 24 | /** | 25 | /** |
| 25 | * Wait until Webpack is done compiling. | 26 | * Wait until Webpack is done compiling. |
| 27 | * @param {boolean} [forceDist=false] Whether to force the use the /dist folder. | ||
| 26 | * @returns {Promise<void>} | 28 | * @returns {Promise<void>} |
| 27 | */ | 29 | */ |
| 28 | devMiddleware.runWebpackCompiler = () => { | 30 | devMiddleware.runWebpackCompiler = (forceDist = false) => { |
| 31 | const publicLibConfig = getPublicLibConfig(forceDist); | ||
| 29 | const compiler = webpack(publicLibConfig); | 32 | const compiler = webpack(publicLibConfig); |
| 30 | 33 | ||
| 31 | return new Promise((resolve) => { | 34 | return new Promise((resolve) => { |
| @@ -1,13 +1,49 @@ | |||
| 1 | import process from 'node:process'; | 1 | import process from 'node:process'; |
| 2 | import path from 'node:path'; | 2 | import path from 'node:path'; |
| 3 | import isDocker from 'is-docker'; | ||
| 3 | 4 | ||
| 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 { | ||
| 6 | mode: 'production', | 42 | mode: 'production', |
| 7 | entry: './public/lib.js', | 43 | entry: './public/lib.js', |
| 8 | cache: { | 44 | cache: { |
| 9 | type: 'filesystem', | 45 | type: 'filesystem', |
| 10 | cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'), | 46 | cacheDirectory: cacheDirectory, |
| 11 | store: 'pack', | 47 | store: 'pack', |
| 12 | compression: 'gzip', | 48 | compression: 'gzip', |
| 13 | }, | 49 | }, |
| @@ -28,8 +64,9 @@ export const publicLibConfig = { | |||
| 28 | hints: false, | 64 | hints: false, |
| 29 | }, | 65 | }, |
| 30 | output: { | 66 | output: { |
| 31 | path: path.resolve(process.cwd(), 'dist'), | 67 | path: outputDirectory, |
| 32 | filename: 'lib.js', | 68 | filename: 'lib.js', |
| 33 | libraryTarget: 'module', | 69 | libraryTarget: 'module', |
| 34 | }, | 70 | }, |
| 35 | }; | 71 | }; |
| 72 | } | ||