2021-05-29 20:48:56 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2021-09-05 20:10:23 +02:00
|
|
|
const path = require("path");
|
|
|
|
const webpack = require("webpack");
|
2023-02-24 03:43:29 +01:00
|
|
|
const MonacoWebpackPlugin = require("monaco-editor-webpack-plugin");
|
2021-09-05 20:10:23 +02:00
|
|
|
const HtmlWebpackPlugin = require("html-webpack-plugin");
|
|
|
|
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
2021-09-17 05:51:38 +02:00
|
|
|
const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
|
2024-07-19 01:27:01 +02:00
|
|
|
const CopyPlugin = require("copy-webpack-plugin");
|
2018-02-24 23:55:06 +01:00
|
|
|
|
2024-07-26 11:17:59 +02:00
|
|
|
/** @type {import("webpack-cli").CallableOption} */
|
2018-09-16 04:20:55 +02:00
|
|
|
module.exports = (env, argv) => {
|
2024-07-26 11:17:59 +02:00
|
|
|
const isDevServer = (env || {}).WEBPACK_SERVE === true;
|
2021-09-05 01:09:30 +02:00
|
|
|
const runInContainer = (env || {}).runInContainer === true;
|
|
|
|
const isDevelopment = argv.mode === "development";
|
2024-06-12 23:31:13 +02:00
|
|
|
const enableReactRefresh = (env || {}).enableReactRefresh === true;
|
2022-04-12 23:07:30 +02:00
|
|
|
const outputDirectory = "dist";
|
2021-09-20 05:29:02 +02:00
|
|
|
const entry = "./src/index.tsx";
|
2019-05-07 03:01:06 +02:00
|
|
|
|
2024-07-26 11:17:59 +02:00
|
|
|
/** @type {webpack.StatsOptions} */
|
2021-09-05 01:09:30 +02:00
|
|
|
const statsConfig = {
|
|
|
|
builtAt: true,
|
|
|
|
children: false,
|
|
|
|
chunks: false,
|
|
|
|
chunkGroups: false,
|
|
|
|
chunkModules: false,
|
|
|
|
chunkOrigins: false,
|
|
|
|
colors: true,
|
2021-09-05 20:31:40 +02:00
|
|
|
entrypoints: false,
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
2018-09-16 04:20:55 +02:00
|
|
|
|
2024-07-26 11:17:59 +02:00
|
|
|
/** @type {webpack.Configuration["devServer"]} */
|
2021-09-05 01:09:30 +02:00
|
|
|
const devServerSettings = {
|
2021-09-17 05:51:38 +02:00
|
|
|
hot: true,
|
2021-09-05 01:09:30 +02:00
|
|
|
port: 8000,
|
2023-01-06 02:29:02 +01:00
|
|
|
devMiddleware: {
|
|
|
|
stats: statsConfig,
|
|
|
|
},
|
2023-01-07 00:16:17 +01:00
|
|
|
static: {
|
2023-01-07 00:20:29 +01:00
|
|
|
directory: path.join(__dirname, "dist"),
|
|
|
|
publicPath: "/dist",
|
2023-01-07 00:16:17 +01:00
|
|
|
},
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
2021-05-08 21:02:26 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// By default, the webpack-dev-server is not exposed outside of localhost.
|
|
|
|
// When running in a container we need it accessible externally.
|
|
|
|
if (runInContainer) {
|
|
|
|
devServerSettings.host = "0.0.0.0";
|
|
|
|
}
|
2021-05-08 21:02:26 +02:00
|
|
|
|
2021-12-16 21:23:27 +01:00
|
|
|
// Get the current commit hash to inject into the app
|
|
|
|
// https://stackoverflow.com/a/38401256
|
2021-12-17 02:07:34 +01:00
|
|
|
const commitHash = require("child_process").execSync("git rev-parse --short HEAD").toString().trim();
|
2021-12-16 21:23:27 +01:00
|
|
|
|
2024-07-26 11:17:59 +02:00
|
|
|
/** @type {HtmlWebpackPlugin.Options} */
|
2022-04-12 23:34:10 +02:00
|
|
|
const htmlConfig = {
|
|
|
|
title: "Bitburner",
|
|
|
|
template: "src/index.html",
|
2023-04-07 13:43:39 +02:00
|
|
|
filename: isDevServer ? "index.html" : "../index.html",
|
2022-04-12 23:34:10 +02:00
|
|
|
favicon: "favicon.ico",
|
|
|
|
meta: {},
|
|
|
|
minify: isDevelopment
|
|
|
|
? false
|
|
|
|
: {
|
|
|
|
collapseBooleanAttributes: true,
|
|
|
|
collapseInlineTagWhitespace: false,
|
|
|
|
collapseWhitespace: false,
|
|
|
|
conservativeCollapse: false,
|
|
|
|
html5: true,
|
|
|
|
includeAutoGeneratedTags: false,
|
|
|
|
keepClosingSlash: true,
|
|
|
|
minifyCSS: false,
|
|
|
|
minifyJS: false,
|
|
|
|
minifyURLs: false,
|
|
|
|
preserveLineBreaks: false,
|
|
|
|
preventAttributesEscaping: false,
|
|
|
|
processConditionalComments: false,
|
|
|
|
quoteCharacter: '"',
|
|
|
|
removeAttributeQuotes: false,
|
|
|
|
removeComments: false,
|
|
|
|
removeEmptyAttributes: false,
|
|
|
|
removeEmptyElements: false,
|
|
|
|
removeOptionalTags: false,
|
|
|
|
removeScriptTypeAttributes: false,
|
|
|
|
removeStyleLinkTypeAttributes: false,
|
|
|
|
removeTagWhitespace: false,
|
|
|
|
sortAttributes: false,
|
|
|
|
sortClassName: false,
|
|
|
|
useShortDoctype: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return {
|
|
|
|
plugins: [
|
2024-03-07 00:52:32 +01:00
|
|
|
new MonacoWebpackPlugin({ languages: ["javascript", "typescript", "json"] }),
|
2021-09-05 01:09:30 +02:00
|
|
|
new webpack.DefinePlugin({
|
2021-09-09 05:47:34 +02:00
|
|
|
"process.env.NODE_ENV": isDevelopment ? '"development"' : '"production"',
|
2021-09-05 01:09:30 +02:00
|
|
|
}),
|
2022-04-12 23:34:10 +02:00
|
|
|
new HtmlWebpackPlugin(htmlConfig),
|
2021-09-05 20:10:23 +02:00
|
|
|
new ForkTsCheckerWebpackPlugin({
|
|
|
|
typescript: {
|
|
|
|
diagnosticOptions: {
|
|
|
|
semantic: true,
|
|
|
|
syntactic: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2021-12-16 21:23:27 +01:00
|
|
|
new webpack.DefinePlugin({
|
2021-12-17 02:07:34 +01:00
|
|
|
__COMMIT_HASH__: JSON.stringify(commitHash || "DEV"),
|
2021-12-16 21:23:27 +01:00
|
|
|
}),
|
2021-09-05 20:31:40 +02:00
|
|
|
// In dev mode, use a faster method of create sourcemaps
|
|
|
|
// while keeping lines/columns accurate
|
|
|
|
isDevServer &&
|
|
|
|
new webpack.EvalSourceMapDevToolPlugin({
|
|
|
|
// Exclude vendor files from sourcemaps
|
|
|
|
// This is a huge speed improvement for not much loss
|
|
|
|
exclude: ["vendor"],
|
|
|
|
columns: true,
|
|
|
|
module: true,
|
|
|
|
}),
|
|
|
|
!isDevServer &&
|
|
|
|
new webpack.SourceMapDevToolPlugin({
|
|
|
|
filename: "[file].map",
|
|
|
|
columns: true,
|
|
|
|
module: true,
|
|
|
|
}),
|
2024-06-12 23:31:13 +02:00
|
|
|
enableReactRefresh && new ReactRefreshWebpackPlugin(),
|
2024-07-19 01:27:01 +02:00
|
|
|
new CopyPlugin({
|
|
|
|
patterns: [
|
|
|
|
{
|
|
|
|
from: "{tex-chtml.js,*/**/*}",
|
|
|
|
to: "mathjax",
|
|
|
|
context: "node_modules/mathjax-full/es5",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
2021-09-05 20:31:40 +02:00
|
|
|
].filter(Boolean),
|
2021-09-05 01:09:30 +02:00
|
|
|
target: "web",
|
2021-09-20 05:29:02 +02:00
|
|
|
entry: entry,
|
2021-09-05 01:09:30 +02:00
|
|
|
output: {
|
2022-03-21 13:04:59 +01:00
|
|
|
path: path.resolve(__dirname, outputDirectory),
|
2021-09-05 01:09:30 +02:00
|
|
|
filename: "[name].bundle.js",
|
2023-02-24 03:43:29 +01:00
|
|
|
assetModuleFilename: "assets/[hash][ext][query]",
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
2022-01-06 13:04:03 +01:00
|
|
|
test: /\.(js$|jsx|ts|tsx)$/,
|
2021-09-05 01:09:30 +02:00
|
|
|
exclude: /node_modules/,
|
2024-07-07 09:08:33 +02:00
|
|
|
resourceQuery: { not: /raw/ },
|
2021-09-05 02:52:23 +02:00
|
|
|
use: {
|
2021-09-05 07:17:30 +02:00
|
|
|
loader: "babel-loader",
|
2021-09-05 02:52:23 +02:00
|
|
|
options: {
|
2024-06-12 23:31:13 +02:00
|
|
|
plugins: [enableReactRefresh && require.resolve("react-refresh/babel")].filter(Boolean),
|
2021-09-05 07:17:30 +02:00
|
|
|
cacheDirectory: true,
|
2021-09-05 02:52:23 +02:00
|
|
|
},
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
2019-05-07 03:01:06 +02:00
|
|
|
},
|
2024-05-23 03:50:09 +02:00
|
|
|
{ test: /\.(ttf|woff2|png|jpe?g|gif|jp2|webp)$/, type: "asset/resource" },
|
2021-09-05 01:09:30 +02:00
|
|
|
{
|
|
|
|
test: /\.s?css$/,
|
2023-02-24 03:43:29 +01:00
|
|
|
use: ["style-loader", "css-loader"],
|
2022-01-19 16:51:36 +01:00
|
|
|
},
|
2024-07-07 09:08:33 +02:00
|
|
|
{
|
|
|
|
resourceQuery: /raw/,
|
|
|
|
type: "asset/source",
|
|
|
|
},
|
2021-09-05 01:09:30 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
optimization: {
|
|
|
|
removeAvailableModules: true,
|
|
|
|
removeEmptyChunks: true,
|
|
|
|
mergeDuplicateChunks: true,
|
|
|
|
flagIncludedChunks: true,
|
|
|
|
sideEffects: true,
|
|
|
|
providedExports: true,
|
|
|
|
usedExports: true,
|
|
|
|
concatenateModules: false,
|
|
|
|
minimize: !isDevelopment,
|
|
|
|
portableRecords: true,
|
|
|
|
splitChunks: {
|
|
|
|
cacheGroups: {
|
|
|
|
vendor: {
|
|
|
|
test: /[\\/]node_modules[\\/]/,
|
2022-03-21 13:04:59 +01:00
|
|
|
name: `vendor`,
|
2021-09-05 01:09:30 +02:00
|
|
|
chunks: "all",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
devServer: devServerSettings,
|
2024-07-26 11:17:59 +02:00
|
|
|
watchOptions: {
|
|
|
|
// When running in a container, we can't necesarily watch filesystem events.
|
|
|
|
poll: runInContainer ? true : undefined,
|
|
|
|
},
|
2021-09-05 01:09:30 +02:00
|
|
|
resolve: {
|
|
|
|
extensions: [".tsx", ".ts", ".js", ".jsx"],
|
2022-10-10 00:42:14 +02:00
|
|
|
alias: {
|
|
|
|
"@player": path.resolve(__dirname, "src/Player"),
|
2023-06-12 06:34:20 +02:00
|
|
|
"@enums": path.resolve(__dirname, "src/Enums"),
|
2022-12-30 02:28:53 +01:00
|
|
|
"@nsdefs": path.resolve(__dirname, "src/ScriptEditor/NetscriptDefinitions.d.ts"),
|
2022-10-10 00:42:14 +02:00
|
|
|
},
|
2023-01-06 02:29:02 +01:00
|
|
|
fallback: { crypto: false },
|
2021-09-05 01:09:30 +02:00
|
|
|
},
|
|
|
|
stats: statsConfig,
|
2024-07-14 23:47:10 +02:00
|
|
|
ignoreWarnings: [
|
|
|
|
{
|
|
|
|
module: /@babel\/standalone/,
|
|
|
|
message: /Critical dependency: the request of a dependency is an expression/,
|
|
|
|
},
|
|
|
|
],
|
2021-09-05 01:09:30 +02:00
|
|
|
};
|
2018-09-16 04:20:55 +02:00
|
|
|
};
|