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.

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