webpack.config.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* eslint-env node */
  2. import * as path from 'path';
  3. import { Configuration as WebpackConfiguration } from 'webpack';
  4. import { Configuration as WebpackDevServerConfiguration } from 'webpack-dev-server';
  5. import { ConsoleRemotePlugin } from '@openshift-console/dynamic-plugin-sdk-webpack';
  6. const CopyWebpackPlugin = require('copy-webpack-plugin');
  7. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  8. const isProd = process.env.NODE_ENV === 'production';
  9. interface Configuration extends WebpackConfiguration {
  10. devServer?: WebpackDevServerConfiguration;
  11. }
  12. const config: Configuration = {
  13. mode: isProd ? 'production' : 'development',
  14. // No regular entry points needed. All plugin related scripts are generated via ConsoleRemotePlugin.
  15. entry: {},
  16. context: path.resolve(__dirname, 'src'),
  17. output: {
  18. path: path.resolve(__dirname, 'dist'),
  19. filename: isProd ? '[name]-bundle-[hash].min.js' : '[name]-bundle.js',
  20. chunkFilename: isProd ? '[name]-chunk-[chunkhash].min.js' : '[name]-chunk.js',
  21. },
  22. resolve: {
  23. extensions: ['.ts', '.tsx', '.js', '.jsx'],
  24. },
  25. module: {
  26. rules: [
  27. {
  28. test: /\.(jsx?|tsx?)$/,
  29. exclude: /\/node_modules\//,
  30. use: ['swc-loader'],
  31. },
  32. {
  33. test: /\.(css)$/,
  34. use: ['style-loader', 'css-loader'],
  35. },
  36. {
  37. test: /\.(png|jpg|jpeg|gif|svg|woff2?|ttf|eot|otf)(\?.*$|$)/,
  38. type: 'asset/resource',
  39. generator: {
  40. filename: isProd ? 'assets/[contenthash][ext]' : 'assets/[name][ext]',
  41. },
  42. },
  43. {
  44. test: /\.(m?js)$/,
  45. resolve: {
  46. fullySpecified: false,
  47. },
  48. },
  49. ],
  50. },
  51. devServer: {
  52. static: './dist',
  53. port: 9001,
  54. // Allow Bridge running in a container to connect to the plugin dev server.
  55. allowedHosts: 'all',
  56. headers: {
  57. 'Access-Control-Allow-Origin': '*',
  58. 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
  59. 'Access-Control-Allow-Headers': 'X-Requested-With, Content-Type, Authorization',
  60. },
  61. devMiddleware: {
  62. writeToDisk: true,
  63. },
  64. },
  65. plugins: [
  66. new ConsoleRemotePlugin(),
  67. new ForkTsCheckerWebpackPlugin({
  68. typescript: {
  69. configFile: path.resolve(__dirname, 'tsconfig.json'),
  70. },
  71. }),
  72. new CopyWebpackPlugin({
  73. patterns: [{ from: path.resolve(__dirname, 'locales'), to: 'locales' }],
  74. }),
  75. ],
  76. devtool: isProd ? false : 'source-map',
  77. optimization: {
  78. chunkIds: isProd ? 'deterministic' : 'named',
  79. minimize: isProd,
  80. },
  81. };
  82. export default config;