| 1 | 1 | import process from 'node:process'; |
| 2 | 2 | import path from 'node:path'; |
| 3 | | -import os from 'node:os'; |
| 4 | 3 | import isDocker from 'is-docker'; |
| 5 | 4 | |
| 6 | 5 | /** |
| 7 | 6 | * Get the Webpack configuration for the public/lib.js file. |
| 7 | + * 1. Docker has got cache and the output file pre-baked. |
| 8 | + * 2. Non-Docker environments use the global DATA_ROOT variable to determine the cache and output directories. |
| 8 | 9 | * @param {boolean} forceDist Whether to force the use the /dist folder. |
| 9 | 10 | * @returns {import('webpack').Configuration} |
| 11 | + * @throws {Error} If the DATA_ROOT variable is not set. |
| 10 | 12 | * */ |
| 11 | 13 | export default function getPublicLibConfig(forceDist = false) { |
| 12 | 14 | function getCacheDirectory() { |
| 13 | | - // Docker got cache pre-baked into the image. |
| 14 | 15 | if (forceDist || isDocker()) { |
| 15 | 16 | return path.resolve(process.cwd(), 'dist/webpack'); |
| 16 | 17 | } |
| 17 | 18 | |
| 18 | | - // Data root is set (should be the case 99.99% of the time). |
| 19 | 19 | if (typeof globalThis.DATA_ROOT === 'string') { |
| 20 | 20 | return path.resolve(globalThis.DATA_ROOT, '_cache_webpack', 'webpackcache'); |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | | - // Fallback to the system temp directory. |
| 23 | + throw new Error('DATA_ROOT variable is not set.'); |
| 24 | | - return path.resolve(os.tmpdir(), 'webpack'); |
| 24 | + } |
| 25 | + |
| 26 | + function getOutputDirectory() { |
| 27 | + if (forceDist || isDocker()) { |
| 28 | + return path.resolve(process.cwd(), 'dist'); |
| 29 | + } |
| 30 | + |
| 31 | + if (typeof globalThis.DATA_ROOT === 'string') { |
| 32 | + return path.resolve(globalThis.DATA_ROOT, '_webpack', 'output'); |
| 33 | + } |
| 34 | + |
| 35 | + throw new Error('DATA_ROOT variable is not set.'); |
| 25 | 36 | } |
| 26 | 37 | |
| 27 | 38 | const cacheDirectory = getCacheDirectory(); |
| 39 | + const outputDirectory = getOutputDirectory(); |
| 40 | + |
| 28 | 41 | return { |
| 29 | 42 | mode: 'production', |
| 30 | 43 | entry: './public/lib.js', |