backend.js 1.1 KB
Newer Older
J
Jason Park 已提交
1 2
const {
  __DEV__,
J
Jason Park 已提交
3
  backendBuildPath,
J
Jason Park 已提交
4 5 6 7 8 9 10
} = require('../environment');

if (__DEV__) {
  const webpack = require('webpack');
  const webpackConfig = require('../webpack.backend.config.js');
  const compiler = webpack(webpackConfig);

11
  let backend = null;
J
Jason Park 已提交
12
  let lastHash = null;
13 14 15 16 17
  compiler.watch({
    watchOptions: {
      ignored: /public/,
    },
  }, (err, stats) => {
J
Jason Park 已提交
18 19 20 21
    if (err) {
      lastHash = null;
      compiler.purgeInputFileSystem();
      console.error(err);
22
    } else if (stats.hash !== lastHash) {
J
Jason Park 已提交
23 24 25
      lastHash = stats.hash;
      console.info(stats.toString({
        cached: false,
26
        colors: true,
J
Jason Park 已提交
27 28
      }));

29 30
      delete require.cache[require.resolve(backendBuildPath)];
      backend = require(backendBuildPath).default;
J
Jason Park 已提交
31 32
    }
  });
33

34 35 36
  const backendWrapper = (req, res, next) => backend(req, res, next);
  backendWrapper.getHierarchy = () => backend.hierarchy;
  module.exports = backendWrapper;
J
Jason Park 已提交
37
} else {
38 39 40 41
  const backend = require(backendBuildPath).default;
  const backendWrapper = (req, res, next) => backend(req, res, next);
  backendWrapper.getHierarchy = () => backend.hierarchy;
  module.exports = backendWrapper;
J
Jason Park 已提交
42
}