|
| 1 | +import CssMinimizerPlugin from 'css-minimizer-webpack-plugin'; |
| 2 | +import HtmlWebpackPlugin from 'html-webpack-plugin'; |
| 3 | +import MiniCssExtractPlugin from 'mini-css-extract-plugin'; |
| 4 | +import path from 'path'; |
| 5 | +import TerserJSPlugin from 'terser-webpack-plugin'; |
| 6 | +import type { Configuration } from 'webpack'; |
| 7 | +import type { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server'; |
| 8 | + |
| 9 | +const NODE_ENV = (process.env.NODE_ENV || 'development') as Configuration['mode']; |
| 10 | + |
| 11 | +const config: Configuration & { |
| 12 | + devServer?: WebpackDevServerConfiguration; |
| 13 | +} = { |
| 14 | + mode: NODE_ENV, |
| 15 | + devtool: 'eval-source-map', |
| 16 | + devServer: { |
| 17 | + host: 'localhost', |
| 18 | + port: 9000, |
| 19 | + historyApiFallback: true, |
| 20 | + open: true, |
| 21 | + static: { |
| 22 | + directory: path.resolve(__dirname, 'dist'), |
| 23 | + }, |
| 24 | + client: { |
| 25 | + overlay: true, |
| 26 | + }, |
| 27 | + devMiddleware: { |
| 28 | + writeToDisk: true, |
| 29 | + index: 'index.html', |
| 30 | + }, |
| 31 | + }, |
| 32 | + module: { |
| 33 | + rules: [ |
| 34 | + { |
| 35 | + test: /\.(jsx?|tsx?)$/, |
| 36 | + exclude: /node_modules\/(?!@koku-ui)/, |
| 37 | + use: [ |
| 38 | + { |
| 39 | + loader: 'ts-loader', |
| 40 | + options: { |
| 41 | + allowTsInNodeModules: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + test: /\.css$/, |
| 48 | + use: ['style-loader', 'css-loader'], |
| 49 | + }, |
| 50 | + { |
| 51 | + test: /\.(svg|ttf|eot|woff|woff2)$/, |
| 52 | + type: 'asset/resource', |
| 53 | + // only process modules with this loader |
| 54 | + // if they live under a 'fonts' or 'pficon' directory |
| 55 | + include: [ |
| 56 | + path.resolve(__dirname, 'node_modules/patternfly/dist/fonts'), |
| 57 | + path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/fonts'), |
| 58 | + path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/pficon'), |
| 59 | + path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/fonts'), |
| 60 | + path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/pficon'), |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + test: /\.(jpg|jpeg|png|gif)$/i, |
| 65 | + include: [ |
| 66 | + path.resolve(__dirname, 'src'), |
| 67 | + path.resolve(__dirname, 'src/assets/images'), |
| 68 | + path.resolve(__dirname, 'node_modules/patternfly'), |
| 69 | + path.resolve(__dirname, 'node_modules/@patternfly/patternfly/assets/images'), |
| 70 | + path.resolve(__dirname, 'node_modules/@patternfly/react-styles/css/assets/images'), |
| 71 | + path.resolve(__dirname, 'node_modules/@patternfly/react-core/dist/styles/assets/images'), |
| 72 | + path.resolve( |
| 73 | + __dirname, |
| 74 | + 'node_modules/@patternfly/react-core/node_modules/@patternfly/react-styles/css/assets/images' |
| 75 | + ), |
| 76 | + path.resolve( |
| 77 | + __dirname, |
| 78 | + 'node_modules/@patternfly/react-table/node_modules/@patternfly/react-styles/css/assets/images' |
| 79 | + ), |
| 80 | + path.resolve( |
| 81 | + __dirname, |
| 82 | + 'node_modules/@patternfly/react-inline-edit-extension/node_modules/@patternfly/react-styles/css/assets/images' |
| 83 | + ), |
| 84 | + ], |
| 85 | + }, |
| 86 | + ], |
| 87 | + }, |
| 88 | + output: { |
| 89 | + filename: '[name].bundle-[contenthash].js', |
| 90 | + path: path.resolve(__dirname, 'dist'), |
| 91 | + publicPath: '/', |
| 92 | + chunkFilename: '[name].bundle-[contenthash].js', |
| 93 | + }, |
| 94 | + plugins: [ |
| 95 | + new HtmlWebpackPlugin({ |
| 96 | + template: path.resolve(__dirname, 'src', 'index.html'), |
| 97 | + filename: 'index.html', |
| 98 | + }), |
| 99 | + ], |
| 100 | + resolve: { |
| 101 | + extensions: ['.js', '.ts', '.tsx', '.jsx'], |
| 102 | + symlinks: false, |
| 103 | + cacheWithContext: false, |
| 104 | + }, |
| 105 | + watchOptions: { |
| 106 | + followSymlinks: true, |
| 107 | + }, |
| 108 | + snapshot: { |
| 109 | + managedPaths: [], |
| 110 | + }, |
| 111 | + optimization: { |
| 112 | + splitChunks: { |
| 113 | + chunks: 'all', |
| 114 | + }, |
| 115 | + }, |
| 116 | +}; |
| 117 | + |
| 118 | +/* Production settings */ |
| 119 | +if (NODE_ENV === 'production') { |
| 120 | + (config.optimization || {}).minimizer = [ |
| 121 | + new TerserJSPlugin({}), |
| 122 | + new CssMinimizerPlugin({ |
| 123 | + minimizerOptions: { |
| 124 | + preset: ['default', { mergeLonghand: false }], |
| 125 | + }, |
| 126 | + }), |
| 127 | + ]; |
| 128 | + config.plugins?.push( |
| 129 | + new MiniCssExtractPlugin({ |
| 130 | + filename: '[name]-[contenthash].css', |
| 131 | + chunkFilename: '[name].bundle-[contenthash].css', |
| 132 | + }) |
| 133 | + ); |
| 134 | + config.devtool = 'source-map'; |
| 135 | +} |
| 136 | + |
| 137 | +export default config; |
0 commit comments