【问题标题】:How to use Express with Parceljs middleware in Production如何在生产环境中使用 Express 和 Parceljs 中间件
【发布时间】:2019-08-06 12:50:31
【问题描述】:

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

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

如何设置它以使其在带有 HMR 的 dev 中和在没有 HMR 的 prod 中工作?基本上我不知道如何在这个中间件上使用build 模式:https://parceljs.org/production.html#%E2%9C%A8-production

如果这是在 dev 中,我应该只使用 parcel-bundler,如果这是在 prod 中,我应该使用 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);

【问题讨论】:

    标签: express parceljs


    【解决方案1】:

    您可以为捆绑器设置选项

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-22
      • 2018-06-09
      • 1970-01-01
      • 2015-06-14
      • 2019-12-23
      • 2021-06-21
      • 1970-01-01
      • 2021-07-08
      • 2021-09-30
      相关资源
      最近更新 更多