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.

228 lines
7.5 KiB

  1. 'use strict';
  2. const glob = require('glob');
  3. const nodeExternals = require('webpack-node-externals');
  4. const path = require('path');
  5. const CleanPlugin = require('clean-webpack-plugin');
  6. const HtmlInlineSourcePlugin = require('html-webpack-inline-source-plugin');
  7. const HtmlPlugin = require('html-webpack-plugin');
  8. const ImageminPlugin = require('imagemin-webpack-plugin').default;
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  10. module.exports = function(env, argv) {
  11. env = env || {};
  12. env.production = !!env.production;
  13. env.optimizeImages = env.production || !!env.optimizeImages;
  14. return [getExtensionConfig(env), getUIConfig(env)];
  15. };
  16. function getExtensionConfig(env) {
  17. const plugins = [new CleanPlugin(['out'], { verbose: false })];
  18. return {
  19. name: 'extension',
  20. entry: './src/extension.ts',
  21. mode: env.production ? 'production' : 'development',
  22. target: 'node',
  23. devtool: !env.production ? 'eval-source-map' : undefined,
  24. output: {
  25. libraryTarget: 'commonjs2',
  26. filename: 'extension.js',
  27. path: path.resolve(__dirname, 'out'),
  28. devtoolModuleFilenameTemplate: 'file:///[absolute-resource-path]'
  29. },
  30. resolve: {
  31. extensions: ['.tsx', '.ts', '.js']
  32. },
  33. externals: [nodeExternals()],
  34. module: {
  35. rules: [
  36. {
  37. test: /\.ts$/,
  38. enforce: 'pre',
  39. use: 'tslint-loader'
  40. },
  41. {
  42. test: /\.tsx?$/,
  43. use: 'ts-loader',
  44. exclude: /node_modules/
  45. }
  46. ]
  47. },
  48. plugins: plugins,
  49. stats: {
  50. all: false,
  51. assets: true,
  52. builtAt: true,
  53. env: true,
  54. errors: true,
  55. timings: true,
  56. warnings: true
  57. }
  58. };
  59. }
  60. function getUIConfig(env) {
  61. const clean = ['settings.html', 'welcome.html'];
  62. if (env.optimizeImages) {
  63. console.log('Optimizing images (src/ui/images/settings/*.png)...');
  64. clean.push('images/settings');
  65. }
  66. const plugins = [
  67. new CleanPlugin(clean, { verbose: false }),
  68. new MiniCssExtractPlugin({
  69. filename: '[name].css'
  70. }),
  71. new HtmlPlugin({
  72. excludeAssets: [/.*\.main\.js/],
  73. excludeChunks: ['welcome'],
  74. template: 'settings/index.html',
  75. filename: path.resolve(__dirname, 'settings.html'),
  76. inject: true,
  77. inlineSource: env.production ? '.(js|css)$' : undefined,
  78. // inlineSource: '.(js|css)$',
  79. minify: env.production
  80. ? {
  81. removeComments: true,
  82. collapseWhitespace: true,
  83. removeRedundantAttributes: true,
  84. useShortDoctype: true,
  85. removeEmptyAttributes: true,
  86. removeStyleLinkTypeAttributes: true,
  87. keepClosingSlash: true
  88. }
  89. : false
  90. }),
  91. new HtmlPlugin({
  92. excludeAssets: [/.*\.main\.js/],
  93. excludeChunks: ['settings'],
  94. template: 'welcome/index.html',
  95. filename: path.resolve(__dirname, 'welcome.html'),
  96. inject: true,
  97. inlineSource: env.production ? '.(js|css)$' : undefined,
  98. // inlineSource: '.(js|css)$',
  99. minify: env.production
  100. ? {
  101. removeComments: true,
  102. collapseWhitespace: true,
  103. removeRedundantAttributes: true,
  104. useShortDoctype: true,
  105. removeEmptyAttributes: true,
  106. removeStyleLinkTypeAttributes: true,
  107. keepClosingSlash: true
  108. }
  109. : false
  110. }),
  111. new HtmlInlineSourcePlugin(),
  112. new ImageminPlugin({
  113. disable: !env.optimizeImages,
  114. externalImages: {
  115. context: path.resolve(__dirname, 'src/ui/images'),
  116. sources: glob.sync('src/ui/images/settings/*.png'),
  117. destination: path.resolve(__dirname, 'images')
  118. },
  119. gifsicle: null,
  120. jpegtran: null,
  121. optipng: null,
  122. pngquant: {
  123. quality: '85-100',
  124. speed: env.production ? 1 : 10
  125. },
  126. svgo: null
  127. })
  128. ];
  129. return {
  130. name: 'ui',
  131. context: path.resolve(__dirname, 'src/ui'),
  132. // This is ugly having main.scss on both bundles, but if it is added separately it will generate a js bundle :(
  133. entry: {
  134. settings: ['./settings/index.ts', './scss/main.scss'],
  135. welcome: ['./welcome/index.ts', './scss/main.scss']
  136. // main: ['./scss/main.scss']
  137. },
  138. mode: env.production ? 'production' : 'development',
  139. devtool: !env.production ? 'eval-source-map' : undefined,
  140. output: {
  141. filename: '[name].js',
  142. path: path.resolve(__dirname, 'out/ui'),
  143. publicPath: '{{root}}/out/ui/'
  144. },
  145. optimization: {
  146. splitChunks: {
  147. cacheGroups: {
  148. styles: {
  149. name: 'styles',
  150. test: /\.css$/,
  151. chunks: 'all',
  152. enforce: true
  153. }
  154. }
  155. }
  156. },
  157. resolve: {
  158. extensions: ['.tsx', '.ts', '.js'],
  159. modules: [path.resolve(__dirname, 'src/ui'), 'node_modules']
  160. },
  161. module: {
  162. rules: [
  163. {
  164. test: /\.ts$/,
  165. enforce: 'pre',
  166. use: [
  167. {
  168. loader: 'tslint-loader',
  169. options: {
  170. tsConfigFile: 'ui.tsconfig.json'
  171. }
  172. }
  173. ]
  174. },
  175. {
  176. test: /\.tsx?$/,
  177. use: {
  178. loader: 'ts-loader',
  179. options: {
  180. configFile: 'ui.tsconfig.json'
  181. }
  182. },
  183. exclude: /node_modules/
  184. },
  185. {
  186. test: /\.scss$/,
  187. use: [
  188. {
  189. loader: MiniCssExtractPlugin.loader
  190. },
  191. {
  192. loader: 'css-loader',
  193. options: {
  194. minimize: env.production,
  195. sourceMap: !env.production,
  196. url: false
  197. }
  198. },
  199. {
  200. loader: 'sass-loader',
  201. options: {
  202. sourceMap: !env.production
  203. }
  204. }
  205. ],
  206. exclude: /node_modules/
  207. }
  208. ]
  209. },
  210. plugins: plugins,
  211. stats: {
  212. all: false,
  213. assets: true,
  214. builtAt: true,
  215. env: true,
  216. errors: true,
  217. timings: true,
  218. warnings: true
  219. }
  220. };
  221. }