Newer
Older
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
// Variables in .env will be added to process.env
require("dotenv").config();
const fs = require("fs");
const path = require("path");
const selfsigned = require("selfsigned");
const webpack = require("webpack");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const _ = require("lodash");
function createHTTPSConfig() {
if (process.env.NODE_ENV === "production") {
return false;
}
let https;
// 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"
}
]
}
]
}
);
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 {
cert: pems.cert
};
}
}
class LodashTemplatePlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.plugin("compilation", compilation => {
compilation.plugin("html-webpack-plugin-before-html-processing", async data => {
data.html = _.template(data.html, this.options)();
return data;
});
});
}
}
entry: {
lobby: path.join(__dirname, "src", "lobby.js"),
room: path.join(__dirname, "src", "room.js"),
onboarding: path.join(__dirname, "src", "onboarding.js")
},
output: {
path: path.join(__dirname, "public"),
filename: "[name]-[chunkhash].js",
publicPath: process.env.BASE_ASSETS_PATH || ""
},
mode: "development",
devtool: process.env.NODE_ENV === "production" ? "source-map" : "inline-source-map",
devServer: {
open: true,
https: createHTTPSConfig(),
port: 8080,
before: function(app) {
// 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.send("");
} else {
next();
}
});
}
},
performance: {
// Ignore media and sourcemaps when warning about file size.
assetFilter(assetFilename) {
return !/\.(map|png|jpg|gif|glb)$/.test(assetFilename);
}
},
module: {
rules: [
{
test: /\.html$/,
loader: "html-loader",
options: {
// <a-asset-item>'s src property is overwritten with the correct transformed asset url.
attrs: [
"img:src",
"a-asset-item:src",
"a-progressive-asset:src",
"a-progressive-asset:high-src",
// You can get transformed asset urls in an html template using ${require("pathToFile.ext")}
interpolate: "require"
}
},
{
test: /\.js$/,
include: [path.resolve(__dirname, "src")],
// Exclude JS assets in node_modules because they are already transformed and often big.
exclude: [path.resolve(__dirname, "node_modules")],
loader: "babel-loader",
query: {
plugins: ["transform-class-properties", "transform-object-rest-spread"]
}
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: {
loader: "css-loader",
options: {
minimize: process.env.NODE_ENV === "production"
}
}
})
},
{
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use: {
loader: "file-loader",
options: {
// move required assets to /public and add a hash for cache busting
name: "[path][name]-[hash].[ext]",
// Make asset paths relative to /src
context: path.join(__dirname, "src")
}
}
}
]
},
plugins: [
// Each output page needs a HTMLWebpackPlugin entry
new HTMLWebpackPlugin({
filename: "index.html",
template: path.join(__dirname, "src", "lobby.html"),
// Chunks correspond with the entries you wish to include in your html template
chunks: ["lobby"]
}),
new HTMLWebpackPlugin({
filename: "room.html",
template: path.join(__dirname, "src", "room.html"),
chunks: ["room"],
inject: "head"
}),
new HTMLWebpackPlugin({
filename: "onboarding.html",
template: path.join(__dirname, "src", "onboarding.html"),
chunks: ["onboarding"]
}),
// Extract required css and add a content hash.
new ExtractTextPlugin("[name]-[contenthash].css", {
disable: process.env.NODE_ENV !== "production"
}),
// Transform the output of the html-loader using _.template
// before passing the result to html-webpack-plugin
new LodashTemplatePlugin({
// expose these variables to the lodash template
// ex: <%= ORIGIN_TRIAL_TOKEN %>
imports: {
NODE_ENV: process.env.NODE_ENV,
ORIGIN_TRIAL_EXPIRES: process.env.ORIGIN_TRIAL_EXPIRES,
ORIGIN_TRIAL_TOKEN: process.env.ORIGIN_TRIAL_TOKEN
}
}),
// Define process.env variables in the browser context.
new webpack.DefinePlugin({
"process.env": JSON.stringify({
NODE_ENV: process.env.NODE_ENV,
JANUS_SERVER: process.env.JANUS_SERVER
})
})
]
};
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
module.exports = () => {
if (process.env.GENERATE_SMOKE_TESTS && process.env.BASE_ASSETS_PATH) {
const smokeConfig = Object.assign({}, config, {
// Set the public path for to point to the correct assets on the smoke-test build.
output: Object.assign({}, config.output, {
publicPath: process.env.BASE_ASSETS_PATH.replace("://", "://smoke-")
}),
// For this config
plugins: config.plugins.map(plugin => {
if (plugin instanceof HTMLWebpackPlugin) {
return new HTMLWebpackPlugin(
Object.assign({}, plugin.options, {
filename: "smoke-" + plugin.options.filename
})
);
}
return plugin;
})
});
return [config, smokeConfig];
} else {
return config;
}
};