如何将webpack-dev-middleware添加到现有节点/反应应用

时间:2018-12-27 18:48:35

标签: node.js reactjs express webpack webpack-dev-middleware

我有一个运行nodejs并在服务器端进行表达并在客户端进行React的现有应用程序。整个使用打字稿。

客户端代码是使用create-react-app创建的,此后已弹出。这段代码在/ client中。

服务器端代码在/ server中。

每个文件夹都有自己的node_modules,package.json,webpack配置等。

当前,开发时我必须在端口3000上运行客户端,而服务器在端口5000上运行。相反,我正在研究如何使其工作,以便所有内容都从端口5000运行,包括自动重新编译客户端捆绑包。我认为webpack-dev-middleware将允许我完成此任务。但是,我遇到了与相对路径有关的麻烦。

这是我添加到服务器(/server/src/app.ts)的代码:

import * as webpack from 'webpack';
import * as webpackDevMiddleware from 'webpack-dev-middleware';

const webpackConfig = require('../../client/config/webpack.config.dev.js');
const webpackCompiler = webpack(webpackConfig);

this.app.use(webpackDevMiddleware(webpackCompiler, {
  publicPath: webpackConfig.output.publicPath,
  stats: { colors: true }
}));

我引用的webpack配置文件是create-react-app随附的标准文件。由于该文件位于/ client / config文件夹中,因此出现有关模块分辨率的错误:

ERROR in ../client/config/webpack.config.dev.js
Module not found: Error: Can't resolve './env' in '/client/config'
 @ ../client/config/webpack.config.dev.js 11:29-45
 @ ./src/app.ts
 @ multi ./src/app.ts

ERROR in ../client/config/webpack.config.dev.js
Module not found: Error: Can't resolve './paths' in '/client/config'
 @ ../client/config/webpack.config.dev.js 12:14-32
 @ ./src/app.ts
 @ multi ./src/app.ts

ERROR in ../client/config/webpack.config.dev.js
Module not found: Error: Can't resolve './polyfills' in '/client/config'
 @ ../client/config/webpack.config.dev.js 37:4-34
 @ ./src/app.ts
 @ multi ./src/app.ts

在服务器端Webpack捆绑期间发生此错误。有办法使它起作用吗?

0 个答案:

没有答案