Skip to content

Latest commit

 

History

History
56 lines (47 loc) · 1.72 KB

File metadata and controls

56 lines (47 loc) · 1.72 KB

Razzle Devcert Https Example

How to use

Create and start the example:
npx create-razzle-app --example with-devcert-https with-devcert-https

cd with-devcert-https
yarn start

Idea behind the example

This example demonstrates how to use a razzle.config.js file to modify Razzle's underlying configuration using a promise. It modifies the name of the server's output file in production (razzle build).

Note that this file is not transpiled, and so you must write it with vanilla Node.js-compatible JavaScript.

// razzle.config.js
'use strict';

module.exports = {
  modifyWebpackOptions(opts) {
    const options = opts.webpackOptions;
    return new Promise(async (resolve) => {
      const httpsCredentials = await devcert.certificateFor('localhost');
      const stringHttpsCredentials = {
        key: httpsCredentials.key.toString(),
        cert: httpsCredentials.cert.toString()
      };
      if (opts.env.target === 'node' && opts.env.dev) {
        options.definePluginOptions.HTTPS_CREDENTIALS = JSON.stringify(stringHttpsCredentials);
      }
      if (opts.env.target === 'web' && opts.env.dev) {
        options.HTTPS_CREDENTIALS = httpsCredentials;
      }
      resolve(options);
    });
  },
  modifyWebpackConfig(opts) {
    const config = opts.webpackConfig;
    const options = opts.webpackOptions;
    if (opts.env.target === 'web' && opts.env.dev) {
      config.devServer.https = options.HTTPS_CREDENTIALS;
    }
    return config;
  },
};