You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.4 KiB

  1. 'use strict';
  2. const glob = require('glob');
  3. const path = require('path');
  4. const CleanWebpackPlugin = require('clean-webpack-plugin');
  5. const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  6. const HtmlWebpackPlugin = require('html-webpack-plugin');
  7. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  8. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  9. module.exports = function(env, argv) {
  10. env = env || {};
  11. const production = !!env.production;
  12. const optimizeImages = production || !!env.optimizeImages;
  13. const clean = [];
  14. if (optimizeImages) {
  15. console.log('Optimizing images (src/ui/images/settings/*.png)...');
  16. clean.push('images/settings');
  17. }
  18. const plugins = [
  19. new CleanWebpackPlugin(clean),
  20. new MiniCssExtractPlugin({
  21. filename: '[name].css'
  22. }),
  23. new HtmlWebpackPlugin({
  24. excludeAssets: [/.*\.main\.js/],
  25. excludeChunks: ['welcome'],
  26. template: 'settings/index.html',
  27. filename: path.resolve(__dirname, 'settings.html'),
  28. inject: true,
  29. inlineSource: production ? '.(js|css)$' : undefined,
  30. // inlineSource: '.(js|css)$',
  31. minify: production
  32. ? {
  33. removeComments: true,
  34. collapseWhitespace: true,
  35. removeRedundantAttributes: true,
  36. useShortDoctype: true,
  37. removeEmptyAttributes: true,
  38. removeStyleLinkTypeAttributes: true,
  39. keepClosingSlash: true
  40. }
  41. : false
  42. }),
  43. new HtmlWebpackPlugin({
  44. excludeAssets: [/.*\.main\.js/],
  45. excludeChunks: ['settings'],
  46. template: 'welcome/index.html',
  47. filename: path.resolve(__dirname, 'welcome.html'),
  48. inject: true,
  49. inlineSource: production ? '.(js|css)$' : undefined,
  50. // inlineSource: '.(js|css)$',
  51. minify: production
  52. ? {
  53. removeComments: true,
  54. collapseWhitespace: true,
  55. removeRedundantAttributes: true,
  56. useShortDoctype: true,
  57. removeEmptyAttributes: true,
  58. removeStyleLinkTypeAttributes: true,
  59. keepClosingSlash: true
  60. }
  61. : false
  62. }),
  63. new HtmlWebpackInlineSourcePlugin(),
  64. new ImageminPlugin({
  65. disable: !optimizeImages,
  66. externalImages: {
  67. context: path.resolve(__dirname, 'src/ui/images'),
  68. sources: glob.sync('src/ui/images/settings/*.png'),
  69. destination: path.resolve(__dirname, 'images')
  70. },
  71. gifsicle: null,
  72. jpegtran: null,
  73. optipng: null,
  74. pngquant: {
  75. quality: '85-100',
  76. speed: production ? 1 : 10
  77. },
  78. svgo: null
  79. })
  80. ];
  81. return {
  82. context: path.resolve(__dirname, 'src/ui'),
  83. // This is ugly having main.scss on both bundles, but if it is added separately it will generate a js bundle :(
  84. entry: {
  85. settings: ['./settings/index.ts', './scss/main.scss'],
  86. welcome: ['./welcome/index.ts', './scss/main.scss']
  87. // main: ['./scss/main.scss']
  88. },
  89. mode: production ? 'production' : 'development',
  90. devtool: !production ? 'eval-source-map' : undefined,
  91. output: {
  92. filename: '[name].js',
  93. path: path.resolve(__dirname, 'out/ui'),
  94. publicPath: '{{root}}/out/ui/'
  95. },
  96. optimization: {
  97. splitChunks: {
  98. cacheGroups: {
  99. styles: {
  100. name: 'styles',
  101. test: /\.css$/,
  102. chunks: 'all',
  103. enforce: true
  104. }
  105. }
  106. }
  107. },
  108. resolve: {
  109. extensions: ['.tsx', '.ts', '.js'],
  110. modules: [path.resolve(__dirname, 'src/ui'), 'node_modules']
  111. },
  112. module: {
  113. rules: [
  114. {
  115. test: /\.tsx?$/,
  116. use: {
  117. loader: 'ts-loader',
  118. options: {
  119. configFile: 'ui.tsconfig.json'
  120. }
  121. },
  122. exclude: /node_modules/
  123. },
  124. {
  125. test: /\.scss$/,
  126. use: [
  127. {
  128. loader: MiniCssExtractPlugin.loader
  129. },
  130. {
  131. loader: 'css-loader',
  132. options: {
  133. minimize: production,
  134. sourceMap: !production,
  135. url: false
  136. }
  137. },
  138. {
  139. loader: 'sass-loader',
  140. options: {
  141. sourceMap: !production
  142. }
  143. }
  144. ],
  145. exclude: /node_modules/
  146. }
  147. ]
  148. },
  149. plugins: plugins
  150. };
  151. };