/
webpack.config.js
192 lines (185 loc) · 5.4 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Variables in .env and .env.defaults will be added to process.env
const dotenv = require("dotenv");
dotenv.config({ path: ".env" });
dotenv.config({ path: ".env.defaults" });
const fs = require("fs");
const path = require("path");
const selfsigned = require("selfsigned");
const webpack = require("webpack");
const cors = require("cors");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
function createHTTPSConfig() {
// Generate certs for the local webpack-dev-server.
if (fs.existsSync(path.join(__dirname, "certs"))) {
const key = fs.readFileSync(path.join(__dirname, "certs", "key.pem"));
const cert = fs.readFileSync(path.join(__dirname, "certs", "cert.pem"));
return { key, cert };
} else {
const pems = selfsigned.generate(
[
{
name: "commonName",
value: "localhost"
}
],
{
days: 365,
algorithm: "sha256",
extensions: [
{
name: "subjectAltName",
altNames: [
{
type: 2,
value: "localhost"
},
{
type: 2,
value: "hubs.local"
}
]
}
]
}
);
fs.mkdirSync(path.join(__dirname, "certs"));
fs.writeFileSync(path.join(__dirname, "certs", "cert.pem"), pems.cert);
fs.writeFileSync(path.join(__dirname, "certs", "key.pem"), pems.private);
return {
key: pems.private,
cert: pems.cert
};
}
}
const defaultHostName = "hubs.local";
const host = process.env.HOST_IP || defaultHostName;
const babelConfig = JSON.parse(fs.readFileSync(path.resolve(__dirname, ".babelrc")).toString());
module.exports = (env, argv) => ({
entry: {
admin: path.join(__dirname, "src", "admin.js")
},
output: {
filename: "assets/js/[name]-[chunkhash].js",
publicPath: process.env.BASE_ASSETS_PATH || ""
},
devtool: argv.mode === "production" ? "source-map" : "inline-source-map",
devServer: {
https: createHTTPSConfig(),
host: process.env.HOST_IP || "0.0.0.0",
port: process.env.PORT || "8989",
public: `${host}:8989`,
useLocalIp: true,
allowedHosts: [host],
headers: {
"Access-Control-Allow-Origin": "*"
},
before: function(app) {
// be flexible with people accessing via a local reticulum on another port
app.use(cors({ origin: /hubs\.local(:\d*)?$/ }));
// networked-aframe makes HEAD requests to the server for time syncing. Respond with an empty body.
app.head("*", function(req, res, next) {
if (req.method === "HEAD") {
res.append("Date", new Date().toGMTString());
res.send("");
} else {
next();
}
});
}
},
performance: {
// Ignore media and sourcemaps when warning about file size.
assetFilter(assetFilename) {
return !/\.(map|png|jpg|gif|glb|webm)$/.test(assetFilename);
}
},
module: {
rules: [
{
test: /\.html$/,
loader: "html-loader"
},
{
test: /\.js$/,
loader: "babel-loader",
options: babelConfig,
exclude: function(modulePath) {
return /node_modules/.test(modulePath) && !/node_modules\/hubs/.test(modulePath);
}
},
{
test: /\.worker\.js$/,
loader: "worker-loader",
options: {
name: "assets/js/[name]-[hash].js",
publicPath: "/",
inline: true
}
},
{
test: /\.(scss|css)$/,
use: [
{
loader: MiniCssExtractPlugin.loader
},
{
loader: "css-loader",
options: {
name: "[path][name]-[hash].[ext]",
localIdentName: "[name]__[local]__[hash:base64:5]",
camelCase: true
}
},
"sass-loader"
]
},
{
test: /\.(glsl|frag|vert)$/,
use: { loader: "raw-loader" }
},
{
test: /\.(png|jpg|gif|glb|ogg|mp3|mp4|wav|woff2|svg|webm)$/,
use: {
loader: "file-loader",
options: {
// move required assets to output dir and add a hash for cache busting
name: "[path][name]-[hash].[ext]",
// Make asset paths relative to /src
context: path.join(__dirname, "src")
}
}
}
]
},
plugins: [
new HTMLWebpackPlugin({
filename: "admin.html",
template: path.join(__dirname, "src", "admin.html")
}),
new CopyWebpackPlugin([
{
from: "src/assets/images/favicon.ico",
to: "favicon.ico"
}
]),
// Extract required css and add a content hash.
new MiniCssExtractPlugin({
filename: "assets/stylesheets/[name]-[contenthash].css",
disable: argv.mode !== "production"
}),
// Define process.env variables in the browser context.
new webpack.DefinePlugin({
"process.env": JSON.stringify({
NODE_ENV: argv.mode,
BUILD_VERSION: process.env.BUILD_VERSION,
CONFIGURABLE_SERVICES: process.env.CONFIGURABLE_SERVICES,
ITA_SERVER: process.env.ITA_SERVER,
RETICULUM_SERVER: process.env.RETICULUM_SERVER,
CORS_PROXY_SERVER: process.env.CORS_PROXY_SERVER,
POSTGREST_SERVER: process.env.POSTGREST_SERVER
})
})
]
});