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