| 1 | import process from 'node:process'; |
| 2 | import path from 'node:path'; |
| 3 | import fs from 'node:fs'; |
| 4 | import crypto from 'node:crypto'; |
| 5 | import isDocker from 'is-docker'; |
| 6 | import webpack from 'webpack'; |
| 7 | import { serverDirectory } from './src/server-directory.js'; |
| 8 | import { getVersion, color } from './src/util.js'; |
| 9 | |
| 10 | /** |
| 11 | * Generate a cache version string based on the application version, Git revision, and Webpack version. |
| 12 | * @returns {string} The cache version string. |
| 13 | */ |
| 14 | function getWebpackCacheVersion() { |
| 15 | return crypto.createHash('shake256', { outputLength: 8 }) |
| 16 | .update(JSON.stringify([appVersion.pkgVersion, appVersion.gitRevision, webpack.version])) |
| 17 | .digest('hex'); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Prune old Webpack cache directories that do not match the current cache version. |
| 22 | * @param {string} webpackRoot The root directory where Webpack caches are stored. |
| 23 | * @param {string} currentCacheVersion The current cache version to keep. |
| 24 | */ |
| 25 | function pruneWebpackCache(webpackRoot, currentCacheVersion) { |
| 26 | try { |
| 27 | if (!fs.existsSync(webpackRoot)) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const cacheDirectories = fs.readdirSync(webpackRoot, { withFileTypes: true }) |
| 32 | .filter(dirent => dirent.isDirectory()) |
| 33 | .map(dirent => dirent.name); |
| 34 | |
| 35 | for (const dir of cacheDirectories) { |
| 36 | const dirPath = path.join(webpackRoot, dir); |
| 37 | if (dir !== currentCacheVersion) { |
| 38 | try { |
| 39 | fs.rmSync(dirPath, { recursive: true, force: true }); |
| 40 | console.debug(`Removed outdated cache directory: ${color.yellow(dir)}`); |
| 41 | } catch (error) { |
| 42 | console.error(`Failed to remove Webpack cache directory: ${color.red(dir)}`, error); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } catch (error) { |
| 47 | console.error('Failed to read Webpack cache directories for pruning.', error); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const appVersion = await getVersion(); |
| 52 | |
| 53 | /** |
| 54 | * Get the Webpack configuration for the public/lib.js file. |
| 55 | * 1. Docker has got cache and the output file pre-baked. |
| 56 | * 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories. |
| 57 | * @param {object} options Configuration options. |
| 58 | * @param {boolean} [options.forceDist=false] Whether to force the use the /dist folder. |
| 59 | * @param {boolean} [options.pruneCache=false] Whether to prune old cache directories. |
| 60 | * @returns {import('webpack').Configuration} |
| 61 | * @throws {Error} If the DATA_ROOT variable is not set. |
| 62 | * */ |
| 63 | export default function getPublicLibConfig({ forceDist = false, pruneCache = false } = {}) { |
| 64 | function getWebpackRoot() { |
| 65 | if (forceDist || isDocker()) { |
| 66 | return path.resolve(process.cwd(), 'dist', '_webpack'); |
| 67 | } |
| 68 | |
| 69 | if (typeof globalThis.DATA_ROOT === 'string') { |
| 70 | return path.resolve(globalThis.DATA_ROOT, '_webpack'); |
| 71 | } |
| 72 | |
| 73 | throw new Error('DATA_ROOT variable is not set.'); |
| 74 | } |
| 75 | |
| 76 | function getCacheDirectory() { |
| 77 | return path.join(webpackRoot, cacheVersion, 'cache'); |
| 78 | } |
| 79 | |
| 80 | function getOutputDirectory() { |
| 81 | return path.join(webpackRoot, cacheVersion, 'output'); |
| 82 | } |
| 83 | |
| 84 | const webpackRoot = getWebpackRoot(); |
| 85 | const cacheVersion = getWebpackCacheVersion(); |
| 86 | const cacheDirectory = getCacheDirectory(); |
| 87 | const outputDirectory = getOutputDirectory(); |
| 88 | |
| 89 | if (pruneCache) { |
| 90 | pruneWebpackCache(webpackRoot, cacheVersion); |
| 91 | } |
| 92 | |
| 93 | return { |
| 94 | mode: 'production', |
| 95 | entry: path.join(serverDirectory, 'public/lib.js'), |
| 96 | cache: { |
| 97 | type: 'filesystem', |
| 98 | cacheDirectory: cacheDirectory, |
| 99 | store: 'pack', |
| 100 | compression: 'gzip', |
| 101 | }, |
| 102 | devtool: false, |
| 103 | watch: false, |
| 104 | module: {}, |
| 105 | stats: { |
| 106 | preset: 'minimal', |
| 107 | assets: false, |
| 108 | modules: false, |
| 109 | colors: true, |
| 110 | timings: true, |
| 111 | }, |
| 112 | experiments: { |
| 113 | outputModule: true, |
| 114 | }, |
| 115 | performance: { |
| 116 | hints: false, |
| 117 | }, |
| 118 | output: { |
| 119 | path: outputDirectory, |
| 120 | filename: 'lib.js', |
| 121 | libraryTarget: 'module', |
| 122 | }, |
| 123 | }; |
| 124 | } |