Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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