gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:13:47 27 4
gpt4 key购买 nike

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

当我在生产中运行它时,我不希望打开热模块更换。

我该如何设置它以使其在带有 HMR 的 dev 和没有 HMR 的 prod 中工作?基本上我不知道如何使用 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);

最佳答案

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

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

这将禁用 HMR,如包裹文档 https://parceljs.org/api.html 中所述。 .

关于express - 如何在生产环境中使用 Express 和 Parceljs 中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55182632/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com