Skip to content

Commit

Permalink
Fixed middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksSavelev committed Oct 4, 2024
1 parent 5758fcb commit 12395d0
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/VisualManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,27 +259,35 @@ export default class VisualManager {
}

private prepareDropFiles() {
this.webpackConfig.devServer.onBeforeSetupMiddleware = (devServer) => {
this.webpackConfig.devServer.setupMiddlewares = (middlewares, devServer) => {
const { headers, publicPath, static: { directory } } = this.webpackConfig.devServer;
const assets = [ 'visual.js`', 'visual.css', 'pbiviz.json' ]

const setHeaders = (res) => {
Object.getOwnPropertyNames(headers)
.forEach(property => res.header(property, headers[property]));
};
const readFile = (file, res) => {
fs.readFile(file).then(function (content) {
res.write(content);
res.end();
const readFile = (file, res, name) => {
middlewares.unshift({
name,
middleware: (req, middlewareRes) => {
fs.readFile(file).then(function (content) {
middlewareRes.write(content);
console.log(`Serving ${name} to `);
middlewareRes.end();
});
},
});
res.end();
};

assets.forEach(asset => {
devServer.app.get(`${publicPath}/${asset}`, function (req, res) {
setHeaders(res);
readFile(path.join(directory, asset), res);
readFile(path.join(directory, asset), res, asset);
});
});
return middlewares;
};
}

Expand Down

0 comments on commit 12395d0

Please sign in to comment.