如何从外部导入 webpack 入口文件的默认导出?



我想我可以用代码最好地解释它。我在 webpack 中有一个文件,如下所示:

import ReactDOMServer from 'react-dom/server';
import Server from './server';
import templateFn from './template';
export default (req, res) => {
const reactString = ReactDOMServer.renderToString(<Server />);
const template = templateFn(html);
res.send(template);
};

我还有一个快速应用程序,我想在其中访问默认的导出函数。如果有任何区别,此文件就是 webpack 条目文件。这是我在我的快速应用程序中尝试的内容:

const handleRequest = require(path.resolve(webpackConfig.output.path, webpackConfig.output.filename));
app.get('*', (req, res) => {
console.log(handleRequest);
});

我正在尝试导入 webpack 生成的文件,希望我能够访问条目文件的默认导出。好吧,我错了,因为导入的输出{}.

是否有一个 webpack 插件或某种技术来完成我正在尝试构建的事情?我不希望快速应用程序成为 webpack 构建的一部分。这就是我以这种方式分离代码的主要原因。

我能够使用库参数(webpack.config.js访问webpack的内容:

output: {
path: ...,
filename: ...,
library: 'myapp',
libraryTarget: 'commonjs'
}

然后在代码中访问它:

const output = require(path.resolve(webpackConfig.output.path, webpackConfig.output.filename));
const defaultExportFunction = output.myapp.default;

最新更新