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

View File

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

View File

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

View File

@ -1,13 +1,36 @@
import process from 'node:process';
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.
* @param {boolean} forceDist Whether to force the use the /dist folder.
* @returns {import('webpack').Configuration}
* */
export default function getPublicLibConfig(forceDist = false) {
function getCacheDirectory() {
// Docker got cache pre-baked into the image.
if (forceDist || isDocker()) {
return path.resolve(process.cwd(), 'dist/webpack');
}
// Data root is set (should be the case 99.99% of the time).
if (typeof globalThis.DATA_ROOT === 'string') {
return path.resolve(globalThis.DATA_ROOT, '_cache', 'webpack');
}
// Fallback to the system temp directory.
return path.resolve(os.tmpdir(), 'webpack');
}
const cacheDirectory = getCacheDirectory();
return {
mode: 'production',
entry: './public/lib.js',
cache: {
type: 'filesystem',
cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'),
cacheDirectory: cacheDirectory,
store: 'pack',
compression: 'gzip',
},
@ -33,3 +56,4 @@ export const publicLibConfig = {
libraryTarget: 'module',
},
};
}