Spoke/webpack.config.js

274 строки
7.3 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

2018-09-15 00:32:30 +03:00
// Variables in .env and .env.defaults will be added to process.env
const dotenv = require("dotenv");
2019-05-01 00:20:49 +03:00
2018-09-15 00:32:30 +03:00
if (process.env.NODE_ENV === "production") {
dotenv.config({ path: ".env.prod" });
} else if (process.env.NODE_ENV === "test") {
dotenv.config({ path: ".env.test" });
2018-09-15 00:32:30 +03:00
} else {
dotenv.config({ path: ".env" });
dotenv.config({ path: ".env.defaults" });
}
2019-03-09 02:31:08 +03:00
const fs = require("fs");
const selfsigned = require("selfsigned");
const cors = require("cors");
2018-05-29 04:35:21 +03:00
const HTMLWebpackPlugin = require("html-webpack-plugin");
2018-09-12 07:31:16 +03:00
const path = require("path");
const webpack = require("webpack");
const TerserJSPlugin = require("terser-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
2018-05-29 04:35:21 +03:00
2019-03-09 02:31:08 +03:00
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 port = process.env.HOST_PORT || 9090;
2019-03-09 02:31:08 +03:00
2019-04-04 21:47:49 +03:00
module.exports = env => {
return {
2019-04-04 21:47:49 +03:00
entry: {
entry: ["./src/index.js"]
},
2018-05-29 04:35:21 +03:00
2019-04-04 21:47:49 +03:00
devtool: process.env.NODE_ENV === "production" ? "source-map" : "inline-source-map",
2018-07-04 01:50:49 +03:00
2019-04-04 21:47:49 +03:00
devServer: {
https: createHTTPSConfig(),
historyApiFallback: true,
port,
2019-04-04 21:47:49 +03:00
host: process.env.HOST_IP || "0.0.0.0",
public: `${host}:${port}`,
2019-04-04 21:47:49 +03:00
publicPath: process.env.BASE_ASSETS_PATH || "",
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*)?$/ }));
}
2019-03-09 02:31:08 +03:00
},
2019-04-04 21:47:49 +03:00
output: {
filename: "assets/js/[name]-[chunkhash].js",
publicPath: process.env.BASE_ASSETS_PATH || "/"
},
2018-05-29 04:35:21 +03:00
2019-04-04 21:47:49 +03:00
module: {
rules: [
{
test: /\.(png|jpg|jpeg|gif|svg)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/images"
}
2019-04-03 03:32:52 +03:00
}
2019-04-04 21:47:49 +03:00
},
{
test: /\.(woff|woff2|ttf|eot)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/fonts"
}
2019-04-03 03:32:52 +03:00
}
2019-04-04 21:47:49 +03:00
},
{
test: /\.(glb)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/models"
}
}
2019-04-04 21:47:49 +03:00
},
2019-09-21 22:29:56 +03:00
{
test: /\.(gltf)(\?.*$|$)/,
use: {
loader: "gltf-webpack-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/models"
}
}
},
{
test: /\.(bin)$/,
use: [
{
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/models"
}
}
]
},
2019-04-04 21:47:49 +03:00
{
test: /\.(mp4|webm)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/videos"
}
}
2019-04-04 21:47:49 +03:00
},
{
test: /\.(spoke)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
name: "[name]-[hash].[ext]",
outputPath: "assets/templates"
}
}
},
2019-04-04 21:47:49 +03:00
{
test: /\.js$/,
include: path.join(__dirname, "src"),
use: "babel-loader"
},
{
test: /\.worker\.js$/,
include: path.join(__dirname, "src"),
loader: "worker-loader",
2019-04-03 03:32:52 +03:00
options: {
// Workers must be inlined because they are hosted on a CDN and CORS doesn't permit us
// from loading worker scripts from another origin. To minimize bundle size, dynamically
// import a wrapper around the worker. See SketchfabZipLoader.js and API.js for an example.
2019-05-03 00:22:08 +03:00
name: "assets/js/workers/[name]-[hash].js",
inline: true,
fallback: false
2019-04-04 21:47:49 +03:00
}
},
{
test: /\.wasm$/,
type: "javascript/auto",
use: {
loader: "file-loader",
options: {
outputPath: "assets/js/wasm",
name: "[name]-[hash].[ext]"
}
2019-04-03 03:32:52 +03:00
}
}
2019-04-04 21:47:49 +03:00
]
},
2018-05-29 04:35:21 +03:00
2019-05-02 21:16:31 +03:00
target: "web",
node: {
__dirname: false,
fs: "empty",
Buffer: false,
process: false
},
2019-04-04 21:47:49 +03:00
optimization: {
minimizer: [new TerserJSPlugin({ sourceMap: true, parallel: true, cache: path.join(__dirname, ".tersercache") })]
2019-04-04 21:47:49 +03:00
},
2019-04-04 21:47:49 +03:00
plugins: [
new BundleAnalyzerPlugin({
analyzerMode: env && env.BUNDLE_ANALYZER ? "server" : "disabled"
}),
new CopyWebpackPlugin([
{
2019-11-19 23:59:59 +03:00
from: path.join(
__dirname,
"src",
"assets",
process.env.IS_MOZ === "true" ? "favicon-spoke.ico" : "favicon-editor.ico"
),
to: "assets/images/favicon.ico"
2019-04-04 21:47:49 +03:00
}
]),
new CopyWebpackPlugin([
{
from: path.join(__dirname, "src", "assets", "favicon-spoke.ico"),
to: "assets/images/favicon-spoke.ico"
}
]),
new CopyWebpackPlugin([
{
from: path.join(__dirname, "src", "assets", "favicon-editor.ico"),
to: "assets/images/favicon-editor.ico"
}
]),
2019-04-04 21:47:49 +03:00
new HTMLWebpackPlugin({
template: path.join(__dirname, "src", "index.html"),
2019-11-19 23:59:59 +03:00
faviconPath: (process.env.BASE_ASSETS_PATH || "/") + "assets/images/favicon.ico"
2019-04-04 21:47:49 +03:00
}),
new webpack.EnvironmentPlugin({
BUILD_VERSION: "dev",
2019-04-04 21:47:49 +03:00
NODE_ENV: "development",
RETICULUM_SERVER: undefined,
THUMBNAIL_SERVER: "",
2019-04-04 21:47:49 +03:00
HUBS_SERVER: undefined,
CORS_PROXY_SERVER: null,
BASE_ASSETS_PATH: "",
NON_CORS_PROXY_DOMAINS: "",
2019-05-01 00:20:49 +03:00
ROUTER_BASE_PATH: "",
SENTRY_DSN: null,
2019-10-31 01:47:16 +03:00
GA_TRACKING_ID: null,
2019-11-20 00:48:36 +03:00
IS_MOZ: false,
GITHUB_ORG: "mozilla",
GITHUB_REPO: "spoke",
GITHUB_PUBLIC_TOKEN: "de8cbfb4cc0281c7b731c891df431016c29b0ace"
2019-04-04 21:47:49 +03:00
})
]
};
};