如何在生产环境中将Express与Parceljs中间件一起使用

时间:2019-03-15 12:28:14

标签: express parceljs

我正在使用带有Express的Parcel中间件,如此处所述:https://parceljs.org/api.html#middleware

在生产环境中运行此程序时,我不希望打开热模块更换功能。

我如何设置它以便它可以在具有HMR的开发人员和没有HMR的产品中工作?基本上,我不知道如何在此中间件上使用build模式:https://parceljs.org/production.html#%E2%9C%A8-production

如果仅在parcel-bundler中使用dev,是否在产品中使用static配置?

添加示例代码以供参考:

const Bundler = require('parcel-bundler');
const app = require('express')();

const file = 'index.html'; // Pass an absolute path to the entrypoint here
const options = {}; // See options section of api docs, for the possibilities

// Initialize a new bundler using a file and options
const bundler = new Bundler(file, options);

// Let express use the bundler middleware, this will let Parcel handle every request over your express server
app.use(bundler.middleware());

// Listen on port 8080
app.listen(8080);

1 个答案:

答案 0 :(得分:2)

您可以这样设置捆绑器的选项

const bundlerOptions = { production: process.env.NODE_ENV === 'production' };
const bundler        = new Bundler( filePath, bundlerOptions );

这将按照包裹文档https://parceljs.org/api.html中的说明禁用HMR。