UNPKG

burdy

Version:

Open Source Headless Platform

59 lines (58 loc) 2.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.attachWebpackMiddleware = exports.compilerRun = void 0; const path_1 = __importDefault(require("path")); const webpack_1 = __importDefault(require("webpack")); const path_util_1 = __importDefault(require("./path.util")); const react_refresh_webpack_plugin_1 = __importDefault(require("@pmmmwh/react-refresh-webpack-plugin")); const webpack_config_1 = require("../../webpack.config"); const compilerRun = (compiler) => { return new Promise((resolve, reject) => { compiler.run((err, stats) => { if (err) { return reject(err); } return resolve(stats); }); }); }; exports.compilerRun = compilerRun; const attachWebpackMiddleware = (app) => __awaiter(void 0, void 0, void 0, function* () { const publicPath = '/admin'; const hmrPath = `${publicPath}/__webpack_hmr`; const webpackConfig = (0, webpack_config_1.webpackAdminConfigure)((wpConfig) => { wpConfig.entry = [ path_util_1.default.admin('index'), `webpack-hot-middleware/client?path=${hmrPath}`, ]; wpConfig.plugins.push(new webpack_1.default.HotModuleReplacementPlugin()); wpConfig.plugins.push(new react_refresh_webpack_plugin_1.default()); return wpConfig; }); const compiler = (0, webpack_1.default)(webpackConfig); const devMiddleware = require('webpack-dev-middleware')(compiler, { publicPath: webpackConfig.output.publicPath, }); app.use(devMiddleware); app.use(require('webpack-hot-middleware')(compiler, { path: hmrPath, })); app.get(['/admin/*', '/admin'], (req, res) => { const index = devMiddleware.context.outputFileSystem.readFileSync(path_1.default.join(webpackConfig.output.path, 'index.html')); res.end(index); }); return devMiddleware; }); exports.attachWebpackMiddleware = attachWebpackMiddleware;