Set lib.js output path to data

0f461289800642d6262bbb06cb1a86007d11bb94

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +20 -7Ignore whitespace
webpack.config.js+20 -7
@@ -1,30 +1,43 @@
1import process from 'node:process';1import process from 'node:process';
2import path from 'node:path';2import path from 'node:path';
3import os from 'node:os';
4import isDocker from 'is-docker';3import isDocker from 'is-docker';
54
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 * */
11export default function getPublicLibConfig(forceDist = false) {13export 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 }
1718
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 }
2222
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 }
2637
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',
@@ -51,7 +64,7 @@ export default function getPublicLibConfig(forceDist = false) {
51 hints: false,64 hints: false,
52 },65 },
53 output: {66 output: {
54 path: path.resolve(process.cwd(), 'dist'),67 path: outputDirectory,
55 filename: 'lib.js',68 filename: 'lib.js',
56 libraryTarget: 'module',69 libraryTarget: 'module',
57 },70 },