/** * Reference: https://webpack.js.org/configuration/configuration-languages/ */ import type { Configuration } from 'webpack'; import type WebpackDevServer from 'webpack-dev-server'; import path from 'path'; import HtmlWebpackPlugin from 'html-webpack-plugin'; import MiniCssExtractPlugin from 'mini-css-extract-plugin'; const ROOT_PATH = path.resolve(__dirname, './'); const DIST_PATH = path.resolve(ROOT_PATH, './dist'); const ASSET_PATH = process.env.ASSET_PATH || '/'; declare module 'webpack' { interface Configuration { devServer?: WebpackDevServer.Configuration; } } const isDev = process.env.NODE_ENV === 'development'; const mode = isDev ? 'development' : 'production'; const config: Configuration = { mode, entry: { app: './src/index.tsx', }, output: { path: DIST_PATH, filename: '[name].[contenthash].js', publicPath: ASSET_PATH, }, devServer: { port: 11011, historyApiFallback: true, }, module: { rules: [ { test: /\.tsx?$/, exclude: /node_modules/, loader: 'esbuild-loader', options: { loader: 'tsx', target: 'es2015', }, }, { test: /\.(less|css)$/, use: [ { loader: MiniCssExtractPlugin.loader, }, { loader: 'css-loader', options: { // https://github.com/webpack-contrib/css-loader#auto modules: { auto: /\.module\.\w+$/i, localIdentName: '[path][name]__[local]--[hash:base64:5]', }, sourceMap: process.env.NODE_ENV !== 'production', }, }, { loader: 'postcss-loader', options: { postcssOptions: { config: path.resolve(ROOT_PATH, 'postcss.config.js'), }, }, }, { loader: 'less-loader', }, ], }, { test: /\.(png|jpg|gif|woff|woff2|svg|eot|ttf)$/, loader: 'url-loader', options: { limit: 8192, name: 'assets/[name].[hash:7].[ext]', }, }, ], }, resolve: { extensions: ['.tsx', '.ts', '.js', '.css'], }, plugins: [ new HtmlWebpackPlugin({ title: 'PawChat', inject: true, hash: true, template: path.resolve(ROOT_PATH, './assets/template.html'), }), new MiniCssExtractPlugin({ filename: 'styles-[contenthash].css' }), ], }; export default config;