"use strict";

var __importDefault = this && this.__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};
Object.defineProperty(exports, "__esModule", {
  value: true
});
const path_1 = __importDefault(require("path"));
const express_1 = __importDefault(require("express"));
const webpack_1 = __importDefault(require("webpack"));
const morgan_1 = __importDefault(require("morgan"));
const compression_1 = __importDefault(require("compression"));
const webpack_dev_middleware_1 = __importDefault(require("webpack-dev-middleware"));
const webpack_hot_middleware_1 = __importDefault(require("webpack-hot-middleware"));
const const_1 = require("../core/src/const");
const isDevelopment = process.env.NODE_ENV === 'development';
const staticPath = path_1.default.join(process.cwd(), './build/client');
const app = (0, express_1.default)();
const start = () => {
  app.listen(const_1.SERVER_PORT, () => console.log(`The app listening at http://localhost:${const_1.SERVER_PORT}`));
};
app.use((0, morgan_1.default)('dev', {
  skip(req) {
    return /^\/(static|__webpack_hmr)/i.test(req.path);
  }
}));
app.use((0, compression_1.default)());
app.use('/static', express_1.default.static(staticPath));
if (isDevelopment) {
  const webpackConfig = require("./.config/webpack/config");
  const compiler = (0, webpack_1.default)(webpackConfig);
  const devMiddleware = (0, webpack_dev_middleware_1.default)(compiler, {
    publicPath: webpackConfig[0].output.publicPath,
    serverSideRender: true,
    writeToDisk: pathname => /server/.test(pathname)
  });
  app.use(devMiddleware);
  app.use((0, webpack_hot_middleware_1.default)(compiler.compilers[0]));
  devMiddleware.waitUntilValid(start);
}
app.get('/', (req, res) => {
  const userAgent = req.headers['user-agent'] ?? '';
  const pathToRenderer = isDevelopment ? './build/server/renderer.js' : '../server/renderer.js';
  const resolved = require.resolve(pathToRenderer);
  if (isDevelopment && require.cache[resolved]) {
    delete require.cache[resolved];
  }
  const render = require(resolved).default;
  const html = render({
    root: 'main',
    userAgent
  });
  res.status(200).send(html);
});
app.on('error', console.error);
if (!isDevelopment) {
  start();
}