Move webpack cache to data root

441e5c6f7e45ca1294108785d95e9de63548af39

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

5 files changed, +101 -42Ignore whitespace
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+56 -32
@@ -1,35 +1,59 @@
11import process from 'node:process';
22import path from 'node:path';
3+import os from 'node:os';
4+import isDocker from 'is-docker';
35
4-/** @type {import('webpack').Configuration} */
6+/**
5-export const publicLibConfig = {
7+ * Get the Webpack configuration for the public/lib.js file.
6- mode: 'production',
8+ * @param {boolean} forceDist Whether to force the use the /dist folder.
7- entry: './public/lib.js',
9+ * @returns {import('webpack').Configuration}
8- cache: {
10+ * */
9- type: 'filesystem',
11+export default function getPublicLibConfig(forceDist = false) {
10- cacheDirectory: path.resolve(process.cwd(), 'dist/webpack'),
12+ function getCacheDirectory() {
11- store: 'pack',
13+ // Docker got cache pre-baked into the image.
12- compression: 'gzip',
14+ if (forceDist || isDocker()) {
13- },
15+ return path.resolve(process.cwd(), 'dist/webpack');
14- devtool: false,
16+ }
15- watch: false,
17+
16- module: {},
18+ // Data root is set (should be the case 99.99% of the time).
17- stats: {
19+ if (typeof globalThis.DATA_ROOT === 'string') {
18- preset: 'minimal',
20+ return path.resolve(globalThis.DATA_ROOT, '_cache', 'webpack');
19- assets: false,
21+ }
20- modules: false,
22+
21- colors: true,
23+ // Fallback to the system temp directory.
22- timings: true,
24+ return path.resolve(os.tmpdir(), 'webpack');
2325 },
24- experiments: {
26+
25- outputModule: true,
27+ const cacheDirectory = getCacheDirectory();
26- },
28+ return {
27- performance: {
29+ mode: 'production',
28- hints: false,
30+ entry: './public/lib.js',
29- },
31+ cache: {
30- output: {
32+ type: 'filesystem',
31- path: path.resolve(process.cwd(), 'dist'),
33+ cacheDirectory: cacheDirectory,
3234 filename store: 'lib.jspack',
3335 libraryTarget compression: 'modulegzip',
3436 },
35-};
37+ devtool: false,
38+ watch: false,
39+ module: {},
40+ stats: {
41+ preset: 'minimal',
42+ assets: false,
43+ modules: false,
44+ colors: true,
45+ timings: true,
46+ },
47+ experiments: {
48+ outputModule: true,
49+ },
50+ performance: {
51+ hints: false,
52+ },
53+ output: {
54+ path: path.resolve(process.cwd(), 'dist'),
55+ filename: 'lib.js',
56+ libraryTarget: 'module',
57+ },
58+ };
59+}