snort/packages/app/webpack.config.js

140 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-05-15 17:38:26 +00:00
// Generated using webpack-cli https://github.com/webpack/webpack-cli
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
2023-05-17 22:02:43 +00:00
const TerserPlugin = require("terser-webpack-plugin");
2023-05-15 17:38:26 +00:00
const ESLintPlugin = require("eslint-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
2023-05-17 22:36:34 +00:00
const CopyPlugin = require("copy-webpack-plugin");
2023-05-15 17:38:26 +00:00
const TsTransformer = require("@formatjs/ts-transformer");
const isProduction = process.env.NODE_ENV == "production";
const config = {
2023-05-16 17:54:49 +00:00
entry: {
main: "./src/index.tsx",
},
2023-05-15 17:38:26 +00:00
target: "browserslist",
2023-05-16 17:54:49 +00:00
devtool: isProduction ? "source-map" : "eval",
2023-05-15 17:38:26 +00:00
output: {
publicPath: "/",
path: path.resolve(__dirname, "build"),
2023-05-16 17:54:49 +00:00
filename: ({ runtime }) => {
if (runtime === "sw") {
return "[name].js";
}
return isProduction ? "[name].[chunkhash].js" : "[name].js";
},
clean: isProduction,
2023-05-15 17:38:26 +00:00
},
devServer: {
open: true,
host: "localhost",
historyApiFallback: true,
},
plugins: [
2023-05-17 22:36:34 +00:00
new CopyPlugin({
patterns: [
{ from: "public/manifest.json" },
{ from: "public/robots.txt" },
{ from: "public/nostrich_512.png" },
{ from: "public/nostrich_256.png" },
2023-05-23 15:13:51 +00:00
{ from: "_headers" },
2023-05-17 22:36:34 +00:00
],
}),
2023-05-15 17:38:26 +00:00
new HtmlWebpackPlugin({
template: "public/index.html",
favicon: "public/favicon.ico",
2023-05-17 22:36:34 +00:00
excludeChunks: ["sw"],
2023-05-15 17:38:26 +00:00
}),
new ESLintPlugin(),
new MiniCssExtractPlugin({
2023-05-16 17:54:49 +00:00
filename: isProduction ? "[name].[chunkhash].css" : "[name].css",
2023-05-15 17:38:26 +00:00
}),
],
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
use: [
"babel-loader",
{
loader: "ts-loader",
options: {
getCustomTransformers() {
return {
before: [
TsTransformer.transform({
overrideIdFn: "[sha512:contenthash:base64:6]",
}),
],
};
},
},
},
],
exclude: ["/node_modules/"],
},
{
test: /\.css$/i,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif|webp)$/i,
type: "asset",
},
],
},
optimization: {
usedExports: true,
2023-05-16 17:54:49 +00:00
chunkIds: "deterministic",
2023-05-17 22:02:43 +00:00
minimize: isProduction,
minimizer: [
"...",
// same as https://github.com/facebook/create-react-app/blob/main/packages/react-scripts/config/webpack.config.js
new TerserPlugin({
terserOptions: {
parse: {
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
comparisons: false,
inline: 2,
},
mangle: {
safari10: true,
},
keep_classnames: isProduction,
keep_fnames: isProduction,
output: {
ecma: 5,
comments: false,
ascii_only: true,
},
},
}),
new CssMinimizerPlugin(),
],
2023-05-15 17:38:26 +00:00
},
resolve: {
extensions: [".tsx", ".ts", ".jsx", ".js", "..."],
modules: ["node_modules", __dirname, path.resolve(__dirname, "src")],
},
};
module.exports = () => {
if (isProduction) {
config.mode = "production";
2023-05-16 17:54:49 +00:00
config.entry.sw = {
import: "./src/service-worker.ts",
2023-05-17 22:36:34 +00:00
filename: "service-worker.js",
2023-05-16 17:54:49 +00:00
};
2023-05-15 17:38:26 +00:00
} else {
config.mode = "development";
}
return config;
};