mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
46 lines
1.4 KiB
JavaScript
46 lines
1.4 KiB
JavaScript
import path from 'node:path';
|
|
import webpack from 'webpack';
|
|
import { publicLibConfig } from '../../webpack.config.js';
|
|
|
|
export default function getWebpackServeMiddleware() {
|
|
const outputPath = publicLibConfig.output?.path;
|
|
const outputFile = publicLibConfig.output?.filename;
|
|
const compiler = webpack(publicLibConfig);
|
|
|
|
/**
|
|
* A very spartan recreation of webpack-dev-middleware.
|
|
* @param {import('express').Request} req Request object.
|
|
* @param {import('express').Response} res Response object.
|
|
* @param {import('express').NextFunction} next Next function.
|
|
* @type {import('express').RequestHandler}
|
|
*/
|
|
function devMiddleware(req, res, next) {
|
|
if (req.method === 'GET' && path.parse(req.path).base === outputFile) {
|
|
return res.sendFile(outputFile, { root: outputPath });
|
|
}
|
|
|
|
next();
|
|
}
|
|
|
|
/**
|
|
* Wait until Webpack is done compiling.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
devMiddleware.runWebpackCompiler = () => {
|
|
return new Promise((resolve) => {
|
|
console.log();
|
|
console.log('Compiling frontend libraries...');
|
|
compiler.run((_error, stats) => {
|
|
const output = stats?.toString(publicLibConfig.stats);
|
|
if (output) {
|
|
console.log(output);
|
|
console.log();
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
};
|
|
|
|
return devMiddleware;
|
|
}
|