| 1 | 1 | import process from 'node:process'; |
| 2 | 2 | import path from 'node:path'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import crypto from 'node:crypto'; |
| 3 | 5 | import isDocker from 'is-docker'; |
| 4 | 6 | import webpack from 'webpack'; |
| 5 | 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(); |
| 6 | 52 | |
| 7 | 53 | /** |
| 8 | 54 | * Get the Webpack configuration for the public/lib.js file. |
| 9 | 55 | * 1. Docker has got cache and the output file pre-baked. |
| 10 | 56 | * 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories. |
| 11 | 57 | * @param {booleanobject} forceDist Whether to force the use theoptions /distConfiguration folderoptions. |
| 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. |
| 12 | 60 | * @returns {import('webpack').Configuration} |
| 13 | 61 | * @throws {Error} If the DATA_ROOT variable is not set. |
| 14 | 62 | * */ |
| 15 | 63 | export default function getPublicLibConfig({ forceDist = false, pruneCache = false } = {}) { |
| 16 | 64 | function getCacheDirectorygetWebpackRoot() { |
| 17 | 65 | if (forceDist || isDocker()) { |
| 18 | 66 | return path.resolve(process.cwd(), 'dist', '_webpack', webpack.version, 'cache'); |
| 19 | 67 | } |
| 20 | 68 | |
| 21 | 69 | if (typeof globalThis.DATA_ROOT === 'string') { |
| 22 | 70 | return path.resolve(globalThis.DATA_ROOT, '_webpack', webpack.version, 'cache'); |
| 23 | 71 | } |
| 24 | 72 | |
| 25 | 73 | throw new Error('DATA_ROOT variable is not set.'); |
| 26 | 74 | } |
| 27 | 75 | |
| 28 | 76 | function getOutputDirectorygetCacheDirectory() { |
| 29 | | - if (forceDist || isDocker()) { |
| 77 | + return path.join(webpackRoot, cacheVersion, 'cache'); |
| 30 | | - return path.resolve(process.cwd(), 'dist', '_webpack', webpack.version, 'output'); |
| 78 | + } |
| 31 | | - } |
| 32 | | - |
| 33 | | - if (typeof globalThis.DATA_ROOT === 'string') { |
| 34 | | - return path.resolve(globalThis.DATA_ROOT, '_webpack', webpack.version, 'output'); |
| 35 | | - } |
| 36 | 79 | |
| 37 | | - throw new Error('DATA_ROOT variable is not set.'); |
| 80 | + function getOutputDirectory() { |
| 81 | + return path.join(webpackRoot, cacheVersion, 'output'); |
| 38 | 82 | } |
| 39 | 83 | |
| 84 | + const webpackRoot = getWebpackRoot(); |
| 85 | + const cacheVersion = getWebpackCacheVersion(); |
| 40 | 86 | const cacheDirectory = getCacheDirectory(); |
| 41 | 87 | const outputDirectory = getOutputDirectory(); |
| 42 | 88 | |
| 89 | + if (pruneCache) { |
| 90 | + pruneWebpackCache(webpackRoot, cacheVersion); |
| 91 | + } |
| 92 | + |
| 43 | 93 | return { |
| 44 | 94 | mode: 'production', |
| 45 | 95 | entry: path.join(serverDirectory, 'public/lib.js'), |