2

我看到这个:

[HMR] Updated modules:
[HMR]  - ./src/resolvers.js
[HMR] Update applied.

但是什么都没有更新。我已经尝试了一些我在网上找到的关于 webpack 的 output.publicPath 的其他解决方案。我不知道还能做什么,很快我就要投入生产了。重新启动服务器不是我的选择。

我的代码:

import express from 'express'
import { execute, subscribe } from 'graphql';
import { ApolloServer } from 'apollo-server-express'

import schema from './schema';


import typeDefs from './typeDefs'
import resolvers from './resolvers'

import getUserByToken from './getUserByToken';


const app = express();

const path = '/graphql';

const server = new ApolloServer({ typeDefs, resolvers, });

server.applyMiddleware({ app });

server.listen(8081, () => {
  console.log(` Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  console.log(` Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})



let currentApp = app

if (module.hot) {
    module.hot.accept(['./index', './resolvers'], () => {
        server.removeListener('request', currentApp);
        server.on('request', app);
        currentApp = app;
    });
}

谢谢!

4

1 回答 1

0

您可以通过将 promise 传递给hot.module.data然后在新的重新加载时等待它来解决这个问题。

import { server } from "./server";

async function startServer() {
  if (module.hot?.data?.oldServer) await module.hot.data.oldServer;
  const { url } = await server.listen();
  console.log(`Server ready at ${url}. `);
}

startServer();

if (module.hot) {
  module.hot.accept();
  module.hot.dispose((data) => {
    data.oldServer = server.stop();
  });
}

于 2021-10-01T00:08:18.237 回答