如何在生产环境中使用 Express 和 Parceljs 中间件

Dar*_*ryn 5 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);
Run Code Online (Sandbox Code Playgroud)

小智 6

You can set options for the bundler as so

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

This will disable HMR as described in the parcel documentation https://parceljs.org/api.html.