Skip to content

Commit afbc13f

Browse files
author
PatrickSachs
committed
Serve project over HTTPS in dev mode
1 parent 5147b80 commit afbc13f

File tree

10 files changed

+44
-8
lines changed

10 files changed

+44
-8
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SERVICE_WORKER=false
2+
BROWSERSYNC=false

.env.dev

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BROWSERSYNC=true

.env.prod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SERVICE_WORKER=true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
dist
22
node_modules
3+
ssl/key.pem
4+
ssl/cert.pem
5+
.env.local

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"browser-sync-webpack-plugin": "^2.2.2",
3636
"copy-webpack-plugin": "^5.0.1",
3737
"css-loader": "^2.1.0",
38+
"dotenv": "^6.2.0",
3839
"style-loader": "^0.23.1",
3940
"ts-loader": "^5.3.3",
4041
"typescript": "^3.2.4",

src/d/slow-tree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ declare global {
1313
const process: {
1414
readonly env: {
1515
readonly NODE_ENV: "production" | "development"
16+
readonly SERVICE_WORKER: "true" | "false";
17+
readonly BROWSERSYNC: "true" | "false";
1618
}
1719
}
1820

src/register-serviceworker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Check that service workers are registered
22
if ('serviceWorker' in navigator) {
3-
if (process.env.NODE_ENV === "production") {
3+
if (process.env.SERVICE_WORKER === "true") {
44
// Use the window load event to keep the page load performant
55
window.addEventListener('load', () => {
66
navigator.serviceWorker.register('/serviceworker.js').then(registration => {

ssl/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Certificates
2+
3+
Generate SSL certificates using this tool: https://github.com/FiloSottile/mkcert/releases
4+
5+
Place your certificate as `cert.pem` & private key as `key.pem`.
6+
7+
Make sure to use the specified name when connecting to your server.

webpack.config.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const dotenv = require("dotenv");
23
const webpack = require('webpack');
34
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
45
const VueLoaderPlugin = require("vue-loader/lib/plugin");
@@ -7,7 +8,12 @@ const CleanPlugin = require("clean-webpack-plugin");
78
const WorkboxPlugin = require("workbox-webpack-plugin");
89

910
const isProduction = process.env.NODE_ENV === "production";
10-
console.log("Running in production mode?", isProduction);
11+
12+
dotenv.config({ path: ".env.local" });
13+
dotenv.config({ path: isProduction ? ".env.prod" : ".env.dev" });
14+
dotenv.config({ path: ".env" });
15+
16+
console.log("Enviroment variables are:", process.env);
1117

1218
// ===============================================
1319
// MISC SETTINGS
@@ -23,26 +29,34 @@ plugins.push(new VueLoaderPlugin());
2329
plugins.push(new webpack.DefinePlugin({
2430
CANVAS_RENDERER: JSON.stringify(true),
2531
WEBGL_RENDERER: JSON.stringify(true),
26-
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
32+
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
33+
'process.env.SERVICE_WORKER': JSON.stringify(process.env.SERVICE_WORKER),
34+
'process.env.BROWSERSYNC': JSON.stringify(process.env.BROWSERSYNC)
2735
}));
2836
plugins.push(new CopyPlugin(["index.html", "manifest.json", { from: "assets", to: "assets" }]));
29-
if (!isProduction) {
37+
if (process.env.BROWSERSYNC === "true") {
3038
plugins.push(new BrowserSyncPlugin({
3139
host: process.env.IP || 'localhost',
3240
port: process.env.PORT || 3000,
3341
server: {
3442
baseDir: ['./dist']
35-
}
43+
},
44+
https: {
45+
key: "ssl/key.pem",
46+
cert: "ssl/cert.pem"
47+
},
48+
open: false
3649
}));
37-
}
38-
if (isProduction) {
50+
} else {
3951
plugins.push(new CleanPlugin());
52+
}
53+
if (process.env.SERVICE_WORKER === "true") {
4054
plugins.push(new WorkboxPlugin.GenerateSW({
4155
swDest: "serviceworker.js",
4256
clientsClaim: true,
4357
importWorkboxFrom: "local",
4458
skipWaiting: true
45-
}))
59+
}));
4660
}
4761

4862
module.exports = {

0 commit comments

Comments
 (0)