webpack.config.ts 2.5 KB

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