webpack.config.ts 2.5 KB

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