Move webpack cache to data root

This commit is contained in:
Cohee 2025-02-17 23:41:59 +02:00
parent fa4a75215b
commit 441e5c6f7e
5 changed files with 101 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,59 @@
import process from 'node:process'; import process from 'node:process';
import path from 'node:path'; import path from 'node:path';
import os from 'node:os';
import isDocker from 'is-docker';
/** @type {import('webpack').Configuration} */ /**
export const publicLibConfig = { * Get the Webpack configuration for the public/lib.js file.
mode: 'production', * @param {boolean} forceDist Whether to force the use the /dist folder.
entry: './public/lib.js', * @returns {import('webpack').Configuration}
cache: { * */
type: 'filesystem', export default function getPublicLibConfig(forceDist = false) {
cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'), function getCacheDirectory() {
store: 'pack', // Docker got cache pre-baked into the image.
compression: 'gzip', if (forceDist || isDocker()) {
}, return path.resolve(process.cwd(), 'dist/webpack');
devtool: false, }
watch: false,
module: {}, // Data root is set (should be the case 99.99% of the time).
stats: { if (typeof globalThis.DATA_ROOT === 'string') {
preset: 'minimal', return path.resolve(globalThis.DATA_ROOT, '_cache', 'webpack');
assets: false, }
modules: false,
colors: true, // Fallback to the system temp directory.
timings: true, return path.resolve(os.tmpdir(), 'webpack');
}, }
experiments: {
outputModule: true, const cacheDirectory = getCacheDirectory();
}, return {
performance: { mode: 'production',
hints: false, entry: './public/lib.js',
}, cache: {
output: { type: 'filesystem',
path: path.resolve(process.cwd(), 'dist'), cacheDirectory: cacheDirectory,
filename: 'lib.js', store: 'pack',
libraryTarget: 'module', 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: path.resolve(process.cwd(), 'dist'),
filename: 'lib.js',
libraryTarget: 'module',
},
};
}