| 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 os from 'node:os'; | | |
| 4 | import isDocker from 'is-docker'; | 3 | import isDocker from 'is-docker'; |
| 5 | | 4 | |
| 6 | /** | 5 | /** |
| 7 | * Get the Webpack configuration for the public/lib.js file. | 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 | * @param {boolean} forceDist Whether to force the use the /dist folder. | 9 | * @param {boolean} forceDist Whether to force the use the /dist folder. |
| 9 | * @returns {import('webpack').Configuration} | 10 | * @returns {import('webpack').Configuration} |
| | 11 | * @throws {Error} If the DATA_ROOT variable is not set. |
| 10 | * */ | 12 | * */ |
| 11 | export default function getPublicLibConfig(forceDist = false) { | 13 | export default function getPublicLibConfig(forceDist = false) { |
| 12 | function getCacheDirectory() { | 14 | function getCacheDirectory() { |
| 13 | // Docker got cache pre-baked into the image. | | |
| 14 | if (forceDist || isDocker()) { | 15 | if (forceDist || isDocker()) { |
| 15 | return path.resolve(process.cwd(), 'dist/webpack'); | 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 | if (typeof globalThis.DATA_ROOT === 'string') { | 19 | if (typeof globalThis.DATA_ROOT === 'string') { |
| 20 | return path.resolve(globalThis.DATA_ROOT, '_cache', 'webpack'); | 20 | return path.resolve(globalThis.DATA_ROOT, '_webpack', 'cache'); |
| 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 | const cacheDirectory = getCacheDirectory(); | 38 | const cacheDirectory = getCacheDirectory(); |
| | 39 | const outputDirectory = getOutputDirectory(); |
| | 40 | |
| 28 | return { | 41 | return { |
| 29 | mode: 'production', | 42 | mode: 'production', |
| 30 | entry: './public/lib.js', | 43 | entry: './public/lib.js', |