Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to serve a local development environment over https using pnpm and webpack #976

Open
onmyway133 opened this issue Jul 29, 2024 · 0 comments

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Jul 29, 2024

When developing locally, especially when interacting with third-party services that have CORS restrictions, serving your development environment over a custom domain with HTTPS can be crucial. Let’s walk through the steps to achieve this with pnpm and webpack. In our example, the domain we want to use is local.onmyway133.com

Map Localhost to Your Domain Name

First, we need to map our custom domain to localhost. Open your hosts file to add this mapping.

sudo vim /etc/hosts

Add the following line to the bottom of the file:

127.0.0.1  local.onmyway133.com

Save the file :wq and close your editor.

Generate a Self-Signed SSL Certificate

Next, we’ll generate a self-signed SSL certificate. Run the following command from the root of your project directory:

mkdir ssl
openssl req -x509 -out ssl/local.onmyway133.com.crt -keyout ssl/local.onmyway133.com.key -newkey rsa:2048 -nodes -sha256

You’ll be prompted to answer several questions. For the Common Name, enter local.onmyway133.com.

After completing the process, you’ll find the .crt and .key files in ssl folder

Trust the SSL Certificate

Double-click the generated .crt file to add it to Keychain Access. Once added, set the certificate to Always Trust.

image

Configure webpack dev server for HTTPS

Now, we need to configure webpack-dev-server to use our custom domain and HTTPS. Update your webpack.config.js with the following:

module.exports = {
  // other webpack configurations...
  devServer: {
    host: "local.onmyway133.com",
    port: 80,
    server: {
      type: "https",
      options: {
        key: "/users/khoa/downloads/ssl/local.onmyway133.com.key",
        cert: "/users/khoa/downloads/ssl/local.onmyway133.com.crt",
      },
    },
  },
};

Now, you’re ready to start your development server. Run the following command in your terminal:

pnpm start // webpack serve

Your application should now be accessible at https://local.onmyway133.com.

By following these steps, you’ve set up your local development environment to serve over a custom domain with HTTPS, allowing you to interact with third-party services that have strict CORS policies. This setup ensures a smoother and more secure development experience.

For more details on webpack-dev-server configuration, refer to the webpack documentation.

@onmyway133 onmyway133 changed the title How to server a local development environment over https using pnpm and webpack How to serve a local development environment over https using pnpm and webpack Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant