Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Express with Parceljs middleware in Production

I am using Parcel middleware with express as explained here: https://parceljs.org/api.html#middleware

When I run this in production I don't want hot-module replacement to be turned on.

How can I set this up so that it works in dev with HMR and in prod without HMR? Essentially I don't know how to use the build mode with this middleware: https://parceljs.org/production.html#%E2%9C%A8-production

Should I only use parcel-bundler if this is in dev and use static config if this is in prod?

Adding sample code for reference:

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);
like image 613
Daryn Avatar asked Mar 15 '19 12:03

Daryn


1 Answers

You can set options for the bundler as so

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

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

like image 60
cs.jasonbrooks Avatar answered Sep 24 '22 12:09

cs.jasonbrooks