const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const CssMinimizerPlugin = require('css-minimizer-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin'); module.exports = (env, argv) => { const isProd = argv.mode === 'production'; return { entry: { sheet: './fabula-ultima-sheet.js', // Imports the shared CSS so HtmlWebpackPlugin can inject it into the book page book: './book.js', }, output: { filename: isProd ? '[name].[contenthash].js' : '[name].js', path: path.resolve(__dirname, 'dist'), clean: true, // Disable IIFE wrapping so onclick= handlers can reach global functions iife: false, }, module: { rules: [ { test: /\.css$/, use: [ isProd ? MiniCssExtractPlugin.loader : 'style-loader', 'css-loader', ], }, ], }, plugins: [ ...(isProd ? [new MiniCssExtractPlugin({ filename: '[name].[contenthash].css' })] : []), new HtmlWebpackPlugin({ template: './fabula-ultima-sheet.html', filename: 'index.html', chunks: ['sheet'], scriptLoading: 'blocking', }), new HtmlWebpackPlugin({ template: './html/index.html', filename: 'book/index.html', chunks: ['book'], scriptLoading: 'blocking', }), // Copy book pages to dist/book/ (excluding index.html, managed by HtmlWebpackPlugin) new CopyWebpackPlugin({ patterns: [ { from: 'html', to: 'book', globOptions: { ignore: ['**/index.html'] }, }, ], }), ], optimization: { minimizer: ['...', new CssMinimizerPlugin()], splitChunks: { cacheGroups: { // Merge CSS from all entries into a single shared file styles: { name: 'styles', type: 'css/mini-extract', chunks: 'all', enforce: true, }, }, }, }, devServer: { static: [ { directory: path.resolve(__dirname, 'dist') }, // Serve raw html/ pages at /book in dev so they don't need to be copied { directory: path.resolve(__dirname, 'html'), publicPath: '/book' }, ], port: 8080, open: true, historyApiFallback: { rewrites: [ // /book (no trailing slash) → /book/index.html { from: /^\/book$/, to: '/book/index.html' }, ], }, }, }; };