import path from 'path'; import { fileURLToPath } from 'url'; import webpack from 'webpack'; import CopyPlugin from 'copy-webpack-plugin'; import WorkboxPlugin from 'workbox-webpack-plugin'; import HtmlWebpackPlugin from 'html-webpack-plugin'; import HtmlWebpackInjectPreload from '@principalstudio/html-webpack-inject-preload'; import MiniCssExtractPlugin from 'mini-css-extract-plugin'; import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'; import { GitRevisionPlugin } from 'git-revision-webpack-plugin'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const dist = path.resolve(__dirname, 'dist'); const gitRevisionPlugin = new GitRevisionPlugin(); const isDevServer = process.env.WEBPACK_SERVE; const appName = "Öffisearch"; export default { mode: 'production', entry: './src/main.js', output: { path: dist, filename: "[name].js" }, module: { rules: [{ test: /\.css$/, use: [ isDevServer ? 'style-loader' : MiniCssExtractPlugin.loader, 'css-loader', ], }], }, performance: { hints: false, }, optimization: { minimize: true, minimizer: [ `...`, new CssMinimizerPlugin(), ], }, devServer: { static: dist, client: { progress: true, }, proxy: [{ context: [ '/hafas', '/db' ], target: 'https://oeffi.katja.wtf', changeOrigin: true, }], }, plugins: [ new webpack.NormalModuleReplacementPlugin(/(node:|https-proxy-agent|cross-fetch|db-hafas-stations)/, (resource) => { const newRequest = { 'node:buffer': 'buffer', 'node:assert': 'assert', 'https-proxy-agent': path.resolve(__dirname, 'src/shim/https-proxy-agent.js'), 'cross-fetch': path.resolve(__dirname, 'src/shim/cross-fetch.js'), 'db-hafas-stations': path.resolve(__dirname, 'src/shim/db-hafas-stations.js'), }[resource.request]; if (newRequest) { resource.request = newRequest; } }), new webpack.DefinePlugin({ 'process.env': '{}', 'isDevServer': isDevServer, 'APPNAME': JSON.stringify(appName), 'VERSION': JSON.stringify(process.env.GIT_VERSION ? process.env.GIT_VERSION : gitRevisionPlugin.version()), 'COMMIT': JSON.stringify(process.env.GIT_COMMIT ? process.env.GIT_COMMIT : gitRevisionPlugin.commithash()), 'COMMITDATE': JSON.stringify(process.env.GIT_COMMITDATE ? process.env.GIT_COMMITDATE : gitRevisionPlugin.lastcommitdatetime()), }), new webpack.ProvidePlugin({ Buffer: ['buffer', 'Buffer'], }), new CopyPlugin({ patterns: [ { from: 'src/assets/favicon.png', }, { from: 'src/assets/manifest.json', }, ], }), new HtmlWebpackPlugin({ title: appName, favicon: './src/assets/favicon.svg', scriptLoading: 'module', hash: true, inject: 'body', template: './src/assets/index.html', minify: { collapseWhitespace: true, keepClosingSlash: true, removeComments: true, removeRedundantAttributes: true, removeScriptTypeAttributes: true, removeStyleLinkTypeAttributes: true, useShortDoctype: true, minifyCSS: true, } }), new HtmlWebpackInjectPreload({ files: [{ match: /.*\.js$/, attributes: {as: 'script'}, }], }), ].concat(isDevServer ? [] : [ new MiniCssExtractPlugin(), new WorkboxPlugin.GenerateSW({ clientsClaim: true, skipWaiting: true, }), ]), resolve: { modules: [ 'node_modules', 'node_modules/hafas-client/node_modules', ], fallback: { 'buffer': 'buffer', 'assert': 'assert', 'https': path.resolve(__dirname, './src/shim/https.js'), 'crypto': path.resolve(__dirname, './src/shim/crypto.js'), 'stream': false, 'http': false, 'url': false, 'tls': false, 'net': false, } } };